首页 > 解决方案 > (R/SHINY) - 使用 selectInput 选择要显示的(哪个)反应表

问题描述

所以我有一个简单的问题。

  1. 我有一个选择输入。让我们称它为 report_select,并带有“报告 1”和“报告 2”选项可供选择。

  2. 另外,我有一个 textInput (用于指定员工 ID)

  3. 我有两个反应器(我们称它们为 r_one 和 r_two)。这些导致基于员工的单独数据。

  4. 我有一个输出 $table1

目标:我希望下拉菜单控制两个反应性报告中的哪一个显示在表 1 中。

注 1:表格单独工作正常。我可以毫无问题地一个接一个地显示。注 2:我使用 SHINY 以及 tabsetpanel 和 tabpanel 进行组织

有什么方法可以帮助解决这个问题吗?

方法 1:(错误是“不允许从 shionyoutput 对象读取对象)

library(shiny)
library(readxl)
library(dplyr)
library(tidyr)

# Globally Pull In Files
new_compdump_data <- read_xlsx("C:/temp/FILE.xlsx")

#Format Imported Data
clean_new <- subset(new_compdump_data,is.na(new_compdump_data$Term_Date))
clean_new$AIN <- as.numeric(clean_new$AIN)
clean_new$`MIP value` <- clean_new$`MIP%`*clean_new$Salary
# Begin UI
ui <- fluidPage(
titlePanel("Data Tool"),

sidebarLayout(
sidebarPanel(
  selectInput(inputId = "report_select",
              label = "Select a Report",
              choices = c("Base Info","Stock Info")),
  textInput("stock_ain","AIN")
),  #End SideBarPanel             

mainPanel(
  tabsetPanel(
    tabPanel(title="Base Info",tableOutput("table1")
  )))
))

#======= SHINY - Server Section

server <- function(input, output) {

report1 <- reactive({
subset(clean_new[c(5,1,2,3)],clean_new$AIN==input$AIN)
})

report2 <- reactive({
subset(clean_new[c(5,6,7,8)],clean_new$AIN==input$AIN)
})

report_choose <- reactive({
ifelse(input$report_select=="Base Info",report1(),
ifelse(input$report_select=="Stock Info",report2()))
})

output$table1({
report_choose()
})

} #End server info

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

方法2:错误:同上

与上面相同,但对 report_choose 的反应是:

report_choose <- reactive{(
switch(input$report_select,
"Base Info"=report1(),
"Stock Info"=report2())
)}

方法 3:(错误是“不允许从 shionyoutput 对象读取对象)

与上述相同(部分),但

report_choose <- reactive({
if(input$report_select=="Base Info") {
report1()
} else {
report2()
}
})

还是没有骰子。有什么想法或方法吗?

标签: rshiny

解决方案


推荐阅读