简体   繁体   中英

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

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!

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)
  })
  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM