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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With