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

后端 未结 5 1808
隐瞒了意图╮
隐瞒了意图╮ 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:19

    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.

提交回复
热议问题