Reputation: 12074
Say I have the following function in R:
tst <- function(x,y){
foo <- match.call()
print(foo)
}
tst(4,5)
This gives tst(x = 4, y = 5)
. Awesome. Now, I want to add an argument.
tst <- function(x,y){
foo <- match.call()
foo[[length(foo)+1]] <- 6
print(foo)
}
tst(4,5)
This prints tst(x = 4, y = 5, 6)
, which is great. However, I need to add an argument name so that the function knows what to do with it. For example, I want it to be tst(x = 4, y = 5, z = 6)
. I tried simply foo[[length(foo)+1]] <- "z = 6"
, but that clearly doesn't work. I've also toyed with parse
and eval
without success. Any suggestions?
Upvotes: 2
Views: 292
Reputation: 94182
You can actually treat the call like a list and give it a named argument:
> tst =
function(x,y){
foo = match.call()
foo[["this"]] ="that"
print(foo)
}
> tst(x=2,y=3)
tst(x = 2, y = 3, this = "that")
Upvotes: 3