[R] Returning the name of an object passed directly or from a list by lapply

andrewH ahoerner at rprogress.org
Thu Sep 15 23:11:10 CEST 2011


Dear folks:

Let’s suppose I want a function to print return the name of the object
passed to it. 

> myname <- function(object) {out<-deparse(substitute(object)); out}

This works fine on a single object:
> O1 <-c(1:4)
> myname(O1)
[1] "O1"

However it does not work if you use lapply to pass it the same object from a
list:
> O2 <-c(1:4)
> object.list <- list(O1,O2)
> lapply(object.list, myname)
[[1]]
[1] "X[[1L]]"

[[2]]
[1] "X[[2L]]"

Is there any way to write myname() so that it returns the same objects name
regardless of whether it is handed the name directly or by lapply as an
element of a list?

Any help you can offer would be greatly appreciated.

Warmly, andrewH


--
View this message in context: http://r.789695.n4.nabble.com/Returning-the-name-of-an-object-passed-directly-or-from-a-list-by-lapply-tp3816798p3816798.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list