[R] getting arg names in function calls?

Spencer Graves spencer.graves at structuremonitoring.com
Wed Apr 9 01:52:48 CEST 2014


Hi, Bill:


       Thanks for the reply.  Unfortunately, I don't see how that solves 
the example I gave of extracting "plot(x=0, y=1)" from tstFn <- 
function()plot(0, 1).


       As I noted, body(tstFn) returns "plot(0, 1)" as an "language" 
object of class "call".  My challenge is to convert that into something 
similar, with explicit names for the arguments.


       Any thoughts?
       Spencer


On 4/7/2014 3:18 PM, William Dunlap wrote:
> 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.


-- 
Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567
web:  www.structuremonitoring.com




More information about the R-help mailing list