Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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:

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:

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)
like image 325
JHou Avatar asked Jun 30 '16 03:06

JHou


Video Answer


1 Answers

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)
like image 149
Mike H. Avatar answered Nov 15 '22 08:11

Mike H.