-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopus_search_plus.R
91 lines (71 loc) · 3.76 KB
/
scopus_search_plus.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
# Run `rscopus::scopus_search` as many times as necessary based on the number of results and the search quota.
# Note. Before running the current function, the user must read in their Scopus API key confidentially
# (see https://cran.r-project.org/web/packages/rscopus/vignettes/api_key.html). An error appears if the key
# has not been read in.
scopus_search_plus =
function( query,
search_period, # single year or period (e.g., '2000-2010')
quota = 5, # Scopus API quota
view = c('STANDARD', 'COMPLETE'), # see https://cran.r-project.org/web/packages/rscopus/rscopus.pdf
inst_token = NULL, # see https://cran.r-project.org/web/packages/rscopus/rscopus.pdf
verbose = TRUE # print progress in R console
) {
require(rscopus)
# Error if API key missing
if(!have_api_key()) {
stop('The login key for the Scopus API has not been read in. Find out more at \n',
' https://cran.r-project.org/web/packages/rscopus/vignettes/api_key.html')
}
require(dplyr)
# Use tryCatch() to handle errors in scopus_search
res = tryCatch({
if(is.null(inst_token)) {
scopus_search(query = query, max_count = quota, count = quota,
date = search_period, view = view,
verbose = verbose)
} else {
scopus_search(query = query, max_count = quota, count = quota,
date = search_period, view = view,
headers = inst_token_header(inst_token),
verbose = verbose)
}
}, error = function(e) { # Print error message to console
print(paste("Error in nested function 'scopus_search':", e$message))
})
total_results = 0 : res$total_results
chunks = total_results[seq(1, length(total_results), quota)]
results = data.frame(author = as.character(), date = as.character(),
title = as.character(), publication = as.character(),
doi = as.character())
for(i_chunk in 1 : n_distinct(chunks)) {
# Use tryCatch() to handle errors in scopus_search
res = tryCatch({
if(is.null(inst_token)) {
scopus_search(query = query, max_count = quota, count = quota,
start = chunks[i_chunk], date = search_period,
view = view, verbose = verbose)
} else {
scopus_search(query = query, max_count = quota, count = quota,
start = chunks[i_chunk], date = search_period,
view = view, headers = inst_token_header(inst_token),
verbose = verbose)
}
}, error = function(e) { # Print error message to console
print(paste("Error in nested function 'scopus_search':", e$message))
})
for(i_entry in 1 : length(res$entries)) {
author = res$entries[[i_entry]]$`dc:creator`
date = res$entries[[i_entry]]$`prism:coverDisplayDate`
title = res$entries[[i_entry]]$`dc:title`
publication = res$entries[[i_entry]]$`prism:publicationName`
doi = res$entries[[i_entry]]$`prism:doi`
i_results = data.frame( author = ifelse(!is.null(author), author, NA),
date = ifelse(!is.null(date), date, NA),
title = ifelse(!is.null(title), title, NA),
publication = ifelse(!is.null(publication), publication, NA),
doi = ifelse(!is.null(doi), doi, NA) )
results = rbind(results, i_results)
}
}
return(results)
}