I am trying to develop a shiny app. In this shiny app, the user could save the input values and also load the saved state. But I have some problems with updating the renderUI after loading the saved state. For example, I select "Add data" for Data Type, put "mydata" as the Description, and then click the "Save" button. Then I rerun the shiny app and click the "Load" button. "Add data" will show in Data Type but the Description input is empty. If I click the "Load" button again, then "mydata" will show in the Description. So to fully load the saved state, I have to click the "Load" button twice.
Here is my code:
# Define the UI
ui <- bootstrapPage(
selectInput("dattype",
label = h5("Data Type"),
choices = c("Default data",
"Add data"),
selected = "Default data"),
uiOutput("datdesc"),
conditionalPanel(
condition = "input.dattype == 'Add data' ",
fileInput("dat.file",
label=h5("Upload Permeability Data"),
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
actionButton("save", "Save"),
actionButton("load", "Load")
)
# Define the server code
server <- function(input, output,session) {
output$datdesc <- renderUI({
if(input$dattype=="Add data") {
textInput("datdesc1",
label = h5("Description"))
} else {
selectInput("datdesc2",
label = h5("Description"),
choices = c("Iris","Car"))
}
})
observeEvent(input$save, {
values <<- lapply(reactiveValuesToList(input), unclass)
})
observeEvent(input$load, {
if (exists("values")) {
lapply(names(values),
function(x) session$sendInputMessage(x, list(value = values[[x]]))
)
}
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
This is just a simple example code. My shiny app is very complicated and the rendUI must be used in the Server. Does anybody know how to load the saved state one time and update the input of Description?