(Ted Harding)
2012-Feb-25 19:53 UTC
[R] Finding name of variable supplied as function argument
Greetings All. I want to do the following simple thing. I have defined a function med3x3() such that, given vectors X,Y, med3x3(X,Y) returns a 3x3 table where: Row 1: X > median(X) Row 2: X = median(X) Row 3: X < median(X) Col 1: Y < median(Y) Col 2: Y = median(Y) Col 3: Y > median(Y) (with intersections of these conditions for the individual cells). I can easily define fixed rownames and colnames within the function so that the call meds3x3(G1,G2) returns a result that looks like: Y<Med Y=Med Y>Med X>Med 29 0 9 X=Med 4 0 1 X<Med 7 0 30 However, what I'd like to be able to do is pick up the names "G1" and "G2" from the function call so that the result looks like: G2<Med G2=Med G2>Med G1>Med 29 0 9 G1=Med 4 0 1 G1<Med 7 0 30 I could of course do this the tedious way by simply entering the name-strings "G1", "G2" as arguments as well as the variable names G1, G2, in a call like meds3x3(G1,G2,"G1","G2") 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 for doing so that I've found in a search of the R-help archives is in: http://finzi.psych.upenn.edu/Rhelp10/2010-May/240162.html which uses a somewhat rebarbative parsing of the call stack. Is there a simpler way? Or should I just use the 4-argument method? 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
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.
Stephen Eglen
2012-Feb-25 21:13 UTC
[R] Finding name of variable supplied as function argument
> I could of course do this the tedious way by simply entering > the name-strings "G1", "G2" as arguments as well as the variable > names G1, G2, in a call like > > meds3x3(G1,G2,"G1","G2") > > 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.does the following help Ted? test <- function(x) { actual <- deparse(substitute(x)) paste('argument passed was called', actual) } test(happy) test(apple)