0

Code to reproduce the problem:

data(iris)
L=list(data=iris)
print(deparse(substitute(L[[1]])))
[1] "L[[1]]"

I want the result to be "iris" instead of "L[[1]]", is there a way?

ChaoYang
  • 837
  • 2
  • 8
  • 14
  • 1
    I don't see a problem here. `substitute` behaves as expected; it is neither a time machine nor a mind reading device. Once you have created your list like this, there is no way to know that its first component was once called "iris". If `substitute` would behave like you want, then with `b <- 1; a <- b; deparse(substitute(b))` would return `1` instead of `b` which would be pretty catastrophic. – lebatsnok Feb 02 '14 at 07:45

1 Answers1

2
L <- list(data=as.name("iris"))
L$data

And to actually retrieve the data:

eval(L$data)

But what you should be doing, rather than playing around with eval and deparse, is storing the name of the dataset along with its contents:

L <- list(iris=iris)
names(L)
L
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • see also http://stackoverflow.com/questions/16951080/can-list-objects-be-created-in-r-that-name-themselves-based-on-input-object-name/16951524#16951524 – Ben Bolker Feb 02 '14 at 05:37