1

Is it possible to take an input, and use that value to subset a named list?

More specifically, I am working with results from an API, and I want to write a generic function along the lines of

do.call("rbind", lapply(my_list, function(x) x$name_here))

In a perfect world, I would allow the end user to specify the value of name_here.

In a simpler version,

my_list <-  list(x = c(1, 2, 3), y=c("A", "B", "C", "D"))
VAR = "x"
my_list$VAR

The my_list$VAR doesn't work, but that is the general idea of what I am trying to achieve.

Thanks in advance.

Btibert3
  • 38,798
  • 44
  • 129
  • 168

1 Answers1

3

You can use [[ for extracting

my_list[[VAR]]
akrun
  • 874,273
  • 37
  • 540
  • 662