Skip to content

Finding name of variable supplied as function argument

4 messages · ilai, (Ted Harding), Stephen Eglen

#
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:

            
...
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') }
5 = 5 	 7 = 7
g = 5 	 gs = 7
g = 5 	 gs2 = 7

HTH
Elai
#
[See in-line below]
On 25-Feb-2012 ilai wrote:
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.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 25-Feb-2012  Time: 21:06:17
This message was sent by XFMail
#
does the following help Ted?

test <- function(x) {
  actual <- deparse(substitute(x))
  paste('argument passed was called', actual)
}

test(happy)
test(apple)