Search code examples
rscopus

Execute a command and save the results into a df from a loop


I try to use scopus using the doi of the papers aiming to take the references of every paper:

Step 1 load example data:

library(rscopus)
df = data.frame(doi = c("10.1109/TPAMI.2018.2798607", "10.1109/CNS.2017.8228696"))

Step 2 iterate through the loop and keep the results of every iteration into a dataframe.

for (i in 1:nrow(df)) {
x = abstract_retrieval(df$doi[i], identifier= "doi")

for (a in 1:length(x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference)){
     ref <- paste("x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference[[",a,"]]$`ref-info`$`ref-title`")
     df_references <- rbind(df_references, data.frame(initial_paper = df$doi[i],
                                           ref_title = ref))
}
}

My problem is the the ref is the character and how can I execute? From a previous answer parse mentioned is not a good solution.

update code with df:

df_references <- data.frame(matrix(nrow = 0, ncol = 2))
names(df_references) = c("initial_paper", "ref_title")

for (i in 1:nrow(df)) {
x = abstract_retrieval(df$doi[i], identifier= "doi")

for (a in 1:length(x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference)){
     #eval(parse(ref = paste("x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference[[",a,"]]$`ref-info`$`ref-title`")))
     call_str <- paste("ref <- x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference[[",a,"]]$`ref-info`$`ref-title`")
     eval(parse(text = call_str))
     print(text)
     df_references <- rbind(df_references, data.frame(initial_paper = df$doi[i],
                                           ref_title = ref$`ref-titletext`))
}
}

Solution

  • eval and parse seems to be exactly what you need. I don't have Elsevier API key, so I can not test the solution. So try this by yourself:

    library(rscopus)
    df = data.frame(doi = c("10.1109/TPAMI.2018.2798607", "10.1109/CNS.2017.8228696"))
    
    df_references <- NULL
    for (i in 1:nrow(df)) {
    x = abstract_retrieval(df$doi[i], identifier= "doi")
    
    for (a in 1:length(x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference)){
         call_str <- paste("ref <- x$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference[[",a,"]]$`ref-info`$`ref-title`")
         eval(parse(text = call_str))
         df_references <- rbind(df_references, data.frame(initial_paper = df$doi[i],
                                               ref_title = ref))
    }
    }
    

    The idea is to construct a string of your desired call. Then parse it (don't forget to pass it as text argument). And finally evaluate it.