简体   繁体   English

无法过滤R Shiny中的数据

[英]Not able to filter the data in R Shiny

I am trying to create a dashboard using an excel file which contains multiple sheets.我正在尝试使用包含多个工作表的 excel 文件创建仪表板。 Each sheet can be selected by the user and based on the sheet selection, the data.tables of selected sheets are displayed.用户可以选择每个工作表,并根据工作表选择显示所选工作表的数据表。

One of the excel sheets contains data in this manner,其中一张 excel 工作表以这种方式包含数据,

Age年龄 Gender性别 Cancertype癌型 Rate速度
10 10 M Brain 20 20
20 20 M Colon冒号 12 12
30 30 F F Breast胸部 13 13
library(shiny)
library(shinythemes)
library(openxlsx)
library(DT)

# Define UI for application that draws a histogram
ui <-tagList(
  #shinythemes::themeSelector(),
  navbarPage(id = "home",
             title = actionLink("title","Home"),
             navbarMenu("Statistics",
                        tabPanel("Incidence",
                                 sidebarLayout(
                                   sidebarPanel(
                                     selectInput(
                                       "incidence", "Select Cancer Incidence Rates By:",
                                       choices = c("Age" = "I_Age",
                                                   "Cancer Type" = "I_Site",
                                                   "Trends" = "I_Trend",
                                                   "Trend By Age" = "I_Tr_Age",
                                                   "Admission Type" = "I_Admin",
                                                   "Stage" = "I_Stage",
                                                   "Stage By Age" = "I_St_Age",
                                                   )
                                     ),
                                      #each choices are different sheets in a workbook
                                     selectInput("cancer", "Cancer Type",
                                                 choices = 
                                                 c("All cancers", 
                                                 "All cancers ex NMSC", 
                                                 "Bladder cancer",
                                                 "Brain cancer",
                                                 "Breast cancer",
                                                 "Brain non-invasive tumours",
                                                 "Brain tumours",
                                                 ),
                                                 ),
                                     conditionalPanel(
                                       condition = "input.incidence != 'I_Tr_Age' && 
                                                    input.incidence != 'I_St_Age'",
                                       selectInput("gender","Gender",
                                                   choices = unique(table$Gender))
                                    )),
                        mainPanel(
                          tabsetPanel( id = "mp",
                            tabPanel("Plot", plotlyOutput("plot"), value = 1),
                            tabPanel("Data", DT::dataTableOutput("data"),value = 2)
                          )
                        )
                      )
                    ),
                        tabPanel("Survival"),
                        tabPanel("Mortality"),
                        tabPanel("Prevalence")
)
)
)

server <- function(input, output){
  data <- reactive({
    x <- read.xlsx("incidence.xlsx", sheet = input$incidence)
    x <- x %>% 
      filter(
        req(!is.null(Gender %in% input$gender)),
        req(!is.null(Cancertype %in% input$cancer))
      )
    x
     })
output$data <- renderDataTable({
    DT :: datatable(data())
  })
}
# Run the application 
shinyApp(ui = ui, server = server)

The above code is not working when I try to filter the data based on user selection such as gender and cancer type.当我尝试根据用户选择(例如性别和癌症类型)过滤数据时,上面的代码不起作用。 Any help would be appreciated.任何帮助,将不胜感激。

Thank you.谢谢你。

First issue with your code is that the names of the Cancertype categories in your example data are different from the labels you use in the selectInput , ie Cancertype %in% input$cancer is always FALSE .您的代码的第一个问题是示例数据中Cancertype类别的名称与您在selectInput中使用的标签不同,即Cancertype %in% input$cancer始终为FALSE Second, using req(.is.null(Gender %in% input$gender)) isn't that useful to filter your dataset.其次,使用req(.is.null(Gender %in% input$gender))对过滤数据集没有多大用处。 Not sure where you learned this kind of code.不确定您是从哪里学到这种代码的。 In general req is used to check that eg a req uired input has a value, ie to prevent that code breaks because the user hasn't made a selection yet.通常, req用于检查reqinput是否具有值,即防止由于用户尚未做出选择而导致代码中断。 To this end you could add eg req(input$gender) to the beginning of the reactive then filter your data using Gender %in% input$gender .为此,您可以将例如req(input$gender)添加到reactive的开头,然后使用Gender %in% input$gender过滤数据。

x <- data.frame(
  Age = c(10L, 20L, 30L),
  Gender = c("M", "M", "F"),
  Cancertype = c("Brain", "Colon", "Breast"),
  Rate = c(20L, 12L, 13L)
)

library(shiny)
library(shinythemes)
library(dplyr)
library(plotly)
library(DT)

# Define UI for application that draws a histogram
ui <- tagList(
  # shinythemes::themeSelector(),
  navbarPage(
    id = "home",
    title = actionLink("title", "Home"),
    navbarMenu(
      "Statistics",
      tabPanel(
        "Incidence",
        sidebarLayout(
          sidebarPanel(
            selectInput(
              "incidence", "Select Cancer Incidence Rates By:",
              choices = c(
                "Age" = "I_Age",
                "Cancer Type" = "I_Site",
                "Trends" = "I_Trend",
                "Trend By Age" = "I_Tr_Age",
                "Admission Type" = "I_Admin",
                "Stage" = "I_Stage",
                "Stage By Age" = "I_St_Age"
              )
            ),
            selectInput("cancer", "Cancer Type",
              choices = unique(x$Cancertype)
            ),
            conditionalPanel(
              condition = "input.incidence != 'I_Tr_Age' &&
                                                    input.incidence != 'I_St_Age'",
              selectInput("gender", "Gender",
                choices = unique(x$Gender)
              )
            )
          ),
          mainPanel(
            tabsetPanel(
              id = "mp",
              #tabPanel("Plot", plotlyOutput("plot"), value = 1),
              tabPanel("Data", DT::dataTableOutput("data"), value = 2)
            )
          )
        )
      ),
      tabPanel("Survival"),
      tabPanel("Mortality"),
      tabPanel("Prevalence")
    )
  )
)

server <- function(input, output) {
  data <- reactive({
    req(input$gender)
    req(input$cancer)

    x %>%
      filter(
        Gender %in% input$gender,
        Cancertype %in% input$cancer
      )
  })

  output$data <- renderDataTable({
    DT::datatable(data())
  })
}
# Run the application
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:7045

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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