首页 > 解决方案 > 在具有多个值的单元格的列上过滤闪亮的数据表

问题描述

我希望该selectInput字段包含提供多个行的行First_Ascent_Country。单个单元格中包含多个国家/地区名称的行用逗号分隔。我有兴趣使用dplyr,但对任何可行的解决方案持开放态度!

例如,如果有人UKFirst Ascent Country:列表输入字段中选择了 ,则生成的子集应包括:

  1. Ama Dablam,因为first_ascent_country包括新西兰、美国和英国。

  2. Amphu Gyabjen 因为first_ascent_country只包括英国。

样本数据

peaks_stackoverflow <- structure(list(peak_id = c("AMAD", "AMPG", "ANN1", "ANN2", "ANN3", 
"ANN4"), peak_name = c("Ama Dablam", "Amphu Gyabjen", "Annapurna I", 
"Annapurna II", "Annapurna III", "Annapurna IV"), peak_alternative_name = c("Amai Dablang", 
NA, NA, NA, NA, NA), height_metres = c(6814, 5630, 8091, 7937, 
7555, 7525), climbing_status = c("Climbed", "Climbed", "Climbed", 
"Climbed", "Climbed", "Climbed"), first_ascent_year = c(1961, 
1953, 1950, 1960, 1961, 1955), first_ascent_country = c("New Zealand, USA, UK", 
"UK", "France", "UK, Nepal", "India", "W Germany"), first_ascent_expedition_id = c("AMAD61101", 
"AMPG53101", "ANN150101", "ANN260101", "ANN361101", "ANN455101"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(peak_id = structure(list(), class = c("collector_character", 
"collector")), peak_name = structure(list(), class = c("collector_character", 
"collector")), peak_alternative_name = structure(list(), class = c("collector_character", 
"collector")), height_metres = structure(list(), class = c("collector_double", 
"collector")), climbing_status = structure(list(), class = c("collector_character", 
"collector")), first_ascent_year = structure(list(), class = c("collector_double", 
"collector")), first_ascent_country = structure(list(), class = c("collector_character", 
"collector")), first_ascent_expedition_id = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

闪亮的代码

country_names <- c("New Zealand", "USA", "UK", NA, "France", "Nepal",
"India", "W Germany", "Spain", "Japan", "Slovenia", "Austria", 
"Switzerland", "Norway", "Romania", "US", "Canada", "S Korea", 
"Netherlands", "Germany", "Poland", "Inida", "Argentina", 
"Yugoslavia", "Russia", "Ukraine", "Denmark", "Swiss", "Australia", 
"China", "Taiwan", "Singapore", "Italy", "Ireland", "Belgium", 
"Brazil", "Netherands", "Georgia", "Portugal")

library(DT)
library(dplyr)

ui <- fluidPage(
    titlePanel("Himalayan Climbing Expeditions - Peaks"),
    
    fluidPage(
        titlePanel("Basic DataTable"),
    
        fluidRow(
            column(4,
                   selectInput("first" ,
                               label = "First Ascent Country:",
                               choices = c("All", country_names),
                               selected = "All",
                               multiple = TRUE)))
                                        
            ),

        DT::dataTableOutput("table")
)

server <- function(input, output) {
    
    # Filter data based on selections
    output$table <- renderDataTable(datatable({
        data <- peaks_stackoverflow
        
        if (input$first != "All") {
            data <- data[data$first_ascent_country == input$first,]
        }
        data
    }
    ))
    
}
shinyApp(ui, server)

标签: rdplyrshiny

解决方案


stringr您可以使用您的矢量str_detect来识别与国家/地区的任何匹配项。input$first使用str_c您可以将所有input$first字符串连接到一个字符串搜索模式中。

由于您使用multiple = TRUE,因此如果“全部”包含在添加的其他特定国家/地区,则不清楚您可能想要什么结果。一种可能性:如果您检查any输入的选择是否不是“全部”,则可以进一步子集化。

library(stringr) 

server <- function(input, output) {
  
  # Filter data based on selections
  output$table <- renderDataTable(datatable({
    req(input$first)
    data <- peaks_stackoverflow
    if (any(input$first != "All")) {
      data <- data %>%
        dplyr::filter(str_detect(first_ascent_country, 
                                 str_c(input$first, collapse = '|')))
    }
    data
  }))
  
}

或者,使用基本 R,您可以使用以下方法子集和匹配字符串grepl

data <- subset(data, grepl(paste0(input$first, collapse = '|'), first_ascent_country))

最后,您可以根据您的数据创建按字母顺序排列的国家/地区列表(以防需要或考虑):

country_names <- sort(
  unique(trimws(unlist(strsplit(peaks_stackoverflow$first_ascent_country, ','))))
)

推荐阅读