r - 在具有多个值的单元格的列上过滤闪亮的数据表
问题描述
我希望该selectInput
字段包含提供多个行的行First_Ascent_Country
。单个单元格中包含多个国家/地区名称的行用逗号分隔。我有兴趣使用dplyr
,但对任何可行的解决方案持开放态度!
例如,如果有人UK
在First Ascent Country:
列表输入字段中选择了 ,则生成的子集应包括:
Ama Dablam,因为
first_ascent_country
包括新西兰、美国和英国。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)
解决方案
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, ','))))
)
推荐阅读
- python-3.x - 当我在 python 中使用开放 CV 从图像中检测形状时,外部边框也被识别,我该怎么做才能阻止这种情况?
- postgresql - Terraform:创建角色rds_pgaudit时出错:pq:角色“rds_pgaudit”已经存在
- android - 第一次需要权限后如何在活动中设置内容
- c# - 获取应将 2 提高到哪个数字以获取 x 的函数
- php - PHP 无法在 Web 浏览器中正确呈现
- c# - ASP.NET Core 3.1 绑定到列表
(通用列表)属性不起作用 - c++ - 是否调用函数指针来生成代码未定义的行为?
- c# - 如何使用 WPF 在画布中以随机角度和方向设置椭圆
- java - 如何按最多用户位置的顺序显示我的列表(RecyclerView)?
- node.js - 检查 MFA 是否为使用 rest api 的用户启用