1

I'm trying to create a shiny app that allows me to fit linear models, display informations about them, and then save them.

I'm facing an issue : when I save a model, it takes a huge place. Here is a simplified code :

library(shiny)
library(ggplot2)

ui <- shinyUI(fluidPage(
    titlePanel("Save linear Model"),

    sidebarLayout(
      sidebarPanel(
        actionButton("save","SAVE !")
      ),

      mainPanel(
       textOutput("saved")
      )
    )
))


server <- shinyServer(function(input, output) {

  load("donnees/new/V283/V283_complete.RData")

  observeEvent(input$save,{

    lm.fit<-lm(price~.,data=diamonds)
    save(lm.fit, file="question-x-validated/my-model.RData")
    output$saved<-renderText("Saved")

  })

})

shinyApp(ui = ui, server = server)

In fact, the more objects I create/load in my app, the bigger my saved model is. For exemple, the object I load with :

load("donnees/new/V283/V283_complete.RData")

is 275.1MB. If I save my lm.fit after loading it, my rdata file is 36.9Mb. If I save my lm.fit without loading it, my file is 13Mb. If I save my lm.fit directly from R (without using my shiny app), the file is 6,57Mb

As suggested in this link, it might be an environment issue. But it seems to me that the fact I'm using shiny is adding some difficulties, as none of the technique suggested by the above link worked in my case.

I also tried using the saveRDS function. And also replaced :

lm.fit<-lm(price~.,data=diamonds)

by

assign("lm.fit",lm(price~.,data=diamonds),envir=globalenv())

It changes the file size, but It never gets as little as 6.57Mb.

As in my real code, I import really big data sets, my real models gets really huge (more than 500Mb), and It makes my Shiny App really slow when it loads/saves these models.

I would really appreciate any help you can provide.

EDIT :

It seems my issue comes from the "terms" element of my model, as if I do :

lm.fit<-lm.fit[1:11]

prior to saving my model, my file is 5.92Mb ! But as you know, the "terms" element is needed to use predict(). And only doing :

attr(lm.fit$terms,".Environment") = c()

does not work.

Also, funny thing :

lm.fit$terms<-NULL and lm.fit<-lm.fit[-12] does not change anything while lm.fit<-lm.fit[1:11] does.

2
  • Voting to migrate to SO. Please see advice in the Help Center on software-specific questions. In essence, CV is not for advice on or debugging code.
    – Nick Cox
    Mar 30, 2016 at 9:05
  • This seems to be primarily a coding problem, rather than a statistical one - have a look at our help center to see what's on-topic on CrossValidated. It seems to me that you have provided enough information to get an answer at StackOverflow; the most likely outcome is that this will be migrated there, so you don't need to cross-post it yourself.
    – Silverfish
    Mar 30, 2016 at 9:06

1 Answer 1

1

Check out this great post for some methods/info on reducing the size of the fat on glm/lm objects.

I use this method, which I took from the above.

3
  • I edited my post : my issue seems to come from the terms element, but using attr(lm.fit$terms,".Environment") = c() as suggested in your post does not work.
    – mdebbiche
    Mar 30, 2016 at 9:57
  • Does this code run for you? I can cut down lm and glm objects without a problem. The attr(lm.fit$terms,".Environment") = c() line runs successfully. What is the object.size before and after running strip_glm for your model?
    – Dex Groves
    Mar 30, 2016 at 10:22
  • Mea culpa, it works fine. Saved so much space. Thank you very much.
    – mdebbiche
    Mar 30, 2016 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.