将函数调用向量折叠成一行,用 & 分隔每个调用,使用替代和减少?
Collapse a vector of function calls in to one line, separating each call with &, using substitute and Reduce?
我正在尝试构造函数调用 str_detect(words, "a") & str_detect(words, "e") & str_detect(words, "i") & str_detect(words, "o") & str_detect(words, "u")
而无需进行所有那些痛苦的输入。我知道这不是解决问题的好方法,但在 doing a poor job of solving it a better way 之后,我决定尝试这样做,看看是否能从中学到什么。
我的尝试如下:
library(stringr)
funList <- sapply(c("a", "e", "i", "o", "u"), function(y) substitute(str_detect(words, x), list(x=y)))
Reduce(function(a,b) paste(a, "&", b), funList)
这几乎奏效了。 funList
正是我们所期望的:
> funList
$a
str_detect(words, "a")
$e
str_detect(words, "e")
$i
str_detect(words, "i")
$o
str_detect(words, "o")
$u
str_detect(words, "u")
但是最后一行给出了一些非常意外的输出,大概是由于 R 构造函数调用的方式:
> Reduce(function(a,b) paste(a, "&", b), funList)
[1] "str_detect & str_detect & str_detect & str_detect & str_detect"
[2] "words & words & words & words & words"
[3] "a & e & i & o & u"
能否修复此问题以提供预期的函数调用?我尝试了一些技巧,例如抛出 quote
函数,但我没有取得任何成就。
从 funList
你可以通过 -
实现预期的输出
paste(funList, collapse = ' & ')
#[1] "str_detect(words, \"a\") & str_detect(words, \"e\") & str_detect(words, \"i\") & str_detect(words, \"o\") & str_detect(words, \"u\")"
但是,您不需要 sapply
来构造 funList
,因为您可以这样做 -
paste0(sprintf('str_detect(words, "%s")', c("a", "e", "i", "o", "u")), collapse = ' & ')
#[1] "str_detect(words, \"a\") & str_detect(words, \"e\") & str_detect(words, \"i\") & str_detect(words, \"o\") & str_detect(words, \"u\")"
这是一个非常糟糕的通用方法。您可能也不需要这样做。
但是,如果您继续使用该语言进行计算并利用运算符实际上被解析为函数,这将非常容易:
funList <- lapply(c("a", "e", "i", "o", "u"), function(y) substitute(str_detect(words, x), list(x=y)))
Reduce(function(a, b) substitute(`&`(a, b), list(a = a, b = b)), funList)
#str_detect(words, "a") & str_detect(words, "e") & str_detect(words,
# "i") & str_detect(words, "o") & str_detect(words, "u")
我们可以使用str_c
library(stringr)
str_c(funList, collapse = ' & ')
我正在尝试构造函数调用 str_detect(words, "a") & str_detect(words, "e") & str_detect(words, "i") & str_detect(words, "o") & str_detect(words, "u")
而无需进行所有那些痛苦的输入。我知道这不是解决问题的好方法,但在 doing a poor job of solving it a better way 之后,我决定尝试这样做,看看是否能从中学到什么。
我的尝试如下:
library(stringr)
funList <- sapply(c("a", "e", "i", "o", "u"), function(y) substitute(str_detect(words, x), list(x=y)))
Reduce(function(a,b) paste(a, "&", b), funList)
这几乎奏效了。 funList
正是我们所期望的:
> funList
$a
str_detect(words, "a")
$e
str_detect(words, "e")
$i
str_detect(words, "i")
$o
str_detect(words, "o")
$u
str_detect(words, "u")
但是最后一行给出了一些非常意外的输出,大概是由于 R 构造函数调用的方式:
> Reduce(function(a,b) paste(a, "&", b), funList)
[1] "str_detect & str_detect & str_detect & str_detect & str_detect"
[2] "words & words & words & words & words"
[3] "a & e & i & o & u"
能否修复此问题以提供预期的函数调用?我尝试了一些技巧,例如抛出 quote
函数,但我没有取得任何成就。
从 funList
你可以通过 -
paste(funList, collapse = ' & ')
#[1] "str_detect(words, \"a\") & str_detect(words, \"e\") & str_detect(words, \"i\") & str_detect(words, \"o\") & str_detect(words, \"u\")"
但是,您不需要 sapply
来构造 funList
,因为您可以这样做 -
paste0(sprintf('str_detect(words, "%s")', c("a", "e", "i", "o", "u")), collapse = ' & ')
#[1] "str_detect(words, \"a\") & str_detect(words, \"e\") & str_detect(words, \"i\") & str_detect(words, \"o\") & str_detect(words, \"u\")"
这是一个非常糟糕的通用方法。您可能也不需要这样做。
但是,如果您继续使用该语言进行计算并利用运算符实际上被解析为函数,这将非常容易:
funList <- lapply(c("a", "e", "i", "o", "u"), function(y) substitute(str_detect(words, x), list(x=y)))
Reduce(function(a, b) substitute(`&`(a, b), list(a = a, b = b)), funList)
#str_detect(words, "a") & str_detect(words, "e") & str_detect(words,
# "i") & str_detect(words, "o") & str_detect(words, "u")
我们可以使用str_c
library(stringr)
str_c(funList, collapse = ' & ')