Is there a way I can get the names of the arguments passed to a function from within a function?
Can you be a little more concrete? If you want the form of the expression given (rather than its value), deparse(substitute()) will work: fnc1 <- function(x){ deparse(substitute(x))} fnc1(3) # 3 fnc1(x) # "x" fnc1(x + 4) # "x+4" If you are passing them through the ... argument, you can coerce that to a list and use the names() attribute. If you want to reconstruct the exact call (e.g., for a modelling function), match.call() will do it. Hope this helps, Michael On Sat, Mar 24, 2012 at 5:04 PM, Ed Siefker <ebs15242 at gmail.com> wrote:> Is there a way I can get the names of the arguments passed to a > function from within a function? > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 12-03-24 5:04 PM, Ed Siefker wrote:> Is there a way I can get the names of the arguments passed to a > function from within a function?Use sys.call() to get the call, names(sys.call()) to get the names. It won't tell you how arguments were matched to formal names; for that you could use names(match.call()). Duncan Murdoch
Thanks, deparse(substitute()) does exactly what I want. On Sat, Mar 24, 2012 at 4:20 PM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:> Can you be a little more concrete? > > If you want the form of the expression given (rather than its value), > deparse(substitute()) will work: > > fnc1 <- function(x){ deparse(substitute(x))} > > fnc1(3) # 3 > > fnc1(x) # "x" > > fnc1(x + 4) # "x+4" > > If you are passing them through the ... argument, you can coerce that > to a list and use the names() attribute. > > If you want to reconstruct the exact call (e.g., for a modelling > function), match.call() will do it. > > Hope this helps, > Michael >