[R] getting/storing the name of an object passed to a function

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Feb 19 16:49:03 CET 2003


As usual Prof. Ripley has delved a little deeper into the question and
pointed out that whilst my functions might be doing/printing what I wanted
them to do, they might not do if the object was updated AFTER using
hotelling.t!  See Prof. Ripley's reply below.

This was something I wasn't even close to considering, so thank you for
pointing this out.  I'll opt for the safe option and pass the bits I
actually require between functions rather than just the name.

All the best,


-----Original Message-----
From: ripley at stats.ox.ac.uk [mailto:ripley at stats.ox.ac.uk] 
Sent: 19 February 2003 15:33
To: Gavin Simpson
Cc: 'r-help'
Subject: RE: [R] getting/storing the name of an object passed to a function

Here's a comment I wrote that has not come through yet (and there is an 
engineer working on my mail server so it may be delayed/lost):

Use  objname <- deparse(substitute(obj))

at the top of hotelling.t, and make objname part of the returned list.

Another way is to capture the call by

        Call <- match.call()

pass that on and look at it in the print function.

However, if you really want the object and not its name, it is better to
pass the object.  Otherwise I could do

fit.lda <- lda(...)
hT <- hotelling.t(fit.lda)
fit.lda <- update(fit.lda, ....)

and that would I suspect not be what you want.

More generally, get() is dangerous, and I am pretty sure you don't want to
start the get() search inside your function nor do you want to eval()
there.  There is a real danger of getting the wrong objects.

On Wed, 19 Feb 2003, Gavin Simpson wrote:

> Dear List,
> Thanks to Patrick Burns I have now have the answer to my problem.
> getting the name and storing it in a variable is done by:
> obj.nam <- deparse(substitute(obj))
> getting the object back out using its name is done by:
> get(obj.nam)
> To actually use the entity that is described by obj.nam I then wrapped
> in eval().  The final line of code now looks like this:
> print(table(predict(get(x$obj.nam))$class, eval(get(x$obj.nam)$call[[3]]),
> dnn=c("Actual", "Predicted")))
> Which does exactly what I wanted it to.
> Many thanks
> Gavin Simpson
> -----Original Message-----
> From: r-help-admin at stat.math.ethz.ch
[mailto:r-help-admin at stat.math.ethz.ch]
> On Behalf Of Gavin Simpson
> Sent: 19 February 2003 14:14
> To: 'r-help'
> Subject: [R] getting/storing the name of an object passed to a function
> Hi
> I have a couple of functions that work on the object created by another R
> command and then print out or summarise the results of this work.
> The main function is defined as:
> hotelling.t <- function(obj)
> {
> 	#internal commands
> }
> I then have print.hotelling.t() that takes the list returned by
> and prints it with some extra significance calculations, formatting, etc.
> I want to then use the named object in another calculation in
> print.hotelling.t() , that only gets done/printed if you ask for it in the
> call to print.hotelling.t()
> How do I store the name of the object obj passed to hotelling.t in the
> object returned by hotelling.t?
> And how do I "paste" the name of that object into a call to another R
> function within my print.hotelling.t()?
> Perhaps this is not the best way to do things in R?  So any comments would
> be most appreciated.
> By the way, the object obj is of class lda (Package MASS), if that
> Functions appended below.  R 1.6.2 on windows XP.
> Many Thanks
> Gavin Simpson
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list