Persistent selected values using modal selectInput

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?



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

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

     output$selected <- renderPrint(input$letters)

shinyApp(ui = ui, server = server)

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!


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

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

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)