Мы можем передать выражение в кавычках в 'extract' и eval
uate для фильтрации строк
library(tidyverse)
foo <- function(d, per, extract, ...){ ## Add a new argument called `extract`
extract <- rlang::enexpr(extract)
out <- data.frame(d, ...)
h <- split(out, rep(seq_along(per), per))
map(h, ~ .x %>%
filter(!! extract))
}
foo(d = 2:4, per = 1:2, extract = age == 2, age = 1:3, prof = c("med", "low", "med"))
#$`1`
#[1] d age prof
#<0 rows> (or 0-length row.names)
#$`2`
# d age prof
#1 3 2 low
Или используя base R
foo <- function(d, per, extract, ...){ ## Add a new argument called `extract`
extract <- substitute(extract)
out <- data.frame(d, ...)
h <- split(out, rep(seq_along(per), per))
lapply(h, function(x) subset(x, subset = eval(extract)))
}
foo(d = 2:4, per = 1:2, extract = age == 2, age = 1:3, prof = c("med", "low", "med"))