In Export all user inputs in a Shiny app to file and load them later the question was answered, how to save,load, and export inputs of a shiny app. I tried to adapt the simple solution given by Ron Talbot saving all inputs in an .RDS file and sending them later to the session via an action button using the
session$sendInputMessage()
function together with a for loop.
library(shiny)
shinyApp(
ui <- shinyUI(fluidPage(
radioButtons("inRadio", "Radio buttons:",
c("label 1", "label 2")),
uiOutput("alternative_Inputs"),
actionButton("load_inputs", "Load inputs"),
actionButton('save_inputs', 'Save inputs')
)),
server <- shinyServer(function(input, output,session) {
observeEvent(input$load_inputs,{
if(!file.exists('inputs.RDS')) {return(NULL)}
savedInputs <- readRDS('inputs.RDS')
inputIDs <- names(savedInputs)
inputvalues <- unlist(savedInputs)
for (i in 1:length(savedInputs)) {
session$sendInputMessage(inputIDs[i], list(value=inputvalues[[i]]) )
}
})
observeEvent(input$save_inputs,{
saveRDS( reactiveValuesToList(input) , file = 'inputs.RDS')
})
output$alternative_Inputs <- renderUI({
if (input$inRadio == "label 1"){
numericInput("Test1", "Test1", value = 1)}
else{
numericInput("Test2", "Test2", value = 2)}
})
})
)
In this code, dependent on the selected radio button, a numericInput "Test1" or "Test2" will appear. Let's assume you save the settings radio button "label 2" and numeric input "Test 2" with value = 5. If you change the radio button to "label 1" and reload the former settings, you will only change the radio button to "label 2", but the value will be 2, as defined as the default value. If you press load again, the label will change, since the "new" numeric input is available in the session. I am struggling to get around this problem, to press the action button just once to update all inputs.