I am new to shiny and apologize for the naive question of updating uiOutput/insertUI. In my setup, I first saved a parameter file at "Enzyme = Special" and "Value = 10". I next closed and reopened the app and reloaded the parameter file. I need to do the loading twice in order to update correctly the uiOutput field. My suspect is that the first loading updated the dynamic UI and the second updated subsequently the ""Value". Would there be a way to update all the fields at once. Your helps on this would be greatly appreciated. My snippet is shown below.
library(shiny)
library(shinyFiles)
library(fs)
myUI <- function(id, choices = c("Default", "Special"))
{
tagList(
selectInput(NS(id, "enzyme"), "Enzyme", choices, selected = "Default"),
uiOutput(NS(id, "with_special")),
)
}
myServer <- function(id)
{
moduleServer(id,
function(input, output, session) {
enzyme <- reactive(input$enzyme)
observe({
if (enzyme() == "Special")
output$with_special <- renderUI(numericInput(NS(id, "more"), "Value", value = 0))
})
list(enzyme = reactive(input$enzyme), more = reactive(input$more))
}
)
}
ui <- fluidPage(
myUI("MID"),
shinySaveButton("savepars", "Save parameters", "Save file",
filetype = list(text = "pars"), viewtype = "icon"),
shinyFilesButton("loadpars", "Load parameters", "Please select a file",
filetype = list(text = "pars"), multiple = FALSE, viewtype = "detail"),
)
server <- shinyServer(function(input, output, session) {
volumes <- c(Home = fs::path_home(), getVolumes()())
ans <- myServer("MID")
enzyme <- ans$enzyme
more <- ans$more
pars <- reactive(list(enzyme = enzyme(), more = more()))
observeEvent(input$savepars, {
shinyFileSave(input, "savepars", roots = volumes, session = session,
restrictions = system.file(package = "base"))
fileinfo <- parseSavePath(volumes, input$savepars)
if (nrow(fileinfo))
saveRDS(pars(), fileinfo$datapath)
})
observeEvent(input$loadpars, {
shinyFileChoose(input, "loadpars", roots = volumes, session = session)
if (is.integer(input$loadpars))
NULL
else {
fileinfo <- parseFilePaths(volumes, input$loadpars)
fileinfo <- gsub("\\\\", "/", unname(fileinfo[["datapath"]]))
cached_pars <- readRDS(fileinfo)
updateSelectInput(session, NS("MID", "enzyme"), "Enzyme", choices = c("Default", "Special"),
selected = cached_pars$enzyme)
updateNumericInput(session, NS("MID", "more"), "Value", value = cached_pars$more)
}
})
})
shinyApp(ui, server)