Search code examples
rellipsis

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


Is it possible to remove an element from ... and pass ... onto other functions? My first two attempts failed:

parent = function(...)
{

   a = list(...)
   str(a)
   a$toRemove = NULL  
   str(a)

   # attempt 1   
   child(a)   

   # attempt 2
   child( ... = a )
}


child = function(...)
{
  a = list( ... )
  str(a)
}

parent( a = 1 , toRemove = 2 )

Edit
Sorry about the confusion. I fixed child(). The intent was to have child list the contents of ...

Edit2
Here's more of a real-world example (but still fairly simple so we can have a useful conversation about it). Parent is called via recursion. Parent need to know the depth of the recursive call. Callers outside of parent should't know about "depth" nor should they set it when calling parent(). Parent calls other functions, in this case child(). Child needs values in ... Clearly child doesn't need "depth" because parent generated it for its own use.

parent = function( ... )
{

   depth = list(...)$depth      
   if ( is.null( depth ) )
   {
       depth = 1
   }  
   print( depth )

   # parent needs value of depth to perform various calculations (not shown here)

   if ( depth == 5 )
   {
       return()
   }
   else
   {
      # child doesn't need "depth" in ...
      child( ... ) 
   }

   # yikes!  now we've added a second, third, etc. depth value to ...
   parent( depth = depth + 1 , ... )

}


child = function(...) 
{       
    # does some magic    
}

Solution

  • 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.