基于列值的过滤器值(在Shiny reactive中)

xytpbqjk  于 2023-03-10  发布在  React
关注(0)|答案(1)|浏览(89)

我正在做一个调查数据集,我有来自不同国家(第1列)的人的答案(第2列)。
| 第1栏|第2栏|
| - ------|- ------|
| 中国|答案A|
| 中国|答案B|
| 法国|答案A|
| 法国|答案C|
| 意大利|答案B|
我的闪亮应用程序(https://aurelienmaignant.shinyapps.io/shiny/)显示曲线,并允许用户按国家过滤(React图)。
我想创建一个图,其中只显示col 2中至少被所有选定国家(col 1)共享一次的值。
例如,如果用户在shiny中选择“中国”和“法国,”则绘图输出应仅显示“答案A”(因为答案A至少由来自法国的某人回答一次,并且至少由来自中国的某人回答一次).
如果shiny中的用户选择“中国”、“法国”和“意大利,”则输出应该是“无,”因为三个答案(A、B或C)中没有一个被来自每个国家的人至少共享一次.
谢谢你的帮忙

v9tzhpje

v9tzhpje1#

欢迎来到SO社区,下次请发一个minimal reproducible example,这样其他人就可以更好地帮助你!
我已经做了一个迷你应用程序,为你做的工作:

### Loading the libraries

library(tidyverse)
library(shiny)

### Building your sample data frame

df <- data.frame("col1" = c("china", "china", "france", "france", "italy"),
                 "col2" = c("A", "B", "A", "C", "B"))

### The user interface part

ui <- fluidPage(fluidRow(
  column(4,
         ### The widget to get the input from user
         selectInput("select", label = "Select box", 
                     choices = unique(df$col1),
                     multiple = TRUE)),
  column(4,
         ### The output to show the mutual options
         textOutput("result")
  ))
)

### The server part

server <- function(input, output) {

  output$result <- renderText({
    tmp <- df %>% filter(col1 %in% input$select)
    tmp <- tmp[with(tmp, col2 == Reduce(intersect, split(col2, col1))),]
    unique(tmp$col2)
  })
  
}

### Running the app

shinyApp(ui = ui, server = server)

在服务器端,我根据用户选择的国家筛选数据集,然后使用intersect(),我们可以根据第一列找到第二列中的共同值。关于这一行的更多细节,您可以查看此SO post here,了解它是如何完成的。

相关问题