Save shiny objects to an Rdata file

1,083 views
Skip to first unread message

Gene Leynes

unread,
Dec 15, 2015, 9:37:52 AM12/15/15
to Shiny - Web Framework for R

I often wish that I could interactively work within my shiny application interactively, similar to using browse()in a function., and sometimes I use a shiny GUI to get to a state where I just want to save things.

So, I wrote something that can save all the shiny application objects,which uses Hadley's pryr package to recursively list the objects. So you need this somewhere (I think it belongs in global.R or server.R)
library(pryr)

In ui.R you need an action button.  You could add this to one of the existing shiny examples.
actionButton("save_objs", "Save Objects")

In server.R I have this code, which listens to save_objs:

    observeEvent(input$save_objs, {
       
# Run whenever save_objs button is pressed

       
print("** saving objects! **")
       
       
## Print the objects being saved
       
print(rls())
       
# ## Put  objects into current environment
       
for(obj in unlist(rls())) {
           
if(class(get(obj, pos =  -1))[1] == "reactive"){
               
## execute the reactive objects and put them in to this
               
## environment i.e. into the environment of this function
                assign
(obj, value = eval(call(obj)))
           
} else {
               
## grab the global variables and put them into this
               
## environment
                assign
(obj, value = get(obj, pos =  -1))
           
}
       
}
       
        input_copy
<- list()
       
for(nm in names(input)){
           
# assign(paste0("input_copy$", nm), value <- input[[nm]])
            input_copy
[[nm]] <- input[[nm]]
       
}
       
       
## save objects in current environment
        save
(list = ls(), file = "shiny_env.Rdata", envir = environment())
       
       
print("** done saving     **")
   
})

I use print statements liberally during development, feel free to omit them.

This took me a while to figure out, so hopefully it will save someone else time (maybe even me later when I forget and google "R how to save objects in shiny").

As a side note, I can't understand how the rls can find the environment with the reactive objects.  It's not in my current budget to buy Hadley's Advanced Programming in R, so I'll have to wait to find out. I tried to get the objects directly with various combinations of parent.frame and parent.env, but I could only find Shiny internal objects and objects in my normal search path.  So... there's some clever stuff in that pryr package.


Gene Leynes

unread,
Dec 15, 2015, 10:47:29 AM12/15/15
to Shiny - Web Framework for R
I wanted to mention a couple more things:
  1. This code isn't perfect; for example I'm not sure if the get(obj, pos =  -1) should just be get(obj). This seems to work, and I got tired of testing different things.
  2. I don't normally advocate using .Rdata files, I usually save specific objects using saveRDS, but in this case it's more convenient to save the whole environment.


Reply all
Reply to author
Forward
0 new messages