-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crossref_members_funders.R
148 lines (120 loc) · 4.54 KB
/
Crossref_members_funders.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#this script uses the rcrossref package to interface with the Crossref API
#to get information on funder coverage per member ID
#Crossref REST API information: https://github.com/CrossRef/rest-api-doc
#use rcrossref
#vignette: https://cran.r-project.org/web/packages/rcrossref/rcrossref.pdf
#install.packages("tidyverse")
#install.packages("rcrossref")
library(tidyverse)
library(rcrossref)
#set email in Renviron
#file.edit("~/.Renviron")
#add email address to be shared with Crossref:
#crossref_email = [email protected]
#save the file and restart your R session
#Use low level API as this includes abstract coverage per type
#set parse = FALSE to get JSON, parse = TRUE to get list output
getCrossref_low <- function(offset){
res <- cr_members_(offset = offset,
limit = 1000,
parse = TRUE) %>%
.$message %>%
.$items
return(res)
}
#add progress bar to function
getCrossref_low_progress <- function(offset){
pb$tick()$print()
result <- getCrossref_low(offset)
return(result)
}
#extract relevant variables with pluck
#also consider (from ?pluck):
#The map() functions use pluck() by default to retrieve multiple values from a list:
# map(x, list(2, "elt")) is equivalent to map(x, pluck, 2, "elt")
extractData_all <- function(x){
data <- tibble(
id = map_dbl(x, "id"),
primary_name = map_chr(x, "primary-name"),
count_current_type = map_dbl(x,
list("counts",
"current-dois"),
.default = 0),
deposits_funders_current = map_lgl(x,
list("flags",
"deposits-funders-current"),
.default = NA),
funders_current_type = map(x,
list("coverage",
"funders-current"),
.default = 0)) %>%
#keep only members with (current) output of type
filter(count_current_type > 0) %>%
#convert abstract coverage into numerical, then percentage
mutate(funders_current_type = as.double(funders_current_type),
funders_current_type = round(funders_current_type, 3)) %>%
#arrange in descending order of count
arrange(desc(count_current_type))
return(data)
}
extractData_type <- function(x, type){
data <- tibble(
id = map_dbl(x, "id"),
primary_name = map_chr(x, "primary-name"),
count_current_type = map_dbl(x,
list("counts-type",
"current",
type),
.default = 0),
deposits_funders_current = map_lgl(x,
list("flags",
"deposits-funders-current"),
.default = NA),
funders_current_type = map(x,
list("coverage-type",
"current",
type,
"funders"),
.default = 0)) %>%
#keep only members with (current) output of type
filter(count_current_type > 0) %>%
#convert abstract coverage into numerical
mutate(funders_current_type = as.double(funders_current_type)) %>%
#arrange in descending order of count
arrange(desc(count_current_type))
return(data)
}
#define function to write to csv
toFile <- function(type, data, path){
filename <- paste0(path,"/crossref_member_funders_current_", type, "_", date,".csv")
write_csv(data, filename)
}
#------------------------------------------------------------------------------
#set date
date <- Sys.Date()
#create output directory
path <- file.path("data",date)
dir.create(path)
#get number of members
res <- cr_members(limit=0)
total <- res$meta$total_results
#set vector of offset values
c <- seq(0, total, by=1000)
#set parameter for progress bar
pb <- progress_estimated(length(c))
#get API results, flatten into 1 list
res <- map(c, getCrossref_low_progress) %>%
flatten()
#extract data for different types, write each to file
type = "all"
data <- extractData_all(res)
toFile(type, data, path)
type = "journal-article"
data <- extractData_type(res, type)
toFile(type, data, path)
type = "posted-content"
data <- extractData_type(res, type)
toFile(type, data, path)
type = "book-chapter"
data <- extractData_type(res, type)
toFile(type, data, path)