简体   繁体   中英

Passing indeces as arguments in R

I am trying to extract values from a large number of lists such as the one below (which has thousands of first-level entries just like that one). The lists are all the same in their first-level structure, but the amount/shape of information to be extracted varies for the level below.

library(tidyverse)

split_extract <- list(structure(c("1 Introduction ", "2 Intermediaries and technological innovation systems ", 
                                    "2.1 Intermediaries’ support for (eco)-innovation ", "2.2 Technological innovation systems (TIS) ", 
                                    "2.3 Linking functions thinking from TIS to intermediaries’ support roles ", 
                                    "3 The analytical approach ", "3.1 Step 1: defining the study focus ", 
                                    "3.2 Step 2: identify intermediaries in the context ", "3.3 Step 3: mapping roles of intermediaries ", 
                                    "3.4 Step 4: assessing the potential roles of intermediaries in eco-innovation ", 
                                    "3.5 Step 5: recommendations for intermediaries and their key stakeholders ", 
                                    "4 Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia ", 
                                    "4.1 Step 1 – define the study focus ", "4.2 Step 2 – identify intermediaries in the context ", 
                                    "4.3 Step 3 – map roles of the roles of intermediaries in eco-innovation ", 
                                    "4.4 Step 4 – assess the roles of intermediaries ", "5 Discussion ", 
                                    "6 Conclusions and further research ", NA, NA, ".1", ".2", ".3", 
                                    NA, ".1", ".2", ".3", ".4", ".5", NA, ".1", ".2", ".3", ".4", 
                                    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                    NA, NA, NA, NA, "Introduction ", "Intermediaries and technological innovation systems ", 
                                    "Intermediaries’ support for (eco)-innovation ", "Technological innovation systems (TIS) ", 
                                    "Linking functions thinking from TIS to intermediaries’ support roles ", 
                                    "The analytical approach ", "Step 1: defining the study focus ", 
                                    "Step 2: identify intermediaries in the context ", "Step 3: mapping roles of intermediaries ", 
                                    "Step 4: assessing the potential roles of intermediaries in eco-innovation ", 
                                    "Step 5: recommendations for intermediaries and their key stakeholders ", 
                                    "Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia ", 
                                    "Step 1 – define the study focus ", "Step 2 – identify intermediaries in the context ", 
                                    "Step 3 – map roles of the roles of intermediaries in eco-innovation ", 
                                    "Step 4 – assess the roles of intermediaries ", "Discussion ", 
                                    "Conclusions and further research ", NA, NA, NA, NA, NA, NA, 
                                    "Step", "Step", "Step", "Step", "Step", NA, "Step", "Step", "Step", 
                                    "Step", NA, NA), .Dim = c(18L, 5L)))

I have created a short function to which I'd like to pass index as an argument, either as a simple integer (eg: 1 ), which can easily be plugged in, or as a string in the form "i,j" for later evaluation inside the call. Often this may be something like ",1" as in this case.

lext <- function(list, index) {

  if(typeof(index) == "character") {index <- rlang::parse_expr(index)}

  map(1:length(list), ~list[[.x]][rlang::eval_bare(index)])

}

l <- lext(split_extract, index = ",1")

However, I get the error below, which suggests to me that the parsing route I've taken is wrong... but I don't know how else to do this.

#> Error in parse(text = x): <text>:1:1: unexpected ','
#> 1: ,
#>     ^

Any help would be much appreciated!

Using ... works with lapply , but not with purrr ...

lext <- function(list, ...) {
  lapply(seq_along(list), function(x) list[[x]][...])
}
lext(split_extracted, , 1)

Or simply

lext <- function(list, ...) {
  lapply(list, function(x) x[...])
}

How about this?

lext <- function(list, index) {
  if(typeof(index) == "character") {index <- rlang::parse_expr(sprintf(".x[%s]", index))}
  map(list, ~rlang::eval_bare(index))
}

l <- lext(split_extract, index = ",1")

[[1]]
 [1] "1 Introduction "                                                                                                                         
 [2] "2 Intermediaries and technological innovation systems "                                                                                  
 [3] "2.1 Intermediaries’ support for (eco)-innovation "                                                                                       
 [4] "2.2 Technological innovation systems (TIS) "                                                                                             
 [5] "2.3 Linking functions thinking from TIS to intermediaries’ support roles "                                                               
 [6] "3 The analytical approach "                                                                                                              
 [7] "3.1 Step 1: defining the study focus "                                                                                                   
 [8] "3.2 Step 2: identify intermediaries in the context "                                                                                     
 [9] "3.3 Step 3: mapping roles of intermediaries "                                                                                            
[10] "3.4 Step 4: assessing the potential roles of intermediaries in eco-innovation "                                                          
[11] "3.5 Step 5: recommendations for intermediaries and their key stakeholders "                                                              
[12] "4 Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia "
[13] "4.1 Step 1 – define the study focus "                                                                                                    
[14] "4.2 Step 2 – identify intermediaries in the context "                                                                                    
[15] "4.3 Step 3 – map roles of the roles of intermediaries in eco-innovation "                                                                
[16] "4.4 Step 4 – assess the roles of intermediaries "                                                                                        
[17] "5 Discussion "                                                                                                                           
[18] "6 Conclusions and further research "  

Another test:

l <- lext(split_extract, index = "2,1")

[[1]]
[1] "2 Intermediaries and technological innovation systems "

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