Search code examples
rcurldoi

r convert list of DOIs to BibTeX


I have a list of DOIs that I would like to convert into BibTeX records. The bib2doi package doesn't seem to be working so I wrote the following code using R's curl package to scan through the list, create the bibtex record and append it to a file. It works fine for many DOIs but it returns this error (Failed to connect to data.chinadoi.cn port 80: Connection refused) for the DOI 10.11975/j.issn.1002-6819.2017.z1.035. What I can't figure out is how to write out the bad DOI and keep going. Here's the code with three DOIs, the second DOI is the one that fails.

library(curl)
DOIlist <- c("10.1111/1748-5967.12330", "10.11975/j.issn.1002-6819.2017.z1.035", "10.1016/j.envsci.2019.03.017")

h <- new_handle()
handle_setheaders(h, "accept" = "application/x-bibtex")

for (i in 1:length(DOIlist)) {
  url <- paste0("https://doi.org/", DOIlist[i])
  print(paste0("url: ", url))
  curl_download(url, destfile = "curltest.bib", handle = h, mode = "a")
}

Solution

  • If you want the for loop to keep going after an error is thrown because of a bad DOI, you can wrap the curl_download() call in try(). It'll still throw the error, but your loop will keep going:

    library(curl)
    DOIlist <- c("10.1111/1748-5967.12330", "10.11975/j.issn.1002-6819.2017.z1.035", "10.1016/j.envsci.2019.03.017")
    
    h <- new_handle()
    handle_setheaders(h, "accept" = "application/x-bibtex")
    
    for (i in 1:length(DOIlist)) {
      url <- paste0("https://doi.org/", DOIlist[i])
      print(paste0("url: ", url))
      try(curl_download(url, destfile = "curltest.bib", handle = h, mode = "a"))
    }