1

I need to load UI and server dynamically. The server function is looking for some input values from the UI, so I need to load the UI first before loading the server functions. Is there a way to know the UI rendering, including Shiny input registering is done and then do something on server?

Think about this simple example:

ui <- fluidPage(
  actionButton("a", "add UI"),
  uiOutput("ui_out")
)

loadserver <- function(input, output, session){
    if(input$n1000 > 0) print(1)
    #### many other things
}

server <- function(input, output, session) {
    flags <- reactiveValues()
    observeEvent(input$a, {
        output$ui_out <- renderUI({
            lapply(1:1000, function(i) {
                numericInput(paste0("n", i), paste0("n", i), value = 0)
            })
        })
        flags$a <- TRUE
    })
    observeEvent(flags$a, {
        req(flags$a)
        loadserver(input, output, session)
    })

}

shinyApp(ui, server)

Imagine we have a button, on click, it will render a very very large UI which takes many seconds to load. Meanwhile, on the server, we also have a flag for a, when the UI is rendering, it will become TRUE, so in another observer I can load the server corresponding to the rendered UI.

Problem: The server functions are looking for some input values from the UI we just rendered immediately after loading. However, the UI rendering is still ongoing and the expected input$n1000 is not rendered or registered in Shiny. The result is it will throw errors because input$n1000 is NULL: Warning: Error in if: argument is of length zero.

I can add req(input$n1000) in loadserver to prevent this, but imagine these functions are not written by me and I have no control over what is in there. I only know if I call the function in normal way (directly load UI and server on app start instead of renderUI), it will work, and also if we wait until the rendering is done, it will also work. So the problem how do we know if the rendering is finished?

PS: I know I can watch for some shiny js event like shiny:value but it doesn't tell me the input registration or the html/js loading is complete.

updates:

The temporary solution is provided by the Shiny team and for those of you who are curious, the link is here. I posted the link as an answer but got deleted by @Bhargav Rao for no reason. sorry but have to post the link in the question.

2
  • For the "throw errors", use shiny::req(input$n1000). It's not what you asked for, but it'll prevent cascading reactivity problems from not-yet-initialized components (without showing errors). There's also validate/need, which can serve a slightly different purpose.
    – r2evans
    Mar 25, 2021 at 3:27
  • > "I can add req(input$n1000) in loadserver to prevent this, but imagine these functions are not written by me and I have no control over what is in there." So imagine the server function comes from a package and you cannot use any req or validate.
    – lz100
    Mar 25, 2021 at 4:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.