shiny
shiny copied to clipboard
Update input before ReactiveFlush
Hi.
I've tried creating a reprex of my original issue. Basically I would like to find out if it is possible to force the observeEvent to trigger before triggering the calculation of out$table ?
library(shiny)
ui <- fluidPage(
shiny::selectInput("nrows",
"Select nrows",
choices = 1:10),
shiny::selectInput("ncols",
"Select ncols",
choices = 1:11,
selected = 11),
tableOutput("table")
)
server <- function(input, output, session) {
observeEvent(input$ncols, {
updateSelectInput(session,
"nrows",
selected = 1)
})
output$table <- renderTable({
Sys.sleep(2)
mtcars[1:as.numeric(input$nrows), 1:as.numeric(input$ncols)]
})
}
shinyApp(ui, server)
I have a similar but more complex App where I want to update a selectInput based on another Input. However the output should still be a reactive on both.
Is this simply bad design by me? Thanks in advance
It's not entirely clear to me what your intention is, but it seems you want something similar to this?
library(shiny)
ui <- fluidPage(
shiny::selectInput("nrows",
"Select nrows",
choices = 1:10),
shiny::selectInput("ncols",
"Select ncols",
choices = 1:11,
selected = 11),
tableOutput("table")
)
server <- function(input, output, session) {
observeEvent(input$ncols, {
freezeReactiveValue(input, "nrows")
updateSelectInput(session, "nrows", selected = 1)
})
output$table <- renderTable({
mtcars[seq_len(input$nrows), seq_len(input$ncols)]
})
}
shinyApp(ui, server)
Note also, that observers like observeEvent()
do have a priority argument to make sure they execute before/after other observers/render functions, but using that is usually an indication that you may want to structure your reactive logic differently