using substitute to get argument name with

前端 未结 2 1449
花落未央
花落未央 2020-11-29 22:49

I\'m trying to get the names of arguments in the global environment within a function. I know I can use substitute to get the name of named arguments, but I would like to b

2条回答
  •  余生分开走
    2020-11-29 23:11

    The canonical idiom here is deparse(substitute(foo)), but the ... needs slightly different processing. Here is a modification that does what you want:

    foo <- function(a, ...) {
        arg <- deparse(substitute(a))
        dots <- substitute(list(...))[-1]
        c(arg, sapply(dots, deparse))
    }
    
    x <- 1
    y <- 2
    z <- 3
    
    > foo(x,y,z)
    [1] "x" "y" "z"
    

提交回复
热议问题