【问题标题】:Why renderTable() does not giving any output in shinyApp for selectInput items?为什么 renderTable() 在 shinyApp 中没有为 selectInput 项提供任何输出?
【发布时间】:2019-11-21 12:19:25
【问题描述】:

我想在 ShinyUI 中输入选择时显示表格数据

这是我的代码:

library(shiny)
library(shinydashboard)
library(dplyr)

ui <- shinyUI(
  dashboardPage(
    dashboardHeader(),
    dashboardSidebar(disable = TRUE),
    dashboardBody(
      selectInput("filt", "Datafiltered",
                  choices = unique(comor$DiagnosisProvided),
                  multiple = TRUE),
      tableOutput("tab1")
    )
  )
)

server <- shinyServer(
  function(input, output, session) {
    output$tab1 <- renderTable({
      apl <- data %>% filter(DiagnosisProvided == input$filt)
    })
  }
)

shinyApp(ui,server)

我收到错误“结果的长度必须为 707,而不是 0” 我想上传我的数据。有上传数据的链接吗?

任何人都可以在 renderTable() 输出方面帮助我吗?

【问题讨论】:

  • 您能否提供comordata 的示例数据?尝试使用dput()

标签: r shiny


【解决方案1】:

由于您没有提供任何数据样本以使其可重现,因此我尝试根据我对 IRIS Data 的理解构建相同的数据。希望这可以解决您的问题。

UI.R

library(shiny)
library(shinydashboard)
library(shinyglide)


dashboardPage(
   dashboardHeader(title = "Basic dashboard"),
   dashboardSidebar(
      sidebarMenu(
         menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
      )
   ),
   dashboardBody(
      useShinyalert(),
      tabItems(
         tabItem(tabName = "dashboard",
                 fluidRow(
                        column(3,
                               uiOutput("filter")
                               ),
                        column(9,
                               tableOutput("tab")
                               )
                        )
                 )
      )
   )
)

服务器.R

library(shiny)
library(shinydashboard)
library(dplyr)


shinyServer(function(input,output){

####### Your data ######  

  data<-iris

 output$filter<-renderUI({
   selectInput("filter_select","Select Filter",choices = unique(data$Species))
 })

 output$tab<-renderTable({
   if(is.null(input$filter_select))
   {
     returnValue()
   }
   else
   {
     show_data<-data %>% filter(Species == input$filter_select)
     show_data
   }

 })


})

【讨论】:

  • 我的问题是:selectInput("filt", "Datafiltered",choices = unique(comor$DiagnosisProvided), multiple = TRUE)
  • 急性肠胃炎 ||发烧1 急性肠胃炎||胃炎 1 急性肠胃炎 ||发热 1 急性肠胃炎 ||癫痫发作 1 急性凝胶性肺炎 2 外伤引起的 AKI(急性肾损伤) 1 贫血 ||发热 || URTI - 急性上呼吸道感染 1 第 1 列中的文本和第 2 列中的数字。我如何仅计算贫血计数?我在一列中有 3 个值。我在 input$DiagnosisProvided 中使用此类列进行过滤。它没有被过滤。
  • @krishna 你可以为你的问题添加一个示例数据集吗?如果上述解决方案有效,我可以尝试使用您的数据集??
  • 我没有找到任何可以上传示例数据的链接。你能指导我如何上传 .csv 文件吗?
  • @krishna 无需上传..您可以在 R 中创建相同的数据集以满足需求条件并共享代码/数据集....
【解决方案2】:

错误是由data %&gt;% filter(DiagnosisProvided == input$filt)引起的,因为input$filt是一个空数组。您可以通过以下方式重现此错误:

iris %&gt;% filter(Sepal.Length == NULL) ....Result must have length 150, not 0

所以请检查您的unique(comor$DiagnosisProvided),它看起来是空的。然后用%in%代替==

data %&gt;% filter(DiagnosisProvided %in% input$filt)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2019-12-23
    • 2020-09-07
    • 2020-09-15
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多