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

后端 未结 5 1817
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 16:01

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

parent = function(...)
{

   a = list(...)
            


        
5条回答
  •  佛祖请我去吃肉
    2020-12-09 16:21

    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.

提交回复
热议问题