简体   繁体   English

使用match.call()和mapply

[英]Using match.call() with mapply

I have a function that basically outputs a boolean condition as a string from the arguments (the details of the function don't matter here) 我有一个函数,基本上从参数输出一个布尔条件作为字符串(函数的细节在这里无关紧要)

makeClause <-function(Sex=c("NA", "male", "female"),
        SmokingHx=c("NA", "current", "former", "never"),
        conjunction=c("&", "|")) {
    arglist = as.list(match.call())
    return(arglist)
}

I have a data frame that has all combinations of input arguments, as: 我有一个包含输入参数的所有组合的数据框,如下所示:

       Sex SmokingHx conjunction
1       NA        NA           &
2     Male        NA           &
...

Which I obtain this way: 我以这种方式获得:

combinations = expand.grid(Sex=c("NA", "male", "female"), 
                           SmokingHx=c("NA", "current", "former", "never"), 
                           conjunction=c("&", "|"),
                           stringsAsFactors=FALSE)

And I call makeClause with mapply : 我呼吁makeClausemapply

mapply(makeClause, Sex=combinations$Sex,SmokingHx=combinations$SmokingHx, conjunction=combinations$conjunction)

Looking at the arglist variable I get: 看看arglist变量我得到:

$Sex
dots[[1L]][[1L]]

$SmokingHx
dots[[2L]][[1L]]

$conjunction
dots[[4L]][[1L]]

And if instead of as.list(match.call()) I call as.list(environment()) I get instead: 如果不是as.list(match.call())而是调用as.list(environment())我改为:

$Sex
[1] "male"

$SmokingHx
[1] "NA"

$conjunction
dots[[4L]][[1L]] # notice this is the only one for which I don't get the actual string

So I have two questions: 所以我有两个问题:

  1. Could you explain the R internals that lead to getting this as argument values instead of the actual string values ? 你能解释导致把它作为参数值而不是实际的字符串值的R内部吗?
  2. How can I remedy this, ie get the string values in the argument list? 我该如何解决这个问题,即在参数列表中获取字符串值?

Thanks 谢谢

Why This is Happening: 为什么会发生这种情况:

match.call captures the quoted call as a language object. match.call将引用的调用捕获为语言对象。 The dots business is what mapply is using to invoke your function, so the return value of match.call is correct. dots业务是mapply用于调用函数的函数,因此match.call的返回值是正确的。 It is just matching the call that mapply constructed for your function and returning the quoted (ie unevaluated) values. 它只是匹配为您的函数构造的mapply并返回引用(即未评估)值的调用。 Internally mapply is doing something like this (though not really since it is internal C code): 内部mapply正在做这样的事情(虽然不是因为它是内部C代码):

dots <- list(...)
call <- list()
for(j in seq_along(dots[[1]])) {
  for(i in seq_along(dots)) call[[i]] <- bquote(dots[[.(j)]][[.(i)]])
  eval(as.call(c(quote(FUN), call))))
}

If you look at as.call(c(FUN, call)) you would see something like FUN(dots[[1L]][[1L]], dots[[1L]][[2L]], dots[[1L]][[3L]]) which helps explain why you were getting the results you were getting. 如果你看as.call(c(FUN, call))你会看到类似FUN(dots[[1L]][[1L]], dots[[1L]][[2L]], dots[[1L]][[3L]]) ,这有助于解释为什么你得到你得到的结果。

How to Solve it: 如何解决它:

You seem to want the values of your argument. 你似乎想要你的论点的价值观 You could evaluate what you get from match.call , or simpler, just use: 你可以评估你从match.call得到什么,或者更简单,只需使用:

list(Sex, SmokingHx, conjunction)

If you want something that gets all the arguments of your function without having to know their names you can do something like: 如果你想要的东西可以获得你的函数的所有参数而不必知道它们的名字,你可以做类似的事情:

mget(names(formals()))

Try (simplifying fun for clarity): 尝试(为了清晰起见,简化乐趣):

makeClause <-function(Sex, SmokingHx, conjunction) mget(names(formals()))

with(combinations, t(mapply(makeClause, Sex, SmokingHx, conjunction)))

Produces: 生产:

       Sex      SmokingHx conjunction
NA     "NA"     "NA"      "&"        
male   "male"   "NA"      "&"        
female "female" "NA"      "&"        
NA     "NA"     "current" "&"        
male   "male"   "current" "&"        
female "female" "current" "&"      
... further rows omitted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM