What's the right approach to have dynamic/conditional filtering in shiny inputs (selectizeInput
) where multiple = TRUE
?
In this example I'm trying to show parent filters limited by the grandparent filter (any, all, none), and then child filters limited by the parent filter.
Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r}
library(tidyverse)
library(flexdashboard)
library(shinyWidgets)
```
```{r global, include=FALSE}
filter_df <- data.frame(filter_GP = c("gpA", "gpA", "gpA", "gpB", "gpB", "gpB"),
filter_P = c("p1", "p1", "p2", "p3", "p4", "p4"),
filter_C = c("g", "h", "i", "j", "k", "l"))
data_df <- data.frame(y = seq(from=1, to=12),
filter_C = c("g", "g", "h", "h", "i", "i", "j", "j", "k", "k", "l", "l"))
data_df <-
data_df %>%
left_join(filter_df, by="filter_C")
```
Sidebar {.sidebar}
=====================================
```{r}
# grandparents
selectizeInput("GP", label = "Grandparents (multi-select):",
choices = unique(filter_df$filter_GP),
multiple = TRUE,
selected = "")
# parents
selectizeInput("P", label = "Parents (multi-select):",
choices = unique(filter_df[filter_df$filter_GP %in% input$GP, "filter_P"]),
multiple = TRUE,
selected = "")
# children
selectizeInput("C", 'Children (multi-select):',
choices = unique(filter_df[filter_df$filter_P %in% input$P, "filter_C"]),
multiple = TRUE,
selected = "")
```
Test
=====================================
Update
Here is the flexdashboard version of the solution from @heds1:
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r}
library(tidyverse)
library(flexdashboard)
library(shinyWidgets)
```
```{r global, include=FALSE}
filter_df <- data.frame(filter_GP = c("gpA", "gpA", "gpA", "gpB", "gpB", "gpB"),
filter_P = c("p1", "p1", "p2", "p3", "p4", "p4"),
filter_C = c("g", "h", "i", "j", "k", "l"))
data_df <- data.frame(y = seq(from=1, to=12),
filter_C = c("g", "g", "h", "h", "i", "i", "j", "j", "k", "k", "l", "l"))
data_df <-
data_df %>%
left_join(filter_df, by="filter_C")
```
Sidebar {.sidebar}
=====================================
```{r}
# grandparents
selectizeInput("GP", label = "Grandparents (multi-select):",
choices = unique(filter_df$filter_GP),
multiple = TRUE,
selected = "")
# parents
output$P <- renderUI({
df <- data_df[data_df$filter_GP %in% input$GP, ]
selectizeInput(
'P', 'Parents', choices = unique(df$filter_P), multiple = TRUE
)
})
uiOutput("P")
# children
output$C <- renderUI({
df <- data_df[data_df$filter_GP %in% input$GP, ]
df <- df[df$filter_P %in% input$P, ]
selectizeInput(
'C', 'Children', choices = unique(df$filter_C), multiple = TRUE
)
})
uiOutput("C")
```
Test
=====================================
You can do this using uiOutput
with selectizeInput
. I don't know about flexdashboard and shinyWidgets, but using shiny itself, you can do it this way:
library(shiny)
library(tidyverse)
filter_df <- data.frame(filter_GP = c("gpA", "gpA", "gpA", "gpB", "gpB", "gpB"),
filter_P = c("p1", "p1", "p2", "p3", "p4", "p4"),
filter_C = c("g", "h", "i", "j", "k", "l"))
data_df <- data.frame(y = seq(from=1, to=12),
filter_C = c("g", "g", "h", "h", "i", "i", "j", "j", "k", "k", "l", "l"))
data_df <-
data_df %>%
left_join(filter_df, by="filter_C")
ui <- {
fluidPage(
# grandparents
selectizeInput("GP", label = "Grandparents (multi-select):",
choices = unique(filter_df$filter_GP),
multiple = TRUE,
selected = ""),
uiOutput("P"),
uiOutput("C")
)
}
server <- function(input, output, session) {
output$P <- renderUI({
df <- data_df[data_df$filter_GP %in% input$GP, ]
selectizeInput(
'P', 'Parents', choices = unique(df$filter_P), multiple = TRUE
)
})
output$C <- renderUI({
df <- data_df[data_df$filter_GP %in% input$GP, ]
df <- df[df$filter_P %in% input$P, ]
selectizeInput(
'C', 'Children', choices = unique(df$filter_C), multiple = TRUE
)
})
}
shinyApp(ui, server)