[R] Finding name of variable supplied as function argument

ilai keren at math.montana.edu
Sat Feb 25 21:42:42 CET 2012


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" ?

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.



More information about the R-help mailing list