match.call with default arguments

前端 未结 4 550
萌比男神i
萌比男神i 2020-12-14 18:32

As part of a function, I want to output a list of all the arguments and their values, including the default values. For example, a function with these arguments:

<         


        
4条回答
  •  囚心锁ツ
    2020-12-14 19:31

    Here is an attempt to wrap this logic in a reusable function to drop in instead of match.call:

    match.call.defaults <- function(...) {
      call <- evalq(match.call(expand.dots = FALSE), parent.frame(1))
      formals <- evalq(formals(), parent.frame(1))
    
      for(i in setdiff(names(formals), names(call)))
        call[i] <- list( formals[[i]] )
    
    
      match.call(sys.function(sys.parent()), call)
    }
    

    It looks like it works:

    foo <- function(x=NULL,y=NULL,z=2,...) {
      match.call.defaults()
    }
    
    
    > foo(nugan='hand', x=4)
    foo(x = 4, y = NULL, z = 2, ... = pairlist(nugan = "hand"))
    

提交回复
热议问题