dynamic/conditional 过滤闪亮的 select 多个输入(selectizeInput,multiple = TRUE)

dynamic/conditional filtering in shiny select multiple inputs (selectizeInput, multiple = TRUE)

multiple = TRUE 的闪亮输入 (selectizeInput) 中进行 dynamic/conditional 过滤的正确方法是什么?

在此示例中,我尝试显示受祖父过滤器限制的父过滤器(任何、所有、none),然后显示受父过滤器限制的子过滤器。

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 
=====================================

更新

这是来自@heds1 的解决方案的 flexdashboard 版本:

---
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 
=====================================

您可以使用 uiOutputselectizeInput 来完成此操作。我不知道 flexdashboard 和 shinyWidgets,但是使用 shiny 本身,你可以这样做:

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)