[R] Finding name of variable supplied as function argument
(Ted Harding)
Ted.Harding at wlandres.net
Sat Feb 25 22:06:23 CET 2012
[See in-line below]
On 25-Feb-2012 ilai wrote:
> On Sat, Feb 25, 2012 at 12:53 PM, Ted Harding <Ted.Harding at wlandres.net>
> wrote:
>
>> I have defined
>> a function med3x3() such that, given vectors X,Y,
>> med3x3(X,Y) returns a 3x3 table
> ...
>> But I'd like to simply be able to pick up, within the function,
>> the names of the variables that were used as arguments in the
>> function call.
>>
>> The only suggestion ...<snip>... uses a somewhat rebarbative parsing of the
>> call stack.
>> Is there a simpler way?
>
> Is this a rhetorical question ? what could be simpler than
> deparse(substitute(y)) when setting up the colnames ? If it's not
> working for you for some reason then post your function. Or am I
> missing something with the definition of "simple" ?
Many thanks, yes it does work just as you suggest, as I now
find out when I try your suggestion. But it was not a rhetorical
question: I had been put off using that method by the discouraging
reply in the R-help message I gave the link to, which proposed
the stack-parsing method instead.
Ted.
> Or should I just use the 4-argument method?
>
> You could, but even more efficient (and better control):
>
> f <- function(x,y,rn=deparse(substitute(x)),cn=deparse(substitute(y))){
> cat(rn,'=',x,'\t',cn,'=',y,'\n') }
>> f(5,7)
> 5 = 5 7 = 7
>> g <- 5 ; gs <- 7
>> f(g,gs)
> g = 5 gs = 7
>> f(g,gs,'g','gs2')
> g = 5 gs2 = 7
>
> HTH
> Elai
>
>
>> With thanks,
>> Ted.
>>
>> -------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
>> Date: 25-Feb-2012 _Time: 19:53:49
>> This message was sent by XFMail
>>
>> ______________________________________________
>> 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.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 25-Feb-2012 Time: 21:06:17
This message was sent by XFMail
More information about the R-help
mailing list