我正在构建一个允许用户下载大量图表的应用程序。 为了减少应用程序的处理时间,我想在一个单独的脱机脚本中生成这些图。
我尝试将由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)
应用“帮助程序”代码(不是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
答案 0 :(得分:1)
由于您的helper.R
环境使用的是ggplot2
版本2.2.1,因此您需要使用packrat或类似的工具在prep_code.R
脚本中本地安装此版本。 / p>
运行init()
到set up packrat:
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选项)
现在,您可以在这个packrat项目中运行prep_code.R
,并且一切正常。
还请注意,如果R在此目录中初始化,则R仅会识别packrat。在Rstudio中,使用packrat时创建项目最方便。