R Shiny updating UI server side

Viewed 256

I have a large shiny app with many modules. Some of these modules have a pickerInput UI-element, which are filled with data on the server-side (using updatePickerInput), because they are too large. My problem is, that a lot of calculations are done BEFORE the updatePickerInput has a chance to fill the values, often with NULL values which cause some output elements to appear with error. Using validate/need or req in every output element seems not very efficient, producing a lot of boilerplate code. So I wanted to know if there is a way to tell the application to only run subsequent calculations if the update calls on the server are finished.

In the (very simplified) example below you can see, that the updatePickerInput-function is executed after the renderText (in the first run). What I would like to have is, that the update takes place before the first rendering.

require(shiny)
require(shinyWidgets)

randText <- c(sample(LETTERS, 100, replace = T), NULL)

ui <- fluidPage(
  pickerInput("test", "Choose me", choices = NULL, selected = NULL, multiple = T),
  textOutput("randText")
)

server <- function(input, output, session) {
  updatePickerInput(session, "test", choices = randText, selected = randText[1:50])
  output$randText <- renderText({
    validate(need(!is.null(input$test), "MOOOOORE!"))
    Sys.sleep(3)
    paste0(input$test)
  })
}

shinyApp(ui, server)
0 Answers
Related