Skip to content

Instantly share code, notes, and snippets.

@jtag04
Created March 29, 2019 06:04
Show Gist options
  • Save jtag04/5e79dcf94886d46a5a1d045ffe53f789 to your computer and use it in GitHub Desktop.
Save jtag04/5e79dcf94886d46a5a1d045ffe53f789 to your computer and use it in GitHub Desktop.
mutually dynamic filters for R Shiny Flexdashboard
---
title: "reactive test"
output:
flexdashboard::flex_dashboard
runtime: shiny
---
```{r}
library(tidyverse)
```
```{r}
candyData <- read.table(
text = "Brand Candy
Nestle 100Grand
Nestle Butterfinger
Nestle Crunch
Hershey's KitKat
Hershey's Reeses
Hershey's Mounds
Mars Snickers
Mars Twix
Mars M&Ms
Mars 100Grand
",
header = TRUE,
stringsAsFactors = FALSE)
```
Sidebar {.sidebar}
---
```{r}
radioButtons("brand",
"brand:",
choices = c("All", unique(candyData$Brand)),
selected = "All")
radioButtons("candy_name",
"candy_name:",
choices = c("All", unique(candyData$Candy)),
selected = "All")
# this controls the brand options
observe({
req(input$brand)
req(input$candy_name)
brand_selection <- input$brand
candy_selection <- input$candy_name
candy_rank_value <- 1
if(brand_selection == 'All' & candy_selection == "All"){
cho1 <- c("All", candyData %>% pull(Candy) %>% unique())
} else { if (brand_selection != 'All' & candy_selection == "All") {
cho1 <- c("All", candyData %>% filter(Brand == brand_selection) %>% pull(Candy))
} else {
# if (brand_selection == 'All' & candy_selection != "All") {
cho1 <- c("All", candy_selection)
candy_rank_value <- 2
# }
}}
updateRadioButtons(session,
"candy_name",
"candy:",
choices = cho1,
selected = cho1[candy_rank_value])
})
observe({
req(input$candy_name)
req(input$brand)
candy_selection <- input$candy_name
brand_selection <- input$brand
brand_rank_value <- 1
if(candy_selection == 'All' & brand_selection == "All"){
cho2 <- c("All", candyData %>% pull(Brand) %>% unique())
} else { if (candy_selection != 'All' & brand_selection == "All") {
cho2 <- c("All", candyData %>% filter(Candy == candy_selection) %>% pull(Brand))
} else {
# if (candy_selection == 'All' & brand_selection != "All") {
cho2 <- c("All", brand_selection)
brand_rank_value <- 2
# }
}}
updateRadioButtons(session,
"brand",
"brand:",
choices = cho2,
selected = cho2[brand_rank_value])
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment