-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRscopus RapidWRA.r
195 lines (140 loc) · 6.12 KB
/
Rscopus RapidWRA.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#this script queries the scopus database for papers mentioning the species in the weed list in section 1
#to try different cleaning words, change terms in section 2b then rerun sections 2b and 3
#rscopus API key d2dcc4a928a10f0d9184f6ced6473c3c
#https://aurelien-goutsmedt.com/post/extracting-biblio-data-1/
library(bib2df)
library(rscopus)
library(bibliometrix)
library(tidyverse)
library(tictoc)
# Section 1. Import and reformat Scopus data -----------------------------------------
set_api_key("d2dcc4a928a10f0d9184f6ced6473c3c")
hdr<-inst_token_header("272404b5b445f8a89b33cda259416973" )
#starts run time timer
tic()
#create list of weeds to search
weed_list <- c("Cirsium vulgare", "Chenopdium album")
#create lists of abbreviated names
weed_list_abr <- paste0(substr(weed_list, 1, 1), ". ",sub("^\\S+\\s+", '', weed_list))
weed_list_abr2 <- paste0(substr(weed_list, 1, 1), " ",sub("^\\S+\\s+", '', weed_list))
#makes the string for Scopus query
QueryList <-
paste0("TITLE-ABS-KEY(weed* OR invasi* OR {introduced species} OR {invasive species} OR {invasive organisms} OR {alien invasive species} OR {invasive alien species} OR {weed control}) AND TITLE-ABS-KEY({", weed_list, "} OR {", weed_list_abr, "} OR {", weed_list_abr2, "})")
# #queries one of the species in list
# weed_query <- rscopus::scopus_search(QueryList[2], view = "COMPLETE", headers = hdr)
# weed_data_raw <- gen_entries_to_df(weed_query$entries)
# weed_papers <- weed_data_raw$df
#
# res2 = bibtex_core_data(weed_query)
#
#
# weed_query
# bibtex_core_data(weed_query)
# weed_papers_test<-convert2df(weed_query, dbsource = "scopus", format = "bibtex")
# # weed_affiliations <- weed_data_raw$affiliation
# # weed_authors <- weed_data_raw$author
#queries each species in list and adds to data table
for (i in 1:length(weed_list)) {
weed_query <-
rscopus::scopus_search(QueryList[i], view = "COMPLETE", headers = hdr)
weed_data_raw <- gen_entries_to_df(weed_query$entries)
species_papers<-weed_data_raw$df
species_papers$weed_searched<- weed_list[i]
if (i == 1) {
# if (exists("weed_papers") == F) {
weed_papers_raw <- species_papers
}
else{
weed_papers_raw <- bind_rows(weed_papers_raw, species_papers)
}
}
#removes temp object
rm("species_papers")
#remove special characters from column names
names(weed_papers_raw) <- names(weed_papers_raw) %>%
{gsub("prism:", "", .)}%>%
{gsub("dc:", "", .)}%>%
{gsub("-", "_", .)}%>%
{gsub("@", "_", .)}%>%
{gsub("[.$]", "", .)}
#will use weed papers from here to keep raw data intact
weed_papers<-weed_papers_raw
#makes titles lowercase and removes punctuation
weed_papers$title <- weed_papers$title %>% str_replace_all(., "[[:punct:]]", "") %>% tolower()
#adds lowercase title, abstract, journal, and keywords to the "combined" column
weed_papers$combined <- paste(weed_papers$title, tolower(weed_papers$description), tolower(weed_papers$authkeywords), tolower(weed_papers$publicationName))
#creates status column
weed_papers<- weed_papers %>% mutate(status="neutral", reason="")
# Section 2a. Clean data -----------------------------------------
#adds duplicates to removed_papers
removed_papers<-
filter(weed_papers,duplicated(paste(tolower(title)," ",weed_searched))) %>%
mutate(status="bad", reason="duplicate")
#removes duplicates from master list
weed_papers <- weed_papers %>%
distinct(paste(title," ",weed_searched), .keep_all=TRUE) %>%
select(-"paste(title, \" \", weed_searched)")
#create function to keep papers with certain words in the title
keep_words <- function(input) {
for (i in 1:length(input)) {
weed_papers <-
weed_papers %>%
mutate(status = ifelse((grepl(input[i], tolower(publicationName))) == TRUE, "good", status)) %>%
mutate(reason = paste0(ifelse(
status == "good",
paste0(ifelse(reason=="", input[i], paste0(reason, ", ", input[i])), " journal"),
reason
)))
}
return(weed_papers)
}
#create function to remove papers with certain words in the combined column
remove_word <- function(input) {
for (i in 1:length(input)) {
to_remove <-
weed_papers %>%
filter(.,grepl(input[i],combined) & status != "good") %>%
mutate(status = "bad", reason = paste(input[i]))
removed_papers <<- bind_rows(removed_papers,to_remove)
weed_papers <-
weed_papers %>%
filter(!(grepl(input[i],combined)& status != "good"))
}
return(weed_papers)
}
# Section 2b. clean data cont. ----------------------------------------------------------
#runs function to keep papers with these words in the Journal name
weed_papers<-keep_words(list("weed"))
#runs function to exclude papers with these words anywhere
weed_papers<-remove_word(list("insect pest", "breeding","pests", "wildlife","medicin","medical", "cancer", "carnivore", "domesticated", "folk", "herbal", "essential oils"))
# Section 3 Output -----------------------------------------
#creates summary of kept papers by counting them, grouped by status and reason
kept_summary<- weed_papers %>%
group_by(status, reason) %>%
summarise(n())
#renames summary columns
names(kept_summary) <- c("kept papers", "reason", "count")
#workaround that removes quotation marks that appeared in the reason column
kept_summary<-kept_summary %>% mutate(reason=noquote(reason))
#creates summary of removed papers by counting them, grouped by status and reason
removed_summary <- removed_papers %>%
group_by(status, reason) %>%
summarise(n())
#renames summary columns
names(removed_summary) <- c("removed papers", "reason", "count")
#display results
removed_summary
kept_summary
#displays run time
toc()
#export to bibtext (doesnt work)
# drop <- c("STATUS", "REASON", "COMBINED", "WEED_SEARCHED")
# weed_papers_2 = weed_papers[,!(names(weed_papers) %in% drop)]
#
# df2bib(weed_papers_2, file = "test.bib", append = FALSE)
# weed_data_raw <- gen_entries_to_df(weed_query$entries)
# weed_papers <- weed_data_raw$df
# weed_affiliations <- weed_data_raw$affiliation
# weed_authors <- weed_data_raw$author
# https://johnmuschelli.com/rscopus/articles/multi_author.html
#maybe adapt this code to query the species one by one?