基于Scopus通过DOI获取文献参考文献的R代码执行问题
Hey there, I see you're having trouble pulling reference titles using the rscopus package—let's get this sorted out without messy eval(parse) hacks, which are error-prone and generally not the best practice here.
The Core Issue with Your Original Code
Your approach of building a string to evaluate was overcomplicating things. The abstract_retrieval response returns a nested list structure, so we can directly index into it instead of constructing code as text. Let's simplify this.
Working Solution Code
Here's a cleaned-up, reliable version of your code that properly extracts reference titles and stores them in your data frame:
library(rscopus) # Load your sample DOI data df <- data.frame(doi = c("10.1109/TPAMI.2018.2798607", "10.1109/CNS.2017.8228696")) # Initialize empty results data frame df_references <- data.frame( initial_paper = character(), ref_title = character(), stringsAsFactors = FALSE ) # Loop through each DOI for (i in 1:nrow(df)) { # Retrieve the abstract/metadata for the current DOI retrieval_result <- abstract_retrieval(df$doi[i], identifier = "doi") # Extract the list of references from the response references <- retrieval_result$content$`abstracts-retrieval-response`$`item`$bibrecord$tail$`bibliography`$reference # Skip if there are no references for this paper if (is.null(references)) next # Loop through each reference to extract the title for (ref in references) { # Get the title text (handle cases where title might be missing) ref_title <- if (!is.null(ref$`ref-info`$`ref-title`$`ref-titletext`)) { ref$`ref-info`$`ref-title`$`ref-titletext` } else { "No title available" } # Add to results data frame df_references <- rbind(df_references, data.frame( initial_paper = df$doi[i], ref_title = ref_title, stringsAsFactors = FALSE )) } } # View the results print(df_references)
Key Improvements Explained:
- Direct List Indexing: Instead of building strings to evaluate, we directly access the nested list elements (
ref$ref-info$ref-title$ref-titletext`). This is cleaner and avoids parsing errors. - Null Handling: Added checks to skip papers with no references, and handle cases where a reference might not have a title (so your code doesn't break unexpectedly).
- Cleaner Initialization: Properly initialized the results data frame with correct column types (
stringsAsFactors = FALSEto avoid outdated factor behavior).
Why eval(parse) Was a Problem
Constructing code as text and evaluating it is risky because:
- It's easy to make syntax errors in the string (like missing brackets or quotes).
- It can introduce security issues if you're working with untrusted data.
- It makes the code harder to read and debug.
Direct list access is the standard, safer way to work with nested list structures in R.
内容的提问来源于stack exchange,提问作者Elr Mant

