闪亮的应用程序只呈现四个列表中的前两个表

Shiny app only renders first two tables from list of four

我正在组装我的第一个 Shiny 应用程序,需要一些帮助。我试图让用户能够通过下拉列表 selection.

在同一数据集上选择不同的视图

在一个单独的 R 脚本中,我创建了我的汇总表,我找到了脚本,运行它在我的应用程序的最开始。

这些是我想在应用程序中使用的汇总表:

按机构汇总

按呼叫上下文汇总

SummarybyLicense

按位置汇总

我还创建了一个向量,其中包含下拉列表中的选项:

Views <- c("Agency", "Context", "License", "Location")

这是应用程序 UI 部分中的代码:

  box(
                    title = "Minute Consumption and Charges", 
                    selectInput("View", "Select view", choices = Views)),
                    dataTableOutput("TableView")
      )

这是在服务器端:

server <- function(input, output, session) {

  reactive_View <- reactive({
    if(input$View == "Agency") {return (SummarybyAgency)} else {
      if (input$View == "Context") {return(SummarybyCallContext)} else {
        if (input$view == "License") {return(SummarybyLicense)} else {
          if (input$View == "Location") {return(SummarybyLocation)}
        }
      }
    }
    
  })  
 
  
  output$TableView <- renderDataTable(reactive_View(), options = list(pageLength = 5))   

  }

shinyApp(ui, server)

}

但是,当我 运行 应用程序时,我只能 select 前两个表,其他两个失败。

示例:

[

这是控制台中的错误:

Warning: Error in if: argument is of length zero
  114: <reactive:reactive_View> [app.R#116]
   98: reactive_View
   97: renderDataTable
   96: func
   83: renderFunc
   82: output$TableView
    1: runApp

但是,如果我在嵌套 if 语句中交换表的顺序,我将始终看到前两个表(如果我将“许可证”和“位置:放在前两个 ifs 中,我将能够渲染它们,但不渲染 Agency 和 Context。

我的代码有问题吗?

也许这样的事情对你有帮助?

library(shiny)
library(shinydashboard)
Views <- c("Agency", "Context", "License", "Location")
ds <- list(
  Agency = data.frame("agency" = c(1,2,3), "b" = c(4,5,6)),
  Context = data.frame("context" = c(4,5,6), "b" = c(4,5,6)),
  License = data.frame("license" = c(7,8,9), "b" = c(4,5,6)),
  Location = data.frame("location" = c(10,11,12), "b" = c(4,5,6))
)


ui <- fluidPage(
  box(
    title = "Minute Consumption and Charges", 
    selectInput("View", "Select view", choices = Views)),
  dataTableOutput("TableView")
)

server <- function(input, output, session) {
  output$TableView = renderDataTable(
    ds[[input$View]]
  )
}