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))
}