2
votes

I've developed a shiny application (using shinydashboard) and i'd like to save the "session" (by session i mean all the values of the input and the data load by the user). I want to save it in a .RData file and then be able to restart the app, load the .RData file and get back the data and the input defined by the user and hence the output...

Is there a way to do such a thing with shiny ?

Thanks

2
Yes there is a session object which contains all the inputs and outputs. See shiny.rstudio.com/reference/shiny/latest/session.htmlXiongbing Jin

2 Answers

1
votes

I tried to save the R environment in an .RData file using save.imagebut it did not work out. What worked though is using the save and load functions to store and restore as .rda files.

As for naming you could use a timestamp maybe to differentiate between users.

Edit (example)

Okay, so in this app there are two selectInput elements: first, and second. If any of these change, the values of these inputs are then assigned to two variables: first_var and second_var which are saved to test.rda file. If this file exists the variables are loaded into the session.

So basically, if you run the app first, whenever you change the inputs, they are saved into a .rda file. If you exit and then rerun the app, the variables are loaded, and they are set as the selected value of the inputs.

library(shiny)
if(file.exists("test.rda")) load("test.rda")

ui <- fluidPage(
  selectInput("first",
              label = "First",
              choices = c("Value A", "Value B", "Value C"),
              selected = ifelse(exists("first_var"), first_var, "Value A")
  ),
  selectInput("second",
              label = "Second",
              choices = c("Value D", "Value E", "Value F"),
              selected = ifelse(exists("second_var"), second_var, "Value D")
  )
)

server <- function(input, output, session){
  observe({
    first_var <- input$first
    second_var <- input$second
    save(file = "test.rda", list = c("first_var", "second_var"))
  })
}

shinyApp(ui, server)
0
votes

Shiny has just released version 0.14, which includes bookmarking that may be a built-in approach to meet your needs. There is a specific tutorial on "advanced bookmarking" for such purposes as dashboards that would be suitable in your case: http://shiny.rstudio.com/articles/advanced-bookmarking.html http://shiny.rstudio.com/articles/bookmarking-state.html

The 0.14 upgrade requires a current R version (3.3, I think?).