small ggplot object (1 mb) turns into 7 gigabyte .Rdata object when saved

后端 未结 1 1128
栀梦
栀梦 2020-12-18 12:22

I have a series of ggplot objects that I\'m trying to save to an .rdata file to load into a Markdown document later. The ggplot object itself is quite small (a few KB). But,

相关标签:
1条回答
  • 2020-12-18 12:57

    I stumbled upon this problem as well. This is indeed related to the environment. If you want to save your plots as an Rdata file, then you should be creating a new environment inside the function that is generating your plot, so that the complete environment doesn't get saved. Example:

    makePlot <- function(plot.data){
      env <- new.env(parent = globalenv())
      env$subset <- plot.data
    
      my.plot <- with(env, {
        my.plot <- ggplot(subset, ...) 
        return(my.plot)
      })
    
      return(my.plot)
    }
    
    0 讨论(0)
提交回复
热议问题