闪亮的动态/条件过滤选择多个输入(selectizeInput,多个= TRUE)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闪亮的动态/条件过滤选择多个输入(selectizeInput,多个= TRUE)相关的知识,希望对你有一定的参考价值。

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

在此示例中,我试图显示受祖父母过滤器限制的父过滤器(任何,全部,无),然后显示受父过滤器限制的子过滤器。

。getReactiveEnvironment()$ currentContext中的错误:如果没有活动的反应性上下文,则不允许进行操作。 (您尝试做只能从反应式表达式或观察器内部完成的操作。)

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

ans

以上是关于闪亮的动态/条件过滤选择多个输入(selectizeInput,多个= TRUE)的主要内容,如果未能解决你的问题,请参考以下文章

R闪亮的动态DT数据表记住过滤器/排序

闪亮的R或两个输入过滤器

如何将条件面板设置为闪亮的选择输入?

更新多个相关的 selectizeInput() 控制通过这些输入选择过滤的反应数据集

能够选择闪亮输入中的所有潜在选项以及动态表

来自 selectInput 的具有多个条件的闪亮 R 观察事件