Populate numericInput with a per-session random value

I have a numericInput that I currently populate as such

numericInput(ns("bootstrapSeedInput"), "Random seed", value = as.integer(runif(1, min = 0, max = 1000000))

Unfortunately the UI is generated only once, which means that even if I have multiple sessions, they all get the same value, until I restart the server.

I clearly have to use updateTextInput to set it in the server function, but I am not sure on what I should observe on. What I need is a lifetime event (ui shown) but I am not sure if they are available in Shiny.

As you point out in your post the UI is defined at the server level and not per session so once a session has been initiated the UI will not re-render until the server is restarted. You can get around this by rendering the UI elements inside the server function which is run for each session, so if you are ok with using the renderUI function you can generate a "Random Seed" per session.


ui <- fluidPage(
    titlePanel("Rando Number Input"),

server <- function(input, output) {
    output$rando_number_input <- renderUI(
        numericInput("rando_number", "Random seed", value = as.integer(runif(1, min = 0, max = 1000000)))
    output$rando_number_text <- renderPrint(input$rando_number)

shinyApp(ui = ui, server = server)
1 Like

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