Batanichek Batanichek - 1 month ago 10
R Question

Save state of input shiny

Is there good way to save state( dont reset ) of shinyinput which generated on server side?

Example

ui=shinyUI(fluidPage(
selectInput("select","",choices = c(1,2),multiple = T),
uiOutput("din_ui")
))

server=function(input, output,session) {
output$din_ui=renderUI({
lapply(input$select,function(i){
numericInput(inputId = paste0("num_",i),i,"")
})
})

}

shinyApp(ui,server)


If i select 1 in
select
insert some values into
num_1
than add 2 in
select
than
num_1
reset to start value.

Answer

You can read the numericInput value, and set the control value at control init. See code:

library(shiny)

ui=shinyUI(fluidPage(
  selectInput("select","",choices = c(1,2),multiple = T),
  uiOutput("din_ui")
)) 

server=function(input, output,session) {
  output$din_ui=renderUI({

    input$select 

    isolate(
      lapply(X   = input$select, 
             FUN = function(i){ 
               cn <- paste0("num_",i)
               numericInput(inputId = cn,
                            label   = i,
                            value   = ifelse(!is.null(input[[cn]]), input[[cn]], ''))
             }
      )
    )
  })

}

shinyApp(ui,server)