Can I remove an element in ... (dot-dot-dot) and pass it on? Can I remove an element in ... (dot-dot-dot) and pass it on? r r

Can I remove an element in ... (dot-dot-dot) and pass it on?


One way to manipulate these things is to wrap the child function inside parent, and use a definition that puts any arguments you don't want passing on to child after the ... argument. For example:

parent <- function(...) {    localChild <- function(..., toRemove) child(...)    localChild(...)}child <- function(a) {    a + 10}> parent(a = 1, toRemove = 10)[1] 11

Another way is to use do.call():

parent2 <- function(...) {    a <- list(...)    a$toRemove <- NULL    do.call(child2, a)}child2 <- function(b) {    b + 10}> parent2(b = 1, toRemove = 10)[1] 11

Depending on your actual use case, the do.call() is perhaps closest to what you intended with your Question.


Your child function is erroneous. Try

> child(a=1)Error in str(a) : object 'a' not found

edit : no longer applicable.

The ... argument should only be used to pass parameters to a next function. You cannot get the parameters from there that easily, unless you convert them to a list. So your child function could be :

child <- function(...){  mc <- match.call()  # or mc <- list(...)  str(mc$a)}

Which doesn't make sense. You can't know whether the user specified a or not. The correct way would be to include a as an argument in your function. the ... is to pass arguments to the next one :

child <- function(a, ...){    str(a,...)}

Then you could do :

parent <- function(...){   mc <- match.call()   mc$toRemove <- NULL   mc[[1L]] <- as.name("child")   eval(mc)}

or use the list(...) and do.call() construct @Gavin proposed. The benefit of match.call() is that you can include non-dot arguments as well. This allows your parent function to specify defaults for the child :

parent <- function(a=3, ...){    ... (see above)}


Here's an example of how to get the items out of ... and remove an element and then I call the next function with do.call:

parent <- function(...){   funArgs <-  list(...)   str(funArgs)   ## remove the second item   newArgs <- funArgs[-2]   str(newArgs)   ## if you want to call another function, use do.call   do.call(child, newArgs)  }child = function(...){  cat("Don't call me a child, buddy!\n")  a <- list(...)  str(a)}parent(a=1, b=2, c=3)

If you need to add more items to your arguments, as opposed to removing arguments, keep in mind that do.call likes named lists where the names are the argument names and the list values are the argument values. It's in the help file, but I struggled with that a bit before finally figuring it out.