Multiple conditional filter Rshiny

shiny

#1

This post is a follow up on a previous post which can be found here.

I want to build an app where the user can

  1. Upload his data set
  2. Filter it based on specific columns (assuming that the column names of all given data sets are always the same)
  3. Apply further filters, but possible filter options depend on what has been selected in 2.
  4. Perform some calculations in another tab (at a later stage) <-- have not tried this yet.

So far I have the following:

UI:

# Define UI for data upload app ----
 ui <- fluidPage(
   
   # App title ----
   titlePanel(title = h1("EMP Analysis Application", align = "center")),
   
   # Sidebar layout with input and output definitions ----
   sidebarLayout(
     
     # Sidebar panel for inputs ----
     sidebarPanel(
       
       # Input: Select a file ----
       fileInput("uploaded_file", "Choose CSV File",
                 multiple = TRUE,
                 accept = c("text/csv",
                            "text/comma-separated-values,text/plain",
                            ".csv")),
       
       # Horizontal line ----
       tags$hr(),
       
       # Input: Checkbox if file has header ----
       checkboxInput("header", "Header", TRUE),
       
       # Input: Select separator ----
       radioButtons("sep", "Separator",
                    choices = c(Semicolon = ";",
                                Comma = ",",
                                Tab = "\t"),
                    selected = ","),
       
       
       # Horizontal line ----
       tags$hr(),
       
       # Input: Select number of rows to display ----
       radioButtons("disp", "Display",
                    choices = c(All = "all",
                                Head = "head"),
                    selected = "all"),
       

       # Select variables to display ----
       uiOutput("regions"),
       uiOutput("sectorlist")
       
     ),
     
     # Main panel for displaying outputs ----
     mainPanel(
       
       tabsetPanel(
         id = "dataset",
         tabPanel("Data Explorer", div(style = "overflow-x: scroll",dataTableOutput("rendered_file")))
       )
     )
     
   )
 )

Server:



# Define server logic to read selected file ----
server <- function(input, output, session) {
  
  # Read file ----
  df <- reactive({
    req(input$uploaded_file)
    read.csv(input$uploaded_file$datapath,
             header = input$header,
             sep = input$sep)  
    
  })
  
  output$regions <- renderUI({
    
    regionlist <- sort(unique(as.vector(df()$region)), decreasing = FALSE)
    regionlist <- append(regionlist, "All", 0)
    
    selectizeInput(inputId = "select_region", 
                   label = "Select region of interest", 
                   choices = regionlist,
                   multiple = T, 
                   options = list(placeholder = 'Select the region of interest'))
    
    # selectizeInput(inputId = "select_region", 
    #                label = "Select region of interest", 
    #                choices = unique(df()$region),
    #                multiple = T, 
    #                options = list(placeholder = 'Select the region of interest'))
  })
  
  output$sectorlist <- renderUI({
    
    sectorlist <- sort(unique(as.vector(df()$sector)), decreasing = FALSE)
    sectorlist <- append(sectorlist, "All", 0)
    
    selectizeInput(inputId = "select_sector", 
                   label = "Select sector of interest", 
                   choices = sectorlist,
                   multiple = T, 
                   options = list(placeholder = 'Select the sector of interest'))
    
  })
  
  # Select columns and regions to print ----
  df_sel <- reactive({
    req(input$select_region)
    req(input$select_sector)
    # df_sel <- df()[df()$region == input$select_region,]
    
    if(input$select_region == "All") {
      
      filt1 <- quote(region != "@?><")
      
      
    } else {
      
      filt1 <- quote(region == input$select_region) 
      
    }
    
    
    if (input$select_sector == "All") {
      
      filt2 <- quote(sector != "@?><")
      
      
    } else {
      
      filt2 <- quote(sector == input$select_sector)
      
    }
    
    
    
    raw %>%
      filter_(filt1) %>%
      filter_(filt2)
    
  
  })

  # Print data table ----  
  
  output$rendered_file <- DT::renderDataTable({
    if(input$disp == "head") {
      head(df_sel())
    }
    else {
      df_sel()
    }
  })
  
  
}


How to select variables using data table