Megan Halbrook
Megan Halbrook

Reputation: 85

R Shiny: using a reactive output in UI

I'm building a shiny app that will sum population counts by user-defined cut points. could be any number of cut points from 0-100.

my thought is to have the user define the number of age categories they want and then have them enter in the ranges. To do this I installed the package shinyMatrix.

Ideally, I'd like the number of rows in the matrix to be equal to the number of cuts the user defined but this requires a reactive value in the UI function.

ui <- shinyUI(fluidPage(
  sidebarLayout(sidebarPanel(
    numericInput("breaks", "Enter number of age groups", 0, min= 1, max= 100),
    matrixInput("grid", value= matrix(NA,6,2, dimnames = list(NULL, c("lower", "upper"))),
                  rows = list(extend = TRUE),
                  cols = list(names = TRUE)),
    actionButton("calc", "Calculate")
  ),
  mainPanel(
    h4("Population Sum by group"),
    dataTableOutput("table")
  )
  )
))

so where the 6 is in the matrixInput it should be something like output$breaks

app view

Upvotes: 0

Views: 59

Answers (1)

langtang
langtang

Reputation: 24722

You could use updateMatrixInput on the server side to update the "grid", setting the number of rows equal to input$breaks

  observeEvent(input$breaks, {
    updateMatrixInput(session,inputId = "grid",
                      value = matrix(NA,input$breaks,2,dimnames =list(NULL,c("lower","upper"))))
  })

Upvotes: 2

Related Questions