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)
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.