0
votes

Just trying to recreate the "Only Relevant Values" feature that some of you may know from Tableau filters... but trying to do this in an RShiny flexdashboard.

Basically, I have a table of chocolates and their companies... When I select a company I only want to see the options for the chocolates of that company. (I already know how to do this one way filtering)...

More importantly I also want the company options to react if I was to select a specific chocolate before a company... for their options to reduce accordingly.

enter image description here

Here is my code:

---
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",
    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")

```

Very appreciative of any help with this... or even advice on whether it's even possible.

2

2 Answers

0
votes

Here is one way using shiny::updateRadioButtons. Please note in order to get the full list of brands after I selected a brand, I add an All to candyData.

```{r}

candyData <- read.table(
  text = "Brand       Candy
All All
    Nestle      100Grand
    Nestle      Butterfinger
    Nestle      Crunch
    Hershey's   KitKat
    Hershey's   Reeses
    Hershey's   Mounds
    Mars        Snickers
    Mars        Twix
    Mars        M&Ms
",
  header = TRUE,
  stringsAsFactors = FALSE)

```

```{r}

  radioButtons("brand",
               "brand:",
               #choices = c("All", unique(candyData$Brand)),
               choices = unique(candyData$Brand),
               selected = "All")


  radioButtons("candy_name",
               "candy_name:",
               choices = unique(candyData$Candy),
               #choices = c("All", unique(candyData$Candy)),
               selected = "All")

   observe({
      req(input$brand)
      x <- input$brand
      if(x=='All'){
        cho1 <- candyData %>% pull(Candy) %>% unique()
      } else{
        cho1 <- filter(candyData, Brand==x) %>% pull(Candy)
      }

      # Can also set the label and select items
      updateRadioButtons(session, "candy_name",
        label = paste("Options for", x,":"),
        choices = cho1,
        selected = cho1[1]
      )
    })

  observe({
      req(input$candy_name)
      y <- input$candy_name
      if(y=='All'){
        cho2 <- unique(candyData$Brand) %>% unique()
      } else{
        cho2 <- filter(candyData, Candy==y) %>% pull(Brand) 
        cho2 <- c(cho2,'All')
      }

      # Can also set the label and select items
      updateRadioButtons(session, "brand",
        choices = cho2,
        selected = cho2[1]
      )
    })



 ```
0
votes

Oh my, this took longer than I had hoped... but I think I have finally got it close enough to what I was after. Thanks to your help A.Suliman.

ps, I added another row for the 100Grand chocolate, so that it would be under both Nestle and Mars...

Here is the full flexdashboard as a gist

https://gist.github.com/jtag04/5e79dcf94886d46a5a1d045ffe53f789