1

I need to use nested elements in a list for a query. For example:

sha_list <- list(c("ed76504696469470dcbf", "164f798524fd6904236a"),
c("669dfeccad88cd4348f7", "af70a76691aacf05c1bb"))

This is a list which has the following structure:

[[1]]
[1] "ed76504696469470dcbf" "164f798524fd6904236a"

[[2]]
[1] "669dfeccad88cd4348f7" "af70a76691aacf05c1bb"

Here's what I've tried so far:

library("devtools")
lapply(sha_list, source_gist)

this gives:

Error: length(id) == 1 is not TRUE

The expected output would be a list with two elements in each of the two entries of the list:

[[1]]
[1] gist1
[2] gist2

[[2]]
[1] gist3
[2] gist4

What I need to do here is to iterate across this list to execute a function (which is non-vectorized, so it can only take one of the elements as an argument at a time). I was hoping that this would work, since I want to recursively use each item in each list:

rapply(sha_list, source_gist, how = "list")

This doesn't work either.

unlist() will not work, because I need to maintain the structure of the list (i.e. the output needs to be structured in the same way, as a list).

8
  • Try lapply(unlist(sha_list), source_gist) Commented Jul 24, 2015 at 20:03
  • 1
    Or lapply(sha_list, function(x) lapply(x, source_gist)) to maintain the structure of the list Commented Jul 24, 2015 at 20:07
  • @choff - The structure of the list will remain unchanged in my call, so long as you do not re-assign unlist(sha_list) to sha_list Commented Jul 24, 2015 at 20:09
  • @RichardScriven But it will change the length of the list to 4, no? Commented Jul 24, 2015 at 20:11
  • @histelheim, what output are you expecting? You are sourcing gists here, which may become complicated in the manor you are describing. I would suggest one gist in each element of a four element list Commented Jul 24, 2015 at 20:14

1 Answer 1

2

You can do:

library("devtools")
sha_list <- list(c("ed76504696469470dcbf", "164f798524fd6904236a"),
                 c("669dfeccad88cd4348f7", "af70a76691aacf05c1bb"))
my.gists <- lapply(sha_list, function(x) lapply(x, source_gist))

Then, for example, the gist associated with sha_list[[1]][2] can be accessed with my.gists[[1]][[2]].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.