Home > Back-end >  Shiny - filter a variable with %in% and a vector defined by an input
Shiny - filter a variable with %in% and a vector defined by an input

Time:09-06

Is it possible in Shiny to filter one variable of a dataframe based on an externally defined vector, which can be selected as an input?

The minimum reproducible example demonstrates the problem. Running the code, you will see that the output table is empty.

When I replace the input$region value with one of the vectors that can be selected, it works. So I guess that the problem is in how the radioButtons input or selectInput returns the vector.

I have studied the documentation for selectInput on cran which suggests that it is possible to group: "" @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 <optgroup> HTML tag) for the elements in the respective sublist. See the example section for a small demo of this feature.

The following stackoverflow QAs Dependent filter in shiny inputs and Filtering from selected input in R shiny address some issues, but they do not answer the same problem, as they filter the variable based on values extracted from the unique values of the variable itself.

Below a minimum reproducible example.

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

CodePudding user response:

Calling input$region is returning the selected object's name as a string. Use the get() function to return the values of a named object:

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