0

I have the following dashboard showing the database mtcars and first column the brand, I have two filters that are select input. My goal is that when filtering by brand, for example if the manufacturer is Mazda then the second model filter shows me only Mazda RX4 and Mazda RX4 Wag. Any idea how to achieve it, thanks.

---
title: "mtcars dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
```

```{r}
df <- mtcars %>% tibble::rownames_to_column("model") %>%
      mutate(brand = c("Mazda","Mazda","Datsun","AMC","AMC","Plymouth","Plymouth","Mercedez","Mercedez","Mercedez",
                     "Mercedez","Mercedez","Mercedez","Mercedez","Cadillac","Lincoln MC","Chrysler","Fiat","Honda","Toyota",
                     "Toyota","Dodge","AMC","Chevrolet","Pontiac","Fiat","Porsche","Lotus","Ford","Ferrari","Maserati","Volvo")) %>%
      select("brand","model", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec","vs", "am", "gear", "carb")
```

Sidebar {.sidebar}
=====================================

Select the **car brand** and **model** you want to observe:

The filter is dynamic, if you filter by a manufacturer, only the corresponding models will be shown.

```{r}
brand_mtcars <- c("all",unique(df$brand))
model_mtcars <- c("all",unique(df$model))

selectInput(inputId = "brand", label = h4(HTML("<i class='far fa-building'></i> Select Brand")), 
            choices = brand_mtcars, selected = "all", multiple = FALSE)

selectInput(inputId = "model", label = h4(HTML("<i class='fas fa-car'></i> Select model")), 
            choices = model_mtcars, selected = "all", multiple = FALSE)
```

Table {data-orientation=rows data-icon="fa-chart-bar"}
=====================================

```{r}
result <- reactive({
  condition1 <- if(input$brand == "all"){brand_mtcars[-1]} else{input$brand}
  condition2 <- if(input$model == "all"){model_mtcars[-1]} else{input$model}
  filter(df, brand %in% condition1 & model %in% condition2)
  })


DT::renderDataTable({
  DT::datatable(result(), options = list(pageLength = 20, dom = 'tip'))
  })
```

enter image description here

My expected output

For example: If you selected the Mazda brand it should look like this.

enter image description here

Rafael Díaz
  • 2,134
  • 2
  • 16
  • 32
  • Have you seen [this link](https://stackoverflow.com/questions/21465411/r-shiny-passing-reactive-to-selectinput-choices). The top answer suggests using `renderUI` to create dynamic UI based off previously selected data. Based off the link I gave, I wonder if you will have to incorporate for shiny syntax – Daniel_j_iii Nov 12 '20 at 14:39
  • @DanielJachetta I see that this is a guide but I am not sure how to incorporate the UI and Server in a flexdashboard. – Rafael Díaz Nov 12 '20 at 15:19

0 Answers0