R:将 ggplot2 对象保存在 .Rds 文件中以读取到不同的环境中
R: Save ggplot2 object in a .Rds file to be read into a different environment
我正在构建一个允许用户下载大量图表的应用程序。
为了减少应用程序的处理时间,我想在一个单独的离线脚本中生成这些图。
我尝试将绘图(用 ggplot2
生成)保存在单独的 .RDS
文件中,但在将它们读入我的应用程序代码时出现以下错误:
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (10): colour, x, y, label
我的猜测是图表使用的环境变量没有保存到 .RDS
文件中。
有人知道实现我想要做的事情的方法吗?
这里是重现错误的最小代码片段:
prep_code.R
library(data.table)
library(ggplot2)
# Just a sample data table---------------
plot_tbl <- data.table(date = c(as.Date("2019-11-25", "%Y-%m-%d"), as.Date("2019-11-24", "%Y-%m-%d"),
as.Date("2019-11-23", "%Y-%m-%d"), as.Date("2019-11-22", "%Y-%m-%d"),
as.Date("2019-11-21", "%Y-%m-%d"), as.Date("2019-11-25", "%Y-%m-%d"),
as.Date("2019-11-24", "%Y-%m-%d"), as.Date("2019-11-23", "%Y-%m-%d"),
as.Date("2019-11-22", "%Y-%m-%d"), as.Date("2019-11-21", "%Y-%m-%d")),
perc = c(0.12, 0.13, 0.17, 0.26, 0.47, 0.05, 0.07, 0.09, 0.15, 0.27),
source = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"))
# Generate the plot using ggplot2---------------
g <- ggplot(plot_tbl, aes(x = date, y = perc, label = perc))
g <- g + geom_line(aes(color = source))
# Save the plot as a .Rds file
saveRDS(g, "/path/to/my/plot/example_plot.Rds", version = 2)
App "helper" 代码(不是 server.R
或 ui.R
,而是辅助脚本 server.R
来源):
helper.R
# I am including all of the libraries used in my actual code in case the issue is caused by some
# incompatibility between packages---------------
library(data.table)
library(zoo)
library(dplyr)
library(plotly)
library(gridExtra)
library(ggplot2)
# Read in the plot---------------
g <- readRDS("/datadg/rndFS/shockett/Data/Misc/example_plot.Rds")
# Attempt to plot g, which returns the error specified above---------------
plot(g)
这是我的会话信息。请注意 prep_code.R
和 helper.R
在不同的机器上使用。
prep_code.R
会话信息:
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.0.0 data.table_1.11.8
loaded via a namespace (and not attached):
[1] Rcpp_0.12.19 rstudioapi_0.8 bindr_0.1.1 magrittr_1.5
[5] tidyselect_0.2.5 munsell_0.5.0 colorspace_1.3-2 R6_2.2.2
[9] rlang_0.3.0.1 plyr_1.8.4 dplyr_0.7.6 tools_3.6.1
[13] grid_3.6.1 gtable_0.2.0 withr_2.1.2 lazyeval_0.2.1
[17] assertthat_0.2.0 digest_0.6.17 tibble_1.4.2 crayon_1.3.4
[21] bindrcpp_0.2.2 purrr_0.2.5 glue_1.3.0 labeling_0.3
[25] compiler_3.6.1 pillar_1.3.0 scales_1.0.0 pkgconfig_2.0.2
helper.R
会话信息:
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] png_0.1-7 gridExtra_2.3 bindrcpp_0.2
[4] shinyjs_1.0 DT_0.2 shinyWidgets_0.4.4
[7] shinydashboardPlus_0.6.0 shinydashboard_0.7.1 shiny_1.0.5
[10] plotly_4.7.1.9000 ggplot2_2.2.1.9000 dplyr_0.7.4
[13] zoo_1.8-1 data.table_1.10.4-3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 later_0.7.5 pillar_1.3.1 compiler_3.4.4
[5] plyr_1.8.4 bindr_0.1 tools_3.4.4 digest_0.6.13
[9] jsonlite_1.5 tibble_2.0.1 gtable_0.2.0 lattice_0.20-38
[13] viridisLite_0.2.0 pkgconfig_2.0.2 rlang_0.3.1 rstudioapi_0.7
[17] yaml_2.1.15 httr_1.3.1 htmlwidgets_0.9 grid_3.4.4
[21] glue_1.2.0 R6_2.2.2 purrr_0.2.4 tidyr_0.7.2
[25] magrittr_1.5 scales_0.5.0.9000 promises_1.0.1 htmltools_0.3.6
[29] assertthat_0.2.0 mime_0.5 colorspace_1.3-2 xtable_1.8-2
[33] httpuv_1.3.5 lazyeval_0.2.1 munsell_0.4.3 crayon_1.3.4
因为您的 helper.R
环境使用 ggplot2
版本 2.2.1,您需要使用 packrat 或类似工具在本地为您的 prep_code.R
脚本安装此版本.
致 set up packrat 运行 init()
:
install.packages("packrat")
packrat::init("/path/to/project")
然后重新安装一切。使用devtools
安装ggplot2 2.2.1版本:
install.packages("data.table")
install.packages("devtools") # see note
devtools::install_version("ggplot2", version = "2.2.1")
(注意:如果全局安装了devtools,查看packrat中的external.packages选项)
现在您可以 运行 prep_code.R
在此 packrat 项目中,一切都应该正常工作。
另请注意,如果 R 在此目录中进行了初始化,则它只会识别 packrat。在Rstudio中使用packrat创建工程最方便
我正在构建一个允许用户下载大量图表的应用程序。 为了减少应用程序的处理时间,我想在一个单独的离线脚本中生成这些图。
我尝试将绘图(用 ggplot2
生成)保存在单独的 .RDS
文件中,但在将它们读入我的应用程序代码时出现以下错误:
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Don't know how to automatically pick scale for object of type quosure/formula. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (10): colour, x, y, label
我的猜测是图表使用的环境变量没有保存到 .RDS
文件中。
有人知道实现我想要做的事情的方法吗?
这里是重现错误的最小代码片段:
prep_code.R
library(data.table)
library(ggplot2)
# Just a sample data table---------------
plot_tbl <- data.table(date = c(as.Date("2019-11-25", "%Y-%m-%d"), as.Date("2019-11-24", "%Y-%m-%d"),
as.Date("2019-11-23", "%Y-%m-%d"), as.Date("2019-11-22", "%Y-%m-%d"),
as.Date("2019-11-21", "%Y-%m-%d"), as.Date("2019-11-25", "%Y-%m-%d"),
as.Date("2019-11-24", "%Y-%m-%d"), as.Date("2019-11-23", "%Y-%m-%d"),
as.Date("2019-11-22", "%Y-%m-%d"), as.Date("2019-11-21", "%Y-%m-%d")),
perc = c(0.12, 0.13, 0.17, 0.26, 0.47, 0.05, 0.07, 0.09, 0.15, 0.27),
source = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"))
# Generate the plot using ggplot2---------------
g <- ggplot(plot_tbl, aes(x = date, y = perc, label = perc))
g <- g + geom_line(aes(color = source))
# Save the plot as a .Rds file
saveRDS(g, "/path/to/my/plot/example_plot.Rds", version = 2)
App "helper" 代码(不是 server.R
或 ui.R
,而是辅助脚本 server.R
来源):
helper.R
# I am including all of the libraries used in my actual code in case the issue is caused by some
# incompatibility between packages---------------
library(data.table)
library(zoo)
library(dplyr)
library(plotly)
library(gridExtra)
library(ggplot2)
# Read in the plot---------------
g <- readRDS("/datadg/rndFS/shockett/Data/Misc/example_plot.Rds")
# Attempt to plot g, which returns the error specified above---------------
plot(g)
这是我的会话信息。请注意 prep_code.R
和 helper.R
在不同的机器上使用。
prep_code.R
会话信息:
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.0.0 data.table_1.11.8
loaded via a namespace (and not attached):
[1] Rcpp_0.12.19 rstudioapi_0.8 bindr_0.1.1 magrittr_1.5
[5] tidyselect_0.2.5 munsell_0.5.0 colorspace_1.3-2 R6_2.2.2
[9] rlang_0.3.0.1 plyr_1.8.4 dplyr_0.7.6 tools_3.6.1
[13] grid_3.6.1 gtable_0.2.0 withr_2.1.2 lazyeval_0.2.1
[17] assertthat_0.2.0 digest_0.6.17 tibble_1.4.2 crayon_1.3.4
[21] bindrcpp_0.2.2 purrr_0.2.5 glue_1.3.0 labeling_0.3
[25] compiler_3.6.1 pillar_1.3.0 scales_1.0.0 pkgconfig_2.0.2
helper.R
会话信息:
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] png_0.1-7 gridExtra_2.3 bindrcpp_0.2
[4] shinyjs_1.0 DT_0.2 shinyWidgets_0.4.4
[7] shinydashboardPlus_0.6.0 shinydashboard_0.7.1 shiny_1.0.5
[10] plotly_4.7.1.9000 ggplot2_2.2.1.9000 dplyr_0.7.4
[13] zoo_1.8-1 data.table_1.10.4-3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 later_0.7.5 pillar_1.3.1 compiler_3.4.4
[5] plyr_1.8.4 bindr_0.1 tools_3.4.4 digest_0.6.13
[9] jsonlite_1.5 tibble_2.0.1 gtable_0.2.0 lattice_0.20-38
[13] viridisLite_0.2.0 pkgconfig_2.0.2 rlang_0.3.1 rstudioapi_0.7
[17] yaml_2.1.15 httr_1.3.1 htmlwidgets_0.9 grid_3.4.4
[21] glue_1.2.0 R6_2.2.2 purrr_0.2.4 tidyr_0.7.2
[25] magrittr_1.5 scales_0.5.0.9000 promises_1.0.1 htmltools_0.3.6
[29] assertthat_0.2.0 mime_0.5 colorspace_1.3-2 xtable_1.8-2
[33] httpuv_1.3.5 lazyeval_0.2.1 munsell_0.4.3 crayon_1.3.4
因为您的 helper.R
环境使用 ggplot2
版本 2.2.1,您需要使用 packrat 或类似工具在本地为您的 prep_code.R
脚本安装此版本.
致 set up packrat 运行 init()
:
install.packages("packrat")
packrat::init("/path/to/project")
然后重新安装一切。使用devtools
安装ggplot2 2.2.1版本:
install.packages("data.table")
install.packages("devtools") # see note
devtools::install_version("ggplot2", version = "2.2.1")
(注意:如果全局安装了devtools,查看packrat中的external.packages选项)
现在您可以 运行 prep_code.R
在此 packrat 项目中,一切都应该正常工作。
另请注意,如果 R 在此目录中进行了初始化,则它只会识别 packrat。在Rstudio中使用packrat创建工程最方便