Re: [R] retrieve name of an object?

2010-08-04 Thread jim holtman
I am not sure what you mean by a hack?  Can you elaborate further,
give details on the problem you are trying to solve.

Does this work as a lesser hack:

 tail(as.character(quote(mtcars$cyl)),1)
[1] cyl
 tail(as.character(quote(cyl)),1)
[1] cyl



On Wed, Aug 4, 2010 at 7:47 AM, Liviu Andronic landronim...@gmail.com wrote:
 Dear all
 Is there an easier way to retrieve the name of an object? For example,
 tmp - 1:10
 as.character(quote(tmp))
 [1] tmp
 as.character(quote(mtcars$cyl))
 [1] $      mtcars cyl
 as.character(quote(mtcars$cyl))[3]
 [1] cyl

 The last call more than anything seems a hack. Is there a better way?

 Thank you
 Liviu

 __
 R-help@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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@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.


Re: [R] retrieve name of an object?

2010-08-04 Thread Henrique Dallazuanna
Tryu this:

deparse(substitute(mtcars))

On Wed, Aug 4, 2010 at 8:47 AM, Liviu Andronic landronim...@gmail.comwrote:

 Dear all
 Is there an easier way to retrieve the name of an object? For example,
  tmp - 1:10
  as.character(quote(tmp))
 [1] tmp
  as.character(quote(mtcars$cyl))
 [1] $  mtcars cyl
  as.character(quote(mtcars$cyl))[3]
 [1] cyl

 The last call more than anything seems a hack. Is there a better way?

 Thank you
 Liviu

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] retrieve name of an object?

2010-08-04 Thread Duncan Murdoch

On 04/08/2010 7:47 AM, Liviu Andronic wrote:

Dear all
Is there an easier way to retrieve the name of an object? For example, 
 tmp - 1:10

 as.character(quote(tmp))
[1] tmp
 as.character(quote(mtcars$cyl))
[1] $  mtcars cyl   
 as.character(quote(mtcars$cyl))[3]

[1] cyl

The last call more than anything seems a hack. Is there a better way? 


I don't understand what you want.  cyl is not the name of an object; 
it's the name of a component of the mtcars object.  If you know that, 
you can simply use names(mtcars) to get all the names, and 
names(mtcars)[2] to get cyl.


Perhaps you thought the result of the expression mtcars$cyl was an 
object whose name was cyl?  It's not.  Unless you assign it to 
something, it's an anonymous object.


As other have pointed out, in a function you can use substitute(arg) to 
retrieve the expression passed as arg, and deparse(substitute(arg)) to 
turn it into a string that's suitable for using as a label.  But that's 
not the name of the object.  The name of the object in that case is arg.


Duncan Murdoch

__
R-help@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.


Re: [R] retrieve name of an object?

2010-08-04 Thread Liviu Andronic
Dear Duncan

On Wed, 04 Aug 2010 08:33:49 -0400
Duncan Murdoch murdoch.dun...@gmail.com wrote:
 As other have pointed out, in a function you can use substitute(arg)
 to retrieve the expression passed as arg, and
 deparse(substitute(arg)) to turn it into a string that's suitable for
 using as a label.  But that's not the name of the object.  The name
 of the object in that case is arg.
 
Thank you for the explanation. I was indeed trying to retrieve the
expression passed as an argument in a function, to use it as a label.
Sofar the best I can manage is this:
my.fun - function(x) {
print(deparse(substitute(x)))
print(paste(substitute(x)[3]))
}

 my.fun(mtcars$cyl)
[1] mtcars$cyl
[1] cyl

Thanks all for their suggestions
Liviu

__
R-help@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.