Persistent selected values using modal selectInput


#1

I would like to create use a select input inside a modal dialog box. The problem I encountered is that the selected values are cleared each time the modal dialog opened. In other words the selected values do not persist when using selectInput inside a modal dialog box. To solve this problem I made the select input a reactive expression that depends on it's own reactive value. This works but I'm worried about creating a loop in the graph of dependencies. I'm just looking for some feedback from a more experienced Shiny programmer. Is this OK? Is there a better approach?

Thanks!

library(shiny)

ui <- fluidPage(
     actionButton("button", "Button"),
     verbatimTextOutput("selected")
     )

server <- function(input, output) {
     my_input <- reactive(selectInput("letters", "letters", selected = input$letters, choices = letters, multiple = T))
     
     observeEvent(input$button, {
               showModal(modalDialog(my_input()))
              })

     output$selected <- renderPrint(input$letters)
}

shinyApp(ui = ui, server = server)

#2

In case anyone is interested here is an example of server-side selectize inside a modal. Again I'm not sure this is the best approach but it works!

library(shiny)
library(dplyr)

baby_names <- babynames::babynames %>%
     distinct(name) %>%
     pull(name) %>%
     sort()

ui <- fluidPage(
     actionButton("show", "Show modal dialog"),
     verbatimTextOutput("selections")
)

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

     observeEvent(input$show, {
          updateSelectizeInput(session, "babyname", choices = baby_names, selected = input$babyname, server = TRUE)
          showModal(modalDialog(selectInput("babyname", "Baby Name", multiple = TRUE, choices = character(0))))
     })

     output$selections <- renderText(input$babyname)
}

shinyApp(ui, server)