R Shiny: save / save reactive input values ​​after changing selection

I am trying to save user-selected values ​​in a dynamically generated selectInput after the user changes the selection in another option selectInput(multiple=T). Whenever I change my selection, all dynamically generated values ​​are reset.

For instance:

First I select "A", "B" and "C" from the "Letters" selectInput, and then select "1"; "12" ; and "1", "2", "3" from the dynamically generated input selection options generated by "A", "B" and "C".

Initial parameters:

Initial selections

"Letters" selectInput, "A" "B" (.. "C" ). , "A", , "B" (.. "1" "a" "1" "2" "b" ), reset.

Reset () a b:

Reset (empty) values ​​for a and b

a b c ( )?

library(shiny)

ui <- fluidPage(
  sidebarPanel(
    uiOutput("n1"),
    uiOutput("n2")
  ),
  mainPanel(
    textOutput("Current"),
    textOutput("Old")
  )
)

server <- function(input, output, session){

  output$n1 <- renderUI({
    selectInput("no1", "Letters", choices=c("A", "B", "C"), multiple=T)
  })

  output$n2 <- renderUI({
    if(!is.null(input$no1)){
      lst <- vector("list", length(input$no1))
      for(i in 1:length(lst)){
        lst[[i]] <- selectInput(input$no1[i], input$no1[i], choices=c(1,2,3), multiple=T)
      }
      return(lst)
    }
  })

#  observe({lk <<- reactiveValuesToList(input)})

  Values <- reactiveValues(old="start")

  session$onFlush(once=FALSE, function(){
    isolate({ Values$old<-input$A })
  })

  output$Current <- renderText({paste(input$A)})
  output$Old     <- renderText({paste(Values$old)})

#  observe({
#    updateSelectInput(session, "A", "A", choices=c(1,2,3), selected=Values$old )
#  })

}

shinyApp(ui,server)
+4
1

, , , , , :

library(shiny)

ui <- fluidPage(
  sidebarPanel(
    uiOutput("n1"),
    uiOutput("n2")
  ),
  mainPanel(
    textOutput("Current"),
    textOutput("Old")
  )
)

server <- function(input, output, session){

  output$n1 <- renderUI({
    selectInput("no1", "Letters", choices=c("A", "B", "C"), multiple=T)
  })

  output$n2 <- renderUI({
    if(!is.null(input$no1)){
      lst <- vector("list", length(input$no1))
      for(i in 1:length(lst)){
        lst[[i]] <- selectInput(input$no1[i], input$no1[i], choices=c(1,2,3), multiple=T)
      }
      return(lst)
    }
  })

  #This is the added code
  observe({
      updateSelectInput(session, "A", "A",selected=lapply(reactiveValuesToList(input), unclass)$A )
      updateSelectInput(session, "B", "B", selected=lapply(reactiveValuesToList(input), unclass)$B )
      updateSelectInput(session, "C", "C", selected=lapply(reactiveValuesToList(input), unclass)$C )
    })

}

shinyApp(ui,server)
+4

All Articles