-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDOIs.R
60 lines (48 loc) · 1.99 KB
/
getDOIs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# START -------------------------------------------------------------------
rm(list=ls())
set.seed(1234)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# LOAD PACKAGES -----------------------------------------------------------
#install.packages('rcrossref')
library(rcrossref)
library(revtools)
library(stringdist)
# LOAD BIBLIOGRAPHY -------------------------------------------------------
#read in bibliography file (e.g. ris, bib)
bib<-revtools::read_bibliography('ENTER_FILENAME')
#add blank doi column
bib$doi<-''
# GET DOIS FROM CROSSREF -----------------------------------------------
alldists<-c()
for (a in 1:nrow(bib)){
#send the title of the reference as a query to CrossRef and get reuslts back
results<-tryCatch(
rcrossref::cr_works(query=bib$title[a], sort = 'relevance'),
error=function(e) NULL)
#check if no results, if so, return NA then iterate the loop
if(is.null(results)==T){
print(paste(a,') ', NA, sep=''))
bib$doi[a]<-NA
alldists<-c(alldists,NA)
a<-a+1
}
#if there are results, return them, find nearest match to title and get the doi
else
{
#extract the bibliographic info
results_data<-results$data
#generate vector of distances between your bibliography title and that of crossref
dists<-stringdist::stringdist(bib$title[a],
results_data$title)
#For those where the distance is over 20 characters, coerce to NA (based on brief tests, this is the threshold of similarity)
dists[dists>20]<-NA
#find the index of the nearest matching title in the CrossRef table
nearestmatch<-which(dists==min(dists, na.rm = T))
#snatch its DOI and add to your bibliographic dataframe
bib$doi[a]<-results_data[nearestmatch,]$doi[1]
#optional print of doi result, good for bug testing/development
#print(paste(a,') ', results_data[nearestmatch,]$doi[1], sep=''))
}
}
#write the new bibliography with the DOIs
#write_bibliography(bib, 'ENTER_FILENAME_wRDOIs')