0

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.

Community
  • 1
  • 1
T.B90
  • 13
  • 2
  • could this be a shortcout for you? https://shiny.rstudio.com/gallery/bookmarking-url.html – Tonio Liebrand Jan 24 '17 at 18:39
  • Thanks for your answer, but unfortunately this is not the output I want to obtain. Maybe I am not advanced enough to fully understand the functionality of this bookmarking, but I like the idea of having this old-school save and load button system in the shiny app. I only need an opportunity to run the loading button code twice after the button is pressed only once. That would solve my problem in a sufficient manner. – T.B90 Jan 25 '17 at 14:15
  • 1
    Okay... after more days on working on this (frustrating) topic, I managed to understand this bookmarking and its benefits and now I use it successfully. Thanks for this advice! – T.B90 Jan 27 '17 at 15:38

0 Answers0