0

Is it possible to access items from a list using a string containing the items name? Example:

myList <- list( a = list (b = rnorm(10)), c = 'more stuff')

first <- 'a'
second <- 'b'

# do something like this
x <- myList$first$second

# to acheive the same result as this
y <- myList$a$b

# this sort of works
myList$a[second]

#but I cant get it too work with two strings
myList$[first][second] 

# ive also tried using a dplyr style
myList$[first] %>% .[second]

I'm trying to access some plots I have stored in a list using strings returned from a shiny input box. If it isnt possible I'll have to think of a better way of generating the list, but I feel I'm missing something simple but hard to search for without knowing the correct terms.

Lespied
  • 322
  • 2
  • 9
  • There's a lot of info on subsetting [here](http://adv-r.had.co.nz/Subsetting.html), you can look in the `Subsetting operators` part. – NicE Feb 24 '17 at 13:54
  • 2
    You need to use `[[`. To extract the list b, use `myList[["a"]]["b"]`. To extract the second element of the vector contained in the list b, use `myList[["a"]][["b"]][2]`. Maybe [this post](http://www.stackoverflow.com/questions/36777567/is-there-a-logical-way-to-think-about-list-indexing/36815401) will be helpful in sorting this out. – lmo Feb 24 '17 at 13:59
  • Thansk for the subsetting link. I didnt know thats what it was called. I guess if I did I would have ended up on the duplicates. I think I have a much better understanding of the operations now. – Lespied Feb 24 '17 at 14:28

0 Answers0