I am new to shiny and need to use renderUI to create a matrixInput with the dimension depending on the selectInput . The matrixInput will later be used in a downstream calculation, which requires the correct dimension of matrixInput. However, whenever I change the dimension in selectInput, the downstream calculation first uses the old selectInput before trying the new selectInput, producing temporary errors when the old selectInput is used. In other words, the actual workflow seems to be
selectInput -> downstream calculation -> matrixInput created from renderUI -> downstream calculation
I would like the workflow to be
selectInput -> matrixInput created from renderUI -> downstream calculation
How can I enforce this sequence of updates? Please see below for a reproducible example. Thank you for your help!
library(shiny)
ui <- fluidPage(
selectInput(
"kMax", "Number of stages",
choices = seq_len(6), selected = 2),
uiOutput("timing"),
verbatimTextOutput("text"),
)
server <- function(input, output, session) {
output$timing <- renderUI({
shinyMatrix::matrixInput(
"timing",
label = "Analysis timing",
value = matrix(seq_len(input$kMax)/as.numeric(input$kMax),
ncol=1,
dimnames = list(paste0("Look ", seq_len(input$kMax)),
"Information time")),
inputClass = "numeric",
rows = list(names=TRUE, extend=FALSE),
cols = list(names=TRUE, extend=FALSE))
})
output$text <- renderPrint({
if (length(input$timing[,1]) != input$kMax) {
stop("Incorrect length for analysis timing")
}
as.vector(input$timing[,1], "numeric")
})
}
shinyApp(ui, server)