I tried to save the R environment in an .RData file using save.image
but 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)
session
object which contains all the inputs and outputs. See shiny.rstudio.com/reference/shiny/latest/session.html – Xiongbing Jin