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
:
mapply(makeClause, Sex=combinations$Sex,SmokingHx=combinations$SmokingHx, conjunction=combinations$conjunction)
Looking at the arglist
variable I get:
$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:
$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:
- Could you explain the R internals that lead to getting this as argument values instead of the actual string values ?
- How can I remedy this, i.e. get the string values in the argument list?
Thanks