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'))
})
```
My expected output
For example: If you selected the Mazda brand it should look like this.