Home > Enterprise >  Can you connect selectInput to multiple output tables in a shiny app?
Can you connect selectInput to multiple output tables in a shiny app?

Time:11-07

I am trying to create an app where if you select one choice on the dropdown menu, one table (called data_2022) will display, and if you select a different choice on the dropdown menu, a different table (called roty_2022) will display. Here is my code:

award_types <- c("MVP", "ROTY")

library(shiny)
ui <- pageWithSidebar(
  headerPanel("NBA Awards Tracker"),
  sidebarPanel(selectInput("Awards", "Choose an Award", choices = award_types, selected = "MVP")),
  mainPanel(dataTableOutput("Table")
))
server <- function(input, output) 
  {reactive({ 
    if(input$Awards == "MVP")
    output$Table <- renderDataTable(data_2022)
  if(input$Awards == "ROTY")
    output$Table <- renderDataTable(roty_2022)})
}

Any help would be greatly appreciated!

CodePudding user response:

You could try the following. You can include a reactive expression that will return the appropriate data based on your input$Awards. Then, you only need one statement for output$Table that will render the appropriate data.

server <- function(input, output) {
  
  output$Table <- renderDataTable(my_data())
  
  my_data <- reactive({
    if(input$Awards == "MVP")
      return(data_2022)
    if(input$Awards == "ROTY")
      return(roty_2022)
  })
  
}
  • Related