捕获...传递给函数而不会破坏 !!操作员
Capture ... passed to function without mangling the !! operator
我需要将 ...
转发到函数参数,提取符号作为代码,并将它们转换为字符,同时保留名称。我通常为此使用 match.call(expand.dots = FALSE)$...
,但它破坏了 !!
运算符。
当前行为
f <- function(...){
match.call(expand.dots = FALSE)$...
}
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## !(!my_variable)
期望的行为
f <- function(...){
# whatever works
}
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## !!my_variable
编辑 更好:
f <- function(...){
# whatever works
}
my_variable <- "my value"
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## [1] "my_value"
以下代码似乎有效。用例是 here。感谢 MrFlick 将我推向正确的方向。
f <- function(...){
rlang::exprs(...)
}
my_variable <- "my value"
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## [1] "my_value"
编辑:顺便说一句,我仍在寻找一种无需评估即可解析 !!
的方法。这将增强与 https://github.com/ropensci/drake/issues/200.
相关的用户端功能
我需要将 ...
转发到函数参数,提取符号作为代码,并将它们转换为字符,同时保留名称。我通常为此使用 match.call(expand.dots = FALSE)$...
,但它破坏了 !!
运算符。
当前行为
f <- function(...){
match.call(expand.dots = FALSE)$...
}
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## !(!my_variable)
期望的行为
f <- function(...){
# whatever works
}
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## !!my_variable
编辑 更好:
f <- function(...){
# whatever works
}
my_variable <- "my value"
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## [1] "my_value"
以下代码似乎有效。用例是 here。感谢 MrFlick 将我推向正确的方向。
f <- function(...){
rlang::exprs(...)
}
my_variable <- "my value"
f(a = 1234, b = !!my_variable)
## $a
## [1] 1234
##
## $b
## [1] "my_value"
编辑:顺便说一句,我仍在寻找一种无需评估即可解析 !!
的方法。这将增强与 https://github.com/ropensci/drake/issues/200.