saveRDS inflating size of object

后端 未结 2 965
难免孤独
难免孤独 2020-12-30 09:55

This is a tricky one as I can\'t provide a reproducible example, but I\'m hoping that others may have had experience dealing with this.

Essentially I have a function

2条回答
  •  既然无缘
    2020-12-30 10:07

    For model objects you could also simply delete the reference to the environment.

    As for example like this

    ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
    trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
    group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
    weight <- c(ctl, trt)
    lm.D9 <- lm(weight ~ group) 
    
    attr(lm.D9$terms, ".Environment") <- NULL
    saveRDS(lm.D9, file = "path_to_save.RDS")
    

    This unfortunatly breaks the model - but you can add an environment manualy after loading again.

    readRDS("path_to_save.RDS")
    attr(lm.D9$terms, ".Environment") <- globalenv()
    

    This helped me in my specific use case and looks a bit saver to me...

提交回复
热议问题