1

首先为这个有点缺乏信息的标题道歉

我有一个闪亮的应用程序,用户下载许多可能的数据集之一,并且对于某些列可以执行过滤器以生成 data.frame 输出

无论下载的数据集如何,我都想标准化代码

问题是列名因数据集而异,并且我希望过滤的列数不定

就创建输入而言,我已经使用 tidyeval 方法调整了这个解决方案。但是,我在输出时遇到了困难,而不必根据可以过滤的列数诉诸大量 if else 语句

这是一个基于数据集的(非闪亮)示例,其中我有 2 个可过滤列,一个始终需要的值列和最终输出中不需要的一列

library(tidyverse)

## desired columns 
my_cols <- c("col 1", "another col")

# selected input
input_1 <- c("A","B")
input_2 <- c("Z")

l <- list(`col 1` = rep(c("A","B","C"),times=3), `another col` = 
rep(c("X","Y","Z"),each=3), Value = c(1:9),`Unwanted 1`=(9:1))

df <- as_tibble(l)

# this creates the right number of correctly-named columns
for (i in seq_along(my_cols)) {
assign(paste0("col_", i), sym(my_cols[i]))
}

## This produces output but wish to adapt
## to varying number of columns

df %>%
filter(!!col_1 %in% input_1) %>%
filter(!!col_2 %in% input_2) %>%
select(!!col_1, !!col_2, Value)

#  `col 1` `another col` Value
#  <chr>   <chr>         <int>
# 1 A       Z                 7
# 2 B       Z                 8

所以这是我希望适应的最后一段代码,以考虑 my_cols 的可变长度

TIA

4

1 回答 1

0

您似乎将输入存储在单独的变量中,并建议您预先知道将操作多少列(除非这些列来自动态生成的 UI)。无论如何,我建议您也将输入保存在一个对象中(希望长度与,否则您可以对输入列表进行子集化以匹配向量my_cols的长度)。my_cols然后你可以准备一个 quosures 列表并将它们拼接到filterandselect中。

library(tidyverse)
library(rlang)

## desired columns 
my_cols <- c("col 1", "another col")

# selected input
input_1 <- c("A","B")
input_2 <- c("Z")
input_3 <- NULL # ui handle that is not used for this dataset

l <- list(`col 1` = rep(c("A","B","C"),times=3), `another col` = 
            rep(c("X","Y","Z"),each=3), Value = c(1:9),`Unwanted 1`=(9:1))

df <- as_tibble(l)

# make a list of inputs
l_input <- list(input_1, input_2, input_3)[seq_along(my_cols)]

# make a list of expression quosures. Make sure you use enquos if inside function 
l_expr <- mapply(function(x,y) quos(!!sym(x) %in% !!y), my_cols, l_input, USE.NAMES = F)
# splice into filter and select
df %>% filter(!!!l_expr) %>%  select(!!!syms(my_cols), Value)

如果你把它放在一个函数中,记得使用enquos()

于 2018-07-05T23:43:41.703 回答