在SHILY中,是否可以基于外部定义的向量过滤数据帧的一个变量,该向量可以 Select 作为输入?

最小可重复性的例子说明了这个问题.运行代码,您将看到输出表为空.

当我将输入的$Region值替换为可以 Select 的一个向量时,它起作用了.所以我猜问题出在RadiButton输入或electInput返回向量的方式上.

我研究了在cran上 Select Input的文档,该文档建议可以分组:"" @param choices List of values to select from. If elements of the list are named, then that name --- rather than the value --- is displayed to the user. It's also possible to group related inputs by providing a named list whose elements are (either named or unnamed) lists, vectors, or factors. In this case, the outermost names will be used as the group labels (leveraging the 100 HTML tag) for the elements in the respective sublist. See the example section for a small demo of this feature.

下面的堆栈溢出QAS Dependent filter in shiny inputsFiltering from selected input in R shiny解决了一些问题,但它们没有回答相同的问题,因为它们根据从变量本身的唯一值中提取的值来过滤变量.

低于最小可重现性的例子.

#library(dplyr) 
#library(shiny)


#Sample data

Country <- c("Finland", "Estonia", "Kuwait", "Germany", "Italy", "Belgium", "Ukraine", "Belgium", "Italy", "Italy", "Belarus", "Turkey", "Italy", "Switzerland", "Turkey", "France", "Turkey", "Denmark", "Latvia", "United Arab Emirates", "Spain")
Organisation <- c("FIN",  "LOT", "KAC", "EZY", "RYR", "BEL", "MSI", "RYR", "AZA", "RYR", "BRU", "THY", "AZA", "EZS", "THY", "TVF", "PGT", "SAS", "BTI", "UAE", "RYR")
Operations <- c(10, 2, 1, 50, 32, 18, 12, 63, 41, 1, 13, 15, 28, 33, 14, 8, 11, 52, 27, 2, 19)

test_data <- data.frame(Country, Organisation, Operations)

Europe <- c("Finland", "Estonia", "Germany", "Italy", "Belgium", "Belarus", "Turkey", "Switzerland", "Denmark", "Latvia", "Spain")
EU <- c("Finland", "Estonia", "Germany", "Italy", "Belgium", "Denmark", "Latvia", "Spain")


#Shiny

# UI
ui <- fluidPage(
      radioButtons("region", "Select Departure Region", 
                     choices = c("Europe", "EU")),
#  selectInput("region", "Select a Departure Region", 
#              list('Europe' = list("Finland", "Estonia", "Germany", "Italy", "Belgium", "Belarus", "Turkey", "Switzerland", "Denmark", "Latvia", "Spain"),
#                      'EU'= list("Finland", "Estonia", "Germany", "Italy", "Belgium", "Denmark", "Latvia", "Spain"))
#                      ),   #Is the problem that the vector is not correctly defined when selected? However, even creating a list does not result in the same output, as a normal dplyr %in% filter
       tableOutput("Market_Table")
    )

server <- function(input, output) {

#Does not work; though works if I use the Vector in place of the input variable  
#  filtered_data <- reactive({
#    test_data[test_data$Country %in% input$region, ]  .
#  })

  filtered_data <- reactive({
    test_data %>% filter(Country %in% input$region)  #Should some sort of reference to .env be used? .env$input$region however does not work.
  })
  
  
  
#suggested in Mastering Shiny chapter 12.2.1 but also does not correctly filter - nothing appears in the table
    
#  filtered_data <- reactive({
#    test_data[test_data$Country %in% input$region, ]
#  })
  
output$Market_Table <- renderTable({filtered_data()})
  
}

# Run the application 
shinyApp(ui = ui, server = server)

推荐答案

调用input$region将以字符串形式返回所选对象的名称.使用get()函数返回命名对象的值:

...
server <- function(input, output) {
  filtered_data <- reactive({
    test_data %>% filter(Country %in% get(input$region))  
  })
...

R相关问答推荐

使用facet_wrap()时如何将面板标题转换为脚注?

从多个前置日期中获取最长日期

按R中的组查找相邻列的行累积和的最大值

ggplot geom_smooth()用于线性回归虚拟变量-没有回归线

警告:lmdif:info = 0. nls. lm()函数的输入参数不正确

如何在emmeans中计算连续变量的对比度

如何在R中对深度嵌套的tibbles中的非空连续行求和?

如何在ggplot中标记qqplot上的点?

如何使用ggplot对堆叠条形图进行嵌套排序?

线性模型斜率在减少原始数据时提供NA

将重复项转换为NA

当我们有多个反斜杠和/特殊字符时使用Gsubing

基于R中的间隔扩展数据集行

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

将多个列合并为一个列的有效方法是什么?

将列的值乘以在不同数据集中找到的值

如何在使用Alpha时让geom_curve在箭头中显示恒定透明度

网络抓取新闻标题和时间

访问数据帧中未定义的列时出现R错误

如何移动点以使它们的打印不重叠