简体   繁体   中英

Using nested lists as input to non-vectorized a function

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 (ie the output needs to be structured in the same way, as a list).

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]] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM