在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 inputs和Filtering 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)