【问题标题】:Hierarchical filtering of a data set数据集的分层过滤
【发布时间】:2021-07-17 00:09:23
【问题描述】:

我想根据几个标准(主要是疾病类型和位置)过滤我的数据集。 我已经决定某些位置没有某些类型的疾病(胰腺疾病只能是一种感染 - 这就是为什么当器官 = 胰腺时选择仅制造Total 的原因 - 我知道这不现实,这只是一个假设)。所以我想做分层过滤(变量Organ在层次结构的顶部,TypeOfDisease在底部)。我正在寻找一种最短的方法(一种递归 if 语句)来计算 if 语句,而不用两个两个组合所有类别。

library(shiny)
library(tidyverse)
TypeOfDisease<-c(rep("Infection",12),rep("Cancer",5),rep("Infection",14),
                 rep("Cancer",9),rep("Infection",8),rep("Cancer",7),rep("Infection",15),rep("Cancer",0),
                 rep("Infection",12),rep("Cancer",18))
Organ<-c(rep("Oesophage",17),rep("Stomach",23),rep("Lung",15),rep("Pancreas",15),rep("Liver",30))
data<-data.frame(TypeOfDisease,Organ)
addmargins(table(data$TypeOfDisease,dataF$Organ))

ui<-fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      selectInput("Organ","Select the organ",
                  choices = c("Total",levels(data$Organ))),
      
      uiOutput("ui"),
    ),
    mainPanel(
      
      fluidRow(
        column(5,tableOutput("table")),
        column(7,
               
               fluidRow(verbatimTextOutput("matrix")),
               fluidRow(verbatimTextOutput("Nrow"))
               )
      )
      
      
      
      )
  )
  
)


server<-function(input,output){
  
  
  output$ui<-renderUI(
    
    switch (input$Organ,
            
      "Total" = selectInput("TypeOfDis","Type of disease",
                                      choices = c("Total","Infection","Cancer")),
      "Oesophage" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Stomach" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Lung" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Pancreas" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total")),
      "Liver" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
    )
  )
  
  dataFilter<-reactive({
    
    if(input$TypeOfDis=="Total"&input$Organ=="Total"){
      data
    }else if(input$TypeOfDis=="Total"&input$Organ=="Oesophage"){
        data%>%filter(Organ=="Oesophage")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Stomach"){
       data%>%filter(Organ=="Stomach")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Lung"){
      data%>%filter(Organ=="Lung")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Pancreas"){
      data%>%filter(Organ=="Pancreas")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Liver"){
      data%>%filter(Organ=="Liver")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Total"){
      data%>%filter(TypeOfDisease=="Infection")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Oesophage"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Oesophage")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Stomach"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Stomach")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Lung"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Lung")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Pancreas"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Pancreas")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Liver"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Liver")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Oesophage"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Oesophage")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Stomach"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Stomach")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Lung"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Lung")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Pancreas"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Pancreas")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Liver"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Liver")
      }else{(data%>%data%>%filter(TypeOfDisease=="Cancer"))}
        
        
  })
  output$table<-renderTable ({dataFilter()})
  output$matrix<-renderPrint({addmargins(table(data$TypeOfDisease,dataF$Organ))})
  output$Nrow<-renderPrint({nrow(dataFilter())})
  
}



shinyApp(ui,server)

【问题讨论】:

    标签: r if-statement shiny filtering


    【解决方案1】:

    或许你可以这样过滤

      dataFilter2<-reactive({
        
        if(input$TypeOfDis=="Total"&input$Organ=="Total"){
          data
        }else {
          if(input$TypeOfDis=="Total"){
            data[data$Organ==input$Organ,]
          }else data[data$TypeOfDisease==input$TypeOfDis & data$Organ==input$Organ ,]
        } 
        
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2015-02-20
      • 2011-08-25
      • 2023-03-08
      相关资源
      最近更新 更多