R Shiny: Keep/retain values of reactive inputs after modifying selection

删除回忆录丶 提交于 2020-01-01 10:43:09

问题


I am trying to keep the user selected values in a dynamically generated selectInput after the user modifies a selection in another selectInput(multiple=T) option. Whenever I modify my selections, all the dynamically generated values are reset.

For example:

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

Initial Selections:

Then I want to modify my selections in the "Letters" selectInput option so that only "A" and "B" are selected (i.e., I delete "C"). After doing this, all the values generated for "A" and all the values generated for "B" (i.e., "1" for "a"; and "1" and "2" for "b") are reset.

Reset (empty) values for a and b:

How can I retain the values for a and b after deleting c (if initially selected)?

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)

回答1:


I realize this is a rather old post, but I believe this is the answer you are looking for if you have not found it yet:

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)


来源:https://stackoverflow.com/questions/38113507/r-shiny-keep-retain-values-of-reactive-inputs-after-modifying-selection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!