0

So I have a list of functions and need to call specific function in this list without using it's name. So just using a position in the list.

Adding simplificated example for the general idea.

d <- function(){print("d")}
e <- function(){print("e")}
r <- list(d, e)

Now if I wanted to call function "d" while using r[], how do I do that?

I've tried using

r[1]() 

but it just brings the "Attempt to apply non function" Error.

  • 4
    Single square brackets return a list with those indexes as elements, so `r[1]` is a list of length 1 with the d-function in it. If you use double square brackets, you get just that element out. `r[[1]]` – Brian Dec 30 '22 at 00:16
  • 1
    traktorista, the dupe link talks about vectors, lists, and frames, and with various types of objects within those lists; I don't think it talks about a list of _functions_, but it doesn't really matter: indexing a list is agnostic to the class of the object being extracted. If after reading the dupe link you still cannot figure it out, please @ping me and we'll sort it out. Good luck! – r2evans Dec 30 '22 at 00:22

0 Answers0