简体   繁体   中英

R: Save ggplot2 object in a .Rds file to be read into a different environment

I am building an application that will allow a user to download a large number of plots. To reduce the processing time in app, I'd like to generate these plots in a separate, offline script.

I have tried saving the plots (generated with ggplot2 ) in separate .RDS files, but am getting the following error when reading them into my app's code:

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

My guess is that the environment variables the graph is using aren't being saved into the .RDS files. Does anybody know a way to implement what I am trying to do?

Here is a minimal piece of code that reproduces the error:

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" code (not server.R or ui.R , but a helper script server.R sources):

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)

Here is my session info. Please note that prep_code.R and helper.R are utilized on different machines.

prep_code.R session info:

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 session info:

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     

Because your helper.R environment uses ggplot2 version 2.2.1, you need to use packrat or a similar tool to install this version locally for your prep_code.R script.

To set up packrat run init() :

install.packages("packrat")
packrat::init("/path/to/project")

Then reinstall everything. Use devtools to install ggplot2 version 2.2.1:

install.packages("data.table")
install.packages("devtools") # see note
devtools::install_version("ggplot2", version = "2.2.1")

(note: if devtools is installed globally, check out the external.packages option in packrat)

Now you can run prep_code.R within this packrat project and everything should work properly.

Also note that R will only recognize packrat if it is initialized within this directory. In Rstudio it's most convenient to create a project when using packrat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM