Seydou GORO
Seydou GORO

Reputation: 1285

Hierarchical filtering of a data set

I want to filter my dataset based on several criteria (mainly disease type and location). I have decided that there are no certain types of disease for certain locations (pancreatic disease can only be an infection - This is why a made onlyTotal for choice when Organ=Pancreas -. I know that's not realistic, it's just a hypothesis). So I want to do hierarchical filtering (The variable Organ is at the top of the hierarchy, and TypeOfDisease at the bottom). I'm looking for the shortest way (A kind of recursive if-statement) to compute the if statements without combining all the categories two by two.

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)

Upvotes: 0

Views: 197

Answers (1)

YBS
YBS

Reputation: 21349

Perhaps you can filter like this

  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 ,]
    } 
    
  })

Upvotes: 1

Related Questions