1

I have a basic Shiny app that display results (4 columns) with the possibility of filtering out some of the results.

These columns are interconnected through a hierarchy:

  • ProductCategory (furniture, food, ...)
  • ProductType (table, oranges, apples, ...)
  • ProductColor (yellow, green, ...)

The data table I'm using looks like this

Product Category --- ProductType --- ProductColor

When I select one product category, let's say fruit, I can still see in the product type selection box the instance "table". So I would want to restrict what I see in one box with what I have selected in another box. Is that possible? I'm quite new to R and Shiny and I'm not sure that I'm using the right terminology to google the issue.

library(shiny)
library(ggplot2)
library(shinydashboard)
library(DT)


ui<-fluidPage(
    titlePanel("Product catalogue"),

    # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4,
             selectInput("ProductCategory",
                         "ProductCategory:",
                         c("All",
                           unique(as.character(Prod$ProductCategory))))
      ),
      column(4,
             selectInput("ProductType",
                         "ProductType:",
                         c("All",
                           unique(as.character(Prod$ProductType))))
      ),
      column(4,
             selectInput("ProductColor",
                         "ProductColor:",
                         c("All",
                           unique(as.character(Prod$ProductColor))))
      )
    ),
    # Create a new row for the table.
    fluidRow(
      DT::dataTableOutput("table")
    ))
server<- function(input, output) {

    # Filter data based on selections
    output$table <- DT::renderDataTable(DT::datatable({
      data <- Prod
      if (input$ProductCategory!= "All") {
        data <- data[data$ProductCategory== input$ProductCategory, ]
      }
      if (input$ProductType!= "All") {
        data <- data[data$ProductType== input$ProductType,]
      }
      if (input$ProductColor!= "All") {
        data <- data[data$ProductColor == input$ProductColor,]
      }
      data
    })

Fixed integrating the previous code with this: this solution

Barbara
  • 1,118
  • 2
  • 11
  • 34

0 Answers0