0

In the following example I have two static radio buttons representing the mtcars and iris datasets. Upon making a selection, the user is presented with a second set of buttons based on data in each dataset. For the mtcars dataset, the user can filter by selecting from the unique list of carburetors or in the case of the iris dataset, the species. Now, I require another set of buttons based on the carb/species buttons to further filter the data. Say, for the mtcars dataset the list of unique gear selections associated with the carburetor selection and for the Iris the unique set of petal lengths. Given the real world application of what I'm trying to accomplish, there is no getting away from requiring a third set of reactive radio buttons. I just have no clue how to approach the next step.

ui.R

library(shinydashboard)

ui <- dashboardPage(
        dashboardHeader(title = "My DFS Dashboard"),
        dashboardSidebar(
                sidebarMenu(
                        menuItem("MTCARS", tabName = "dashboard", icon = icon("dashboard")),
                        menuItem("IRIS", tabName = "widgets", icon = icon("th"))
                )
        ),
        dashboardBody(
                fluidRow (
                        column(width = 3,
                               box(title = "Select Dataset", width = NULL, status = "primary", background = "aqua",
                                   radioButtons ("mydataset",
                                                 "",
                                                 inline = TRUE,
                                                 c("mtcars", "iris"),
                                                 selected = "mtcars"))),
                        column(width = 3, 
                               box(title="Select Filter One", width = NULL, status = "primary", background = "aqua",
                                   uiOutput("filter1"))),
                        column(width = 3, 
                               box(title = "Select Fitler Two", width = NULL, status = "primary", background = "aqua",
                                   uiOutput("filter2")))
                        )
                    )        
                )

server.R

library(tidyverse)


server <- function(input, output, session) {

data("mtcars")
data("iris")

cars <- mtcars
flowers <- iris

carbs <- cars %>%
        dplyr::select(carb)

carbs <- carbs$carb
carbs <- as.data.frame(carbs)
carbs <- unique(carbs$carb)

spec <- flowers %>%
                dplyr::select(Species)
spec <- unique(spec$Species)

               
        vards <- reactive ({
                switch(input$mydataset,
                       "mtcars" = carbs,
                       "iris" = spec,
                         )
        })
        output$filter1 <- renderUI({
                radioButtons("fil1","", choices=vards())
        })

}

1 Answers1

0

Perhaps this may be helpful. You can add another reactive expression to filter your dataset and obtain choices for the third set of radio buttons. I included isolate so that the third set of buttons does not react to changes in the dataset (only changes in the second radio buttons, which is dependent already on the dataset). Please let me know if this is what you had in mind for behavior.

server <- function(input, output, session) {
  
  data("mtcars")
  data("iris")
  
  cars <- mtcars
  flowers <- iris
  
  vards1 <- reactive({
    switch(input$mydataset,
           "mtcars" = unique(cars$carb),
           "iris" = unique(flowers$Species),
    )
  })
  
  vards2 <- reactive({
    req(input$fil1)
    if (isolate(input$mydataset) == "mtcars") {
      cars %>%
        filter(carb == input$fil1) %>%
        pull(gear) %>%
        unique()
    } else {
      flowers %>%
        filter(Species == input$fil1) %>%
        pull(Petal.Length) %>%
        unique()
    }
  })
  
  output$filter1 <- renderUI({
    radioButtons("fil1","", choices=vards1())
  })
  
  output$filter2 <- renderUI({
    radioButtons("fil2","", choices=vards2())
  })
  
}
Ben
  • 28,684
  • 5
  • 23
  • 45