Double nested updateSelectizeInput

Hi everybody! I have a (simplified) app that:

  • has a button in the "home page"
  • the button opens a modal
  • in the modal there is a selectizeInput that needs to be dynamically updated

I can't understand why the updateSelectizeInput doesn't work. What am I doing wrong? Here is the reproducible example:

library(shiny)


name_module_UI <- function(id) {
    ns <- NS(id)

    selectizeInput(
        inputId = ns("name_surname"),
        label = "Choose name and/or surname",
        choices = NULL,
        width = '200%',
        multiple = TRUE,
        options = list(create = FALSE)
    )
}

name_module_SERVER <- function(id, name_surname, surname) {
    moduleServer(id, function(input, output, session) {
        observe({
            updateSelectizeInput(
                session,
                inputId = "name_surname",
                choices = name_surname,
                selected = surname
            )
        })

    })
}

modal_module_UI <- function(id) {
    ns <- NS(id)
    name_module_UI(id = ns("test"))
}

modal_module_SERVER <- function(id) {
    moduleServer(id, function(input, output, session) {
        name_module_SERVER(
            id = "test",
            name_surname = c("Davide", "Magno"),
            surname = "Magno"
        )
    })
}

# Main UI
ui <- fluidPage(
    actionButton(inputId = "button", label = "Open Modal")
)

# Main server function
server <- function(input, output, session) {
    observeEvent(input$button, {
        showModal(
            modalDialog(
                title = "Twice nested UpdateInput",
                size = "xl",
                modal_module_UI(id = "modal_module"),
                easyClose = TRUE
            )
        )
    })

    modal_module_SERVER(id = "modal_module")
}

shinyApp(ui, server)

I think it is a timing issue because I put a browser both in the name_module_SERVER and in the name_module_UI functions. When I run shinyApp(ui, server), it:

  1. immediately stops in the name_module_SERVER
  2. then I click the button
  3. it stops in the name_module_UI function
  4. it opens the modal and nothing more happens, it doesn't enter again in the name_module_SERVER function

How can I call the name_module_SERVER after the modal has been open and the selectizeInput created?

Thanks a lot for your support!
Davide

not sure the best way to help you ... I'll start by pointing out a few issues as they occur to me and perhaps we can loop back around.
1 )

selectizeInput that needs to be dynamically updated

Did you mean that you simply want it to be populated with the params passed in at its creation i.e.

name_module_SERVER(
            id = "test",
            name_surname = c("Davide", "Magno"),
            surname = "Magno"
        ) 

because there is no other dynamic source that could influence it
2) for

observe({
           updateSelectizeInput(
})

to execute there should be some reactive() trigger within the observe body {} , theres no such thing so this code wouldnt be expected to work ; unless perhaps name_surname param was a reactive, but even if so it would need to be unwrapped i.e. name_surname()

  1. This example is not as minimal as it could be seeing as modal_module_* seems to be the thinnest possible wrapper around name_module_* . I'm thinking the dynamics might be easier to follow if you omitted modal_module all together, or is there some special purpose its intended to serve ?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.