R hierarchical drop down slicer

50 views Asked by At

I anm trying to create a dropdown slicer in R studio before linking it into Power BI.

The code I have now has the logic that I want the slicer to have. But i'm not too sure how to change the settings of it to a dropdown interface. Can anyone help, Thank you!

library(shiny)
data <- data.frame(
    ParentCode = c("A", "A", "B", "B", "C", "D", "D", "E", 'F'),
    EntityCode = c("A1", "A2", "B1", "B2", "C1", "D1", "D2", "E1", "E2")
)
 ui<-fluidPage(
     selectInput("selected_entity", "Select Entity:", choices =
                     unique(data$EntityCode), multiple = TRUE),
     verbatimTextOutput("selected_entities")
)
server <- function(input, output) {
     observe({
         filtered_data <- data[data$EntityCode %in% input$selected_entity, ]
         output$selected_entities <- renderPrint({
             paste("Selected Entities:", paste(input$selected_entity, collaspe = ", "))
             paste ("ParentCodes:", unique(filtered_data$ParentCode))
         })
     })
 }
shinyApp(ui,server)


1

There are 1 answers

1
Stéphane Laurent On

If I correctly understand, you want something like that:

enter image description here

This is obtained as follows:

  selectInput(
    "selected_entity", "Select Entity:", 
    choices = list(
      A = list("A1", "A2"),
      B = list("B1", "B2"),
      C = list("C1"),
      D = list("D1", "D2"),
      E = list("E1", "E2")
    ), 
    multiple = TRUE
  )