0

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 Answers2

1

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]
      )
    })



 ```
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • 1
    Hi A. Suliman, Thanks so much for your response. Apologies for the delay in getting back to you. Have tried out your solution and I think it's pretty close. There's a lot of techniques in there that I haven't used before so will take some investigating of it. Basically, when I click on a chocolate bar... the filtering of the companies is correct... but I do not need the remainder of the chocolate bars to be filtered to only that company... I'm going to have some time to further investigate tommorow. Will update the post then. Thanks – Julian Tagell Mar 28 '19 at 07:07
  • Hi again, Have created a tableau public demonstrating the functionality that I'm hoping to acheive... https://public.tableau.com/profile/julian.tagell#!/vizhome/candydataexample/Sheet1?publish=yes Am still working on getting it like this... – Julian Tagell Mar 29 '19 at 03:40
0

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