-
Notifications
You must be signed in to change notification settings - Fork 2
/
ajpsReviewers.r
138 lines (105 loc) · 3.2 KB
/
ajpsReviewers.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
### -----------------------------
### ajps reviewers
### simon munzert
### -----------------------------
## goals ------------------------
# fetch list of AJPS reviewers from PDFs
# locate them on a map
## tasks ------------------------
# downloading PDF files
# importing them into R (as plain text)
# extract information via regex
# geocoding
## packages ---------------------
library(stringr) # string processing
library(rvest) # scraping suite
library(pdftools) # get text out of PDFs
library(ggmap) # geocoding
## directory ---------------------
wd <- ("./ajpsReviewers")
dir.create(wd)
setwd(wd)
## code ---------------------
## step 1: inspect page
url <- "http://ajps.org/list-of-reviewers/"
browseURL(url)
## step 2: retrieve pdfs
# get page
content <- read_html(url)
# get anchor (<a href=...>) nodes via xpath
anchors <- html_nodes(content, xpath = "//a")
# get value of anchors' href attribute
hrefs <- html_attr(anchors, "href")
# filter links to pdfs
pdfs <- hrefs[ str_detect(hrefs, "reviewer.*pdf") ]
pdfs <- pdfs[!is.na(pdfs)]
pdfs
# define names for pdfs on disk
pdf_names <- str_extract(pdfs, "\\d{4}.pdf")
pdf_names
pdf_names[1] <- "2015.pdf"
# download pdfs
for(i in seq_along(pdfs)) {
download.file(pdfs[i], pdf_names[i], mode="wb")
}
## step 3: convert pdfs to raw txt
for (i in seq_along(pdf_names)) {
pdftext <- pdf_text(pdf_names[i])
write(pdftext, file = paste0(str_extract(pdf_names[i], "[[:digit:]]{4}"), ".txt"))
}
## step 4: import data
txt_names <- list.files(pattern = "txt")
txt_names
rawdat <- readLines(txt_names[4]) # only reviewers from 2013
head(rawdat)
## step 5: tidy data
rev13 <- rawdat %>%
str_c(collapse="") %>%
str_replace_all(pattern = "[!\f]", replacement = " ") %>%
str_replace_all(pattern = "\\]", replacement = " ") %>%
str_split(pattern = "\\)") %>%
unlist()
head(rev13)
rev13 <- rev13[-1]
names <- rev13 %>%
str_extract(pattern = "^.*?,") %>%
str_replace_all(pattern = " |,", replacement = " ") %>%
str_trim()
head(names)
institution <- rev13 %>%
str_extract(pattern = ",.*\\(") %>%
str_replace_all(pattern = " |\\(|^, ", replacement = " ") %>%
str_trim
head(institution)
institution %>% table %>% sort
reviews <- rev13 %>%
str_extract("\\(.*") %>%
str_extract("\\d+") %>%
as.numeric()
head(reviews)
rev13_dat <- data.frame(names = names, institution = institution, reviews = reviews)
head(rev13_dat)
## step 6: geocode reviewers/institutions
# geocoding takes a while -> save results
# 2500 requests allowed per day
if ( !file.exists("institutions2013_geo.RData")){
pos <- geocode(rev13_dat$institution)
geocodeQueryCheck()
save(pos, file="institutions2013_geo.RData")
} else {
load("institutions2013_geo.RData")
}
head(pos)
rev13_dat$lon <- pos$lon
rev13_dat$lat <- pos$lat
## step 7: plot reviewers, worldwide
mapWorld <- borders("world")
map <-
ggplot() +
mapWorld +
geom_point(aes(x=rev13_dat$lon, y=rev13_dat$lat) ,
color="#F54B1A90", size=3 ,
na.rm=T) +
theme_bw() +
coord_map(xlim=c(-180, 180), ylim=c(-60,70))
map