R Shiny - 无法根据 "selectInput" 用户选择在主面板中显示数据 table

R Shiny - Unable to display data table in mainpanel based on "selectInput" user selection

我正在尝试根据用户从下拉列表中的选择在主面板中显示 table。

例如。 “selectInput”下拉菜单的值将从 table789 开始(选项为 123、xyz、...) 如果用户选择 123,则必须显示“table123”,否则显示“table456”。

我是 shiny 的新手,尝试了下面的方法并得到了 "attempt to replicate an object of type 'closure'" 错误。我不确定我错过了什么。有人可以帮忙吗?

服务器:

    server = function(input, output, session) {
  
  output$outputtable = reactive({ifelse(input$selction == '123', DT::renderDataTable(table123), DT::renderDataTable(table456)) })
  
}

UI:

    ui <- fluidPage(
  
  sidebarPanel(selectInput("selction", "Select", choices = table789$column1, selected = "xyz")),
  
  mainPanel(
    tabsetPanel(
      tabPanel("select", DT::dataTableOutput("outputtable")),
    )
  )
)

我认为你在这里混淆了东西,你不应该将 renderDataTable 放入 reactive,这应该有效:

library(shiny)

table789 <- mtcars
table456 <- iris
table123 <- mtcars
table789$column1 <- sample(c("123","456"),nrow(table789),replace = T)


ui <- fluidPage(
    
    sidebarPanel(selectInput("selection", "Select", choices = unique(table789$column1), selected = "xyz")),
    
    mainPanel(
        tabsetPanel(
            tabPanel("select", 
                     DT::dataTableOutput("outputtable")
            )
        )
    )
)

server = function(input, output, session) {
    
    
    data <- eventReactive(input$selection,{
        if(input$selection == "123"){
            return (table123)
        }
        table456
    })
    
    output$outputtable <- DT::renderDataTable(
        data()
    )
    
}

#Run the app
shinyApp(ui = ui, server = server)