[R] getting arg names in function calls?
Spencer Graves
spencer.graves at structuremonitoring.com
Wed Apr 9 23:29:24 CEST 2014
Thanks very much to both Bill Dunlap and Hadley Wickham.
Per Bill's second suggestion, "match.call(eval(bo[[1]]), bo)"
returned the desired "plot(x = 0, y = 1)" [with bo <-
body(function()plot(0, 1)].
Hadley's "standardise_call(bo)" [from library(pryr)] is much
simpler.
Thanks again,
Spencer
On 4/8/2014 7:49 PM, William Dunlap wrote:
>> I don't see how that solves
>> the example I gave of extracting "plot(x=0, y=1)" from tstFn <-
>> function()plot(0, 1).
> Try
> > match.call(definition=plot, call=body(tstFn))
> plot(x = 0, y = 1)
>
> plot() is not a great example, since some of its methods do not have an
> argument called 'y' (e.g., plot.stepfun) and it has a lot of ... arguments which
> you need to go to the help file for.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: Spencer Graves [mailto:spencer.graves at structuremonitoring.com]
>> Sent: Tuesday, April 08, 2014 4:53 PM
>> To: William Dunlap; R list
>> Subject: Re: [R] getting arg names in function calls?
>>
>> 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.
>>
More information about the R-help
mailing list