[英]Use character string as function argument
I'm sure this is simple, but I cannot find a solution ... I would like to use a variable containing a character string as argument for a function.我确定这很简单,但我找不到解决方案......我想使用一个包含字符串的变量作为函数的参数。
x <- c(1:10)
myoptions <- "trim=0, na.rm=FALSE"
Now, something like现在,像
foo <- mean(x, myoptions)
should be the same as应该是一样的
foo <- mean(x, trim=0, na.rm=FALSE)
Thanks in advance!提前致谢!
您可以使用eval
和parse
:
foo <- eval(parse(text = paste("mean(x,", myoptions, ")")))
A more natural way to do what you want is to use do.call
.做你想做的更自然的方法是使用
do.call
。 For example,例如,
R> l[["trim"]] = 0
R> l[["na.rm"]] = FALSE
R> l[["x"]] = 1:10
##Or l <- list(trim = 0, na.rm = FALSE, x = 1:10)
R> do.call(mean, l)
[1] 5.5
If for some reason you really want to use a myoptions
string, you could always use strsplit
to coarce it into a list form.如果由于某种原因你真的想使用一个
myoptions
字符串,你总是可以使用strsplit
将它压缩成一个列表形式。 For example,例如,
R> y = "trim=0, na.rm=FALSE"
R> strsplit(y, ", ")
[[1]]
[1] "trim=0" "na.rm=FALSE"
R> strsplit(y, ", ")[[1]][1]
[1] "trim=0"
Here's a third answer that both uses parse
, alist
and do.call
.这是第三个答案,它们都使用
parse
、 alist
和do.call
。 My motivation for this new answer, is in the case where arguments are passed interactively from a client-side as chars.我对这个新答案的动机是在参数作为字符从客户端以交互方式传递的情况下。 Then I guess, there is no good way around not using
parse
.然后我想,不使用
parse
没有什么好办法。 Suggested solution with strsplit
, cannot understand the context whether a comma ,
means next argument or next argument within an argument.建议的
strsplit
解决方案,无法理解上下文是逗号,
是指下一个参数还是参数中的下一个参数。 strsplit
does not understand context as strsplit
is not a parser. strsplit
不理解上下文,因为strsplit
不是解析器。
here arguments can be passed as "a=c(2,4), b=3,5"
or list("c(a=(2,4)","b=3","5")
这里的参数可以作为
"a=c(2,4), b=3,5"
或list("c(a=(2,4)","b=3","5")
传递
#' convert and evaluate a list of char args to a list of arguments
#'
#' @param listOfCharArgs a list of chars
#'
#' @return
#' @export
#'
#' @examples
#' myCharArgs = list('x=c(1:3,NA)',"trim=0","TRUE")
#' myArgs = callMeMaybe(myCharArgs)
#' do.call(mean,myArgs)
callMeMaybe2 = function(listOfCharArgs) {
CharArgs = unlist(listOfCharArgs)
if(is.null(CharArgs)) return(alist())
.out = eval(parse(text = paste0("alist(",
paste(parse(text=CharArgs),collapse = ","),")")))
}
myCharArgs = list('x=c(1:3,NA)',"trim=0","TRUE")
myArgs = callMeMaybe2(myCharArgs)
do.call(mean,myArgs)
[1] 2
Using all of do.call
, eval
and parse
(combining kohske's and csgillespie's answers, and also WoDoSc's answer to 'Pass a comma separated string as a list' ):使用所有
do.call
、 eval
和parse
(结合 kohske 和 csgillespie 的答案,以及WoDoSc 对“将逗号分隔的字符串作为列表传递”的回答):
x <- c(1:10)
myoptions <- "trim = 0, na.rm = FALSE"
do.call(
what = mean,
args = append(list(x = x), eval(parse(text = paste0("list(", myoptions, ")"))))
)
This solution can be quite resilient in a more complex case, such as shown below.在更复杂的情况下,此解决方案可以非常灵活,如下所示。
myfn <- function(x, y = 0, z = 0, ...) {
print(paste("x:", x))
print(paste("y:", y))
print(paste("z:", z))
if (length(list(...)) > 0) {
print("other:")
print(list(...))
}
}
myextraargs <- paste(
"y = c(11, 14), z = 47,",
"t = data.frame(p = c('apple', 'plum'), j = c(7, 2), k = c(3, 21))"
)
do.call(
what = myfn,
args = append(
list(x = 7),
eval(parse(text = paste0("list(", myextraargs, ")")))
)
)
results in:结果是:
[1] "x: 7"
[1] "y: 11" "y: 14"
[1] "z: 47"
[1] "other:"
$t
p j k
1 apple 7 3
2 plum 2 21
...and... ...和...
myextraargs <- NULL
do.call(
what = myfn,
args = append(
list(x = 7),
eval(parse(text = paste0("list(", myextraargs, ")")))
)
)
results in结果是
[1] "x: 7"
[1] "y: 0"
[1] "z: 0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.