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

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


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 get()
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 hotelling.t
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 matters.
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




More information about the R-help mailing list