Problem with code for searching DOI numbers for a list of references.

I would like to make a code that I can use to query sources to display the DOI numbers of each source. There is a website where you can make such searches: https://search.crossref.org/

Here, for example, the title of the article is entered into the search and as a result a DOI number for the corresponding article is displayed.

Unfortunately, when executing the code, the following error message appears. Does anyone have an idea how I can fix the error? I am quite new to coding.

library(httr)
library(jsonlite)

get_doi <- function(reference) {
query <- paste0("https://api.crossref.org/works?query=", reference)
response <- GET(query)
data <- fromJSON(content(response, as = "text"))
if (is.null(data$message$items)) {
return(NA)
} else {
doi <- data$message$items[[1]]$DOI
return(doi)
}
}

reference <- "Sports marketing. Fundamentals – Strategies – Instruments, Wiesbaden 2023 (gemeinsam mit P. Rohlmann)"
doi <- get_doi(reference)
if (is.na(doi)) {
print("The DOI for the reference could not be found.")
} else {
print(paste("The DOI for the reference is:", doi))
}

Hi,

Welcome to the RStudio community!

Here is my fix:

library(httr)
library(jsonlite)

get_doi <- function(reference) {
  query <- paste0("https://api.crossref.org/works?query=", URLencode(reference))
  response <- GET(query)
  data <- fromJSON(content(response, as = "text", encoding = "UTF-8"))
  if (is.null(data$message$items)) {
    return(NA)
  } else {
    doi <- data$message$items$DOI[1]
    return(doi)
  }
}

reference <- "Sports marketing. Fundamentals – Strategies – Instruments, Wiesbaden 2023 (gemeinsam mit P. Rohlmann)"
doi <- get_doi(reference)
if (is.na(doi)) {
  print("The DOI for the reference could not be found.")
} else {
  print(paste("The DOI for the reference is:", doi))
}

# [1] "The DOI for the reference is: 10.1007/978-3-658-39122-5"
  • Your main issue was not encoding the search text properly. You cannot put things like spaces or other special characters in an API call, so the URLencode() function makes sure you get a safe string.
  • I also changed the way you access the DOI as this was not correct either. However, the way you implement is now is just pulling the first one, which might not always be the correct one...

Hope this helps,
PJ

HI PJ,

Thank you so much for your help.

What is the easiest way to conduct the search for a List of references? How to copy a list of references and how to conduct the search for e.g. 20 references at once?

Hi,

You can simply write a loop, or use an sapply statement, to go through a list of references, but again, be careful that the top DOI might not always be the correct one per se.

PJ

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.