I'm using rcrossref
package to collect abstracts for multiple DOIs stored in a column of a data frame, and I want the outputs (the abstracts) to be moved over into another column of the same data frame. I'm doing this by running a for loop, but:
- the loop seems to get hung up on the error message that appears if there is no abstract available.
- A second kind of error also occurs when there is no DOI value in the input column.
How might I go about skipping these errors and moving on to the next row when they are encountered?
Here is my R code:
library(bib2df)
library(rcrossref)
url <- "https://gist.githubusercontent.com/zackbatist/46c14011fd5dd4e2763842cd98627927/raw/e8678589cbb9f73ada52e7944bf617e588e1a5fe/GS01ax.bib"
df <- bib2df(url)
df
str(df)
df$DOI
df$ABSTRACT <- NA
df$ABSTRACT
for (i in 1:nrow(df)) {
n <- cr_abstract(doi = df[i,28])
df[i,31] <- n
}
df$ABSTRACT
FYI, df$DOI
corresponds with the 28th column, and df$ABSTRACT
corresponds with the 31st column.
EDIT pertaining to my comment below:
for (i in 1:nrow(df)) {
try(n <- cr_abstract(doi = df[i,28]))
try(df[i,31] <- n)
}
EDIT including tracebacks (never done these before so pardon if I'm doing this wrong)
for error 1:
Error: no abstract found for 10.11141/IA.44.15
3.
stop("no abstract found for ", doi, call. = FALSE)
2.
cr_abstract(doi = df[i, 28])
1.
.traceback(for (i in 1:nrow(df)) {
n <- cr_abstract(doi = df[i, 28])
df[i, 31] <- n
})
and for error 2:
Error: Not Found (HTTP 404)
3.
stop(sprintf("%s (HTTP %s)", x$message, x$status_code), call. = FALSE)
2.
res$raise_for_status()
1.
cr_abstract(doi = df[i, 28])