[R] getting arg names in function calls?

William Dunlap wdunlap at tibco.com
Tue Apr 8 00:18:43 CEST 2014


Look at match.call().  E.g.,
  > f <- function(x, y = log2(x), ...) match.call()
  > f()
  f()
  > f(1, x=2, anotherArg=3, 4)
  f(x = 2, y = 1, anotherArg = 3, 4)
or, using its 'definition' and 'call' arguments directly
  > match.call(function(x, y, ...)NULL, quote(foo(1, x=2, extraArg=3, 4)))
  foo(x = 2, y = 1, extraArg = 3, 4)
  > match.call(function(x, y, ...)NULL, quote(foo(1, x=2, extraArg=3, 4)), expand.dots=FALSE)
  foo(x = 2, y = 1, ... = list(extraArg = 3, 4))

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Spencer Graves
> Sent: Monday, April 07, 2014 3:05 PM
> To: R list
> Subject: [R] getting arg names in function calls?
> 
>        How can I convert "plot(0, 1)" into "plot(x=0, y=1)"?
> 
> 
>        More generally, how can I get argument names assigned to function
> calls in "language" objects?
> 
> 
>        Example:
> 
> 
> tstFn <- function()plot(0, 1)
> bo <- body(tstFn)
> 
> 
> tstFnxy <- function()plot(x=0, y=1)
> boxy <- body(tstFnxy)
> 
> 
>        Is there a function that will modify "bo" to match "boxy"?
> 
> 
>        My current solution requires me to know the names of the
> arguments for "plot" (in this example).  I'd prefer a more general
> solution.
> 
> 
>        Thanks,
>        Spencer
> 
> 
> p.s.  I'm trying to create an animation by repeatedly calling a function
> that contains something like text(0, 1, "abc").  By computing on the
> language object 'text(0, 1, "abc")', I can call text(0, 1, 'a') the
> first time, text(0, 1, 'ab') the second, and text(0, 1, 'abc') the
> third.  The function will be more general if I can get the names of the
> arguments as just described.
> 
> ______________________________________________
> 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.




More information about the R-help mailing list