-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek_2.Rmd
398 lines (280 loc) · 12.7 KB
/
Week_2.Rmd
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
---
title: "Data Review"
author: "Etienne Rolland"
date: "11/10/2020"
output: html_document
---
```{r setup, include=FALSE}
#####Make sure you load any required packages.
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Import Your Data
*In the following code hunk, import your data.*
**Update :** one problem appear with the CDB90 Battle Dataset I originally choose to use. I needed to do additionnal scrapping, using the url in the battles.csv, to query additionnal data (latitude, longitude, etc). Sadly, the dataset is slightly old, and the links towards the resources of dbpedia were not the same as before. Hence, the query via SPARQL were not valid. Finally, I will just create the dataset using queries to the website.
I am loading the battles.csv, just to got the name of the fronts, to be sure to not forget something in the subsequent queries.
```{r data_import, warning=FALSE}
#### Use read_csv() or another function
#### Make sure your data is converted into a tibble.
#### For demonstration purposes, this example uses the mtcars data.
library(utils)
library(readr)
battles <- read_csv("battles.csv")
battles <- battles[grep(battles$cow_warname, pattern="World War I of 1914-1918"),]
```
```{r}
unique(battles$war)
```
## Scrapping
### Testing queries
If you are wondering what the hell is SPARQL, it is [here](https://www.r-bloggers.com/2013/01/sparql-with-r-in-less-than-5-minutes/).
```{r}
library(SPARQL)
```
```{r}
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?lat ?long ?causalties year(?date) as ?year month(?date) as ?month day(?date) as ?day ?name ?campaign ?desc where {
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:lat ?lat .
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:long ?long .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:causalties ?causalties .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:date ?date .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:name ?name .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:isPartOfMilitaryConflict ?campaign .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:abstract ?desc .
FILTER (langMatches(lang(?desc),"en"))
}'
query <- str_replace_all(query, "http://dbpedia.org/resource/Battle_of_Lorraine", "http://dbpedia.org/resource/Battle_of_Magdhaba")
qd <- SPARQL(endpoint,query)
df <- qd$results
df
```
```{r}
cleaning_and_concatenate_result <- function(df) {
df_concatenated <- df[1,]
for (column in colnames(df)) {
if (column == "depiction") { # une seule image
df_concatenated[column] <- df[1,]$depiction
next
}
content <- df[column]
content <- unique(content)
content <- paste(content, sep = ", ")
content <- str_replace_all(string=content, pattern = "c\\(", replacement = "")
content <- str_replace_all(string=content, pattern = '\\"', replacement = "")
content <- str_replace_all(string=content, pattern = '\\)$', replacement = "")
content <- str_replace_all(string=content, pattern = '@en', replacement = "")
df_concatenated[column] <- content
}
return(df_concatenated)
}
```
```{r}
library(digest)
# https://stackoverflow.com/questions/33689980/get-thumbnail-image-from-wikimedia-commons
digest("Camel_corps_at_Magdhaba.jpg", algo="md5", serialize=F)
```
```{r}
clean_url_image <- function(depiction) {
# the image inside dbpedia is a relative/redirection path inside wiki commons
# we need the exact url for printing inside a shiny app
#depiction <- "<http://commons.wikimedia.org/wiki/Special:FilePath/Camel_corps_at_Magdhaba.jpg>"
depiction <- str_replace_all(depiction ,"<|>", "")
img_name <- unlist(str_split(depiction, "/"))
img_name <- tail(img_name, n=1) #last one
hash <- digest(img_name, algo="md5", serialize=F)
# hash
# new column for the popup label
depiction <- paste0("https://upload.wikimedia.org/wikipedia/commons/",
substr(hash, 1, 1), # first character
"/",
substr(hash, 1, 2),
"/",
URLencode(img_name))
return(depiction)
}
```
```{r}
clean_url_image("<http://commons.wikimedia.org/wiki/Special:FilePath/Camel_corps_at_Magdhaba.jpg>")
```
### Scrapping for real
```{r}
got_data_dbpedia <- function(url) {
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?lat ?long ?strength ?causalties ?campaign ?depiction year(?date) as ?year month(?date) as ?month day(?date) as ?day ?name ?result ?desc where {
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:lat ?lat .
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:long ?long .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:strength ?strength .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:causalties ?causalties .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:isPartOfMilitaryConflict ?campaign .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:depiction ?depiction .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:date ?date .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:name ?name .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:result ?result .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:abstract ?desc .
FILTER (langMatches(lang(?desc),"en"))
}'
query <- str_replace_all(query, "http://dbpedia.org/resource/Battle_of_Lorraine", url)
qd <- SPARQL(endpoint,query)
df <- qd$results
df <- cleaning_and_concatenate_result(df)
df$depiction <- clean_url_image(df$depiction)
df$desc <- str_replace_all(df$desc, "–", "-")
df$name <- iconv(df$name , from="UTF-8", to="LATIN1")
df$desc <- iconv(df$desc , from="UTF-8", to="LATIN1")
df$campaign <- str_replace_all(df$campaign , "<|>", "")
df$campaign <- str_replace_all(df$campaign , "http://dbpedia.org/resource/", "")
return(df)
}
```
```{r}
url <- "http://dbpedia.org/resource/Battle_of_Magdhaba"
```
```{r}
got_data_dbpedia(url)
```
### Politely
See : [the polite package](https://dmi3kno.github.io/polite/index.html).
```{r}
library(polite)
got_data_dbpedia <- politely(got_data_dbpedia, verbose=TRUE)
```
```{r}
# url <- "http://dbpedia.org/resource/Battle_of_Lorraine"
got_data_dbpedia(url)
```
Edit while coding : the recursion add up for 100 battles.
See the Recursion entry below.
```{r}
got_battles_for_front <- function(url) {
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?battle where {
?battle dbo:isPartOfMilitaryConflict <balise_to_change>
}'
query <- str_replace_all(query, "balise_to_change", url)
qd <- SPARQL(endpoint,query)
df <- qd$results
vector_battles <- as.vector(unlist(df))
vector_battles <- iconv(vector_battles, from="UTF-8", to="LATIN1")
vector_battles <- str_replace_all(vector_battles, "<|>", "")
# recursion ?
for (url in vector_battles) {
vector_battles <- c(vector_battles, try(got_battles_for_front(url)))
}
return(vector_battles[grepl(vector_battles, pattern="http")])
}
got_battles_for_front <- politely(got_battles_for_front, verbose=TRUE)
```
```{r message=FALSE, warning=FALSE}
vector_battles <- got_battles_for_front("http://dbpedia.org/resource/Western_Front_(World_War_I)")
length(vector_battles)
```
```{r message=FALSE, warning=FALSE}
length(unique(vector_battles))
```
```{r}
vector_battles <- head(vector_battles)
```
```{r message=FALSE, warning=FALSE}
# Liège bug et retourne une dataframe vide mais parcequ'elle a pas lon et lat.
list_battles_df<-list()
i <- 0
for (battles in vector_battles) {
i <- i + 1
list_battles_df[[i]] <- try(got_data_dbpedia(battles))
}
```
```{r}
list_battles_df <- list_battles_df[sapply(list_battles_df, class) == "data.frame"]
```
```{r}
list_battles_df <- do.call(rbind, list_battles_df)
# list_battles_df # works
```
```{r}
create_front_battle_df <- function(url_front) {
vector_battles <- got_battles_for_front(url_front)
#vector_battles <- head(vector_battles)
list_battles_df<-list()
i <- 0
for (battles in vector_battles) {
i <- i + 1
list_battles_df[[i]] <- try(got_data_dbpedia(battles))
}
list_battles_df <- list_battles_df[sapply(list_battles_df, class) == "data.frame"]
list_battles_df <- do.call(rbind, list_battles_df)
return(list_battles_df)
}
```
### Recursion
Some front are nested inside battles, see for example :
Gallipoli_campaign is queried from inside https://dbpedia.org/page/Middle_Eastern_theatre_of_World_War_I.
Edit : after some refactoring, the recursion must go inside got_battles_for_front
```{r message=FALSE, warning=FALSE}
#head(got_data_dbpedia("http://dbpedia.org/resource/Gallipoli_campaign"))
```
```{r message=FALSE, warning=FALSE}
#got_battles_for_front("http://dbpedia.org/resource/Gallipoli_campaign")
```
## Part 1
*Using words, describe the visualization you are going to make using which variables/characteristics in your data*:
*Western front tab*: For my first figure, I am going to create a bubble map that plots battles of the first war, on the western front map. A sidebar will allow to choose the year, and the markers of each battle will be clickable and display informations (descriptions, causalties, etc.) The data required are contained on the dbpedia page western front. Colors of the battle/bubble will be made based on the campaign color.
```{r message=FALSE, warning=FALSE}
# western_front <- create_front_battle_df("http://dbpedia.org/resource/Western_Front_(World_War_I)")
# dim(western_front)
# head(western_front, 3)
# write.csv(western_front,file='western_front.csv', row.names=FALSE)
```
```{r message=FALSE, warning=FALSE}
World_War_I <- create_front_battle_df("http://dbpedia.org/resource/World_War_I")
dim(World_War_I)
write.csv(World_War_I,file='World_War_I.csv', row.names=FALSE)
```
```{r message=FALSE, warning=FALSE}
#bug
# Eastern_theatre_of_World_War_I <- create_front_battle_df("http://dbpedia.org/resource/Eastern_Front_(World_War_I)")
# dim(Eastern_theatre_of_World_War_I)
# write.csv(Eastern_theatre_of_World_War_I,file='Eastern_theatre_of_World_War_I.csv', row.names=FALSE)
```
## Part 2
*Eastern front tab*: For my second figure, I am going to create a bubble map that plots battles of the first war, on the eastern front map. A sidebar will allow to choose the year, and the markers of each battle will be clickable and display informations (descriptions, causalties, etc.) The data required are contained on the dbpedia page western front. Colors of the battle/bubble will be made based on the campaign color.
Some additionnal data cleaning is required on the fly.
```{r message=FALSE, warning=FALSE}
Middle_Eastern_theatre_of_World_War_I <- create_front_battle_df("http://dbpedia.org/resource/Middle_Eastern_theatre_of_World_War_I")
dim(Middle_Eastern_theatre_of_World_War_I)
head(Middle_Eastern_theatre_of_World_War_I, 3)
write.csv(Middle_Eastern_theatre_of_World_War_I,file='Middle_Eastern_theatre_of_World_War_I.csv', row.names=FALSE)
```
## Part 3, 4, 5 and more
*World map tab*: For my second figure, I am going to create a bubble map that plots battles of the first war, on the world front map. A sidebar will allow to choose the year, and the markers of each battle will be clickable and display informations (descriptions, causalties, etc.) The data required are contained on the dbpedia page western front. Colors of the battle/bubble will be made based on the campaign color.
This also include an African map tab and a European tab map.
### Africa
```{r message=FALSE, warning=FALSE}
#Africa, from https://dbpedia.org/page/African_theatre_of_World_War_I
African_theatre_of_World_War_I <- create_front_battle_df("http://dbpedia.org/resource/African_theatre_of_World_War_I")
dim(African_theatre_of_World_War_I)
head(African_theatre_of_World_War_I, 3)
write.csv(African_theatre_of_World_War_I,file='African_theatre_of_World_War_I.csv', row.names=FALSE)
```
### Europa
```{r message=FALSE, warning=FALSE}
European_theatre_of_World_War_I <- create_front_battle_df("http://dbpedia.org/resource/European_theatre_of_World_War_I")
dim(European_theatre_of_World_War_I)
head(European_theatre_of_World_War_I[1:8], 3)
write.csv(European_theatre_of_World_War_I,file='European_theatre_of_World_War_I.csv', row.names=FALSE)
```
### Asia
```{r message=FALSE, warning=FALSE}
Asian_and_Pacific_theatre_of_World_War_I <- create_front_battle_df("http://dbpedia.org/resource/Asian_and_Pacific_theatre_of_World_War_I")
dim(Asian_and_Pacific_theatre_of_World_War_I)
head(Asian_and_Pacific_theatre_of_World_War_I, 3)
write.csv(Asian_and_Pacific_theatre_of_World_War_I,file='Asian_and_Pacific_theatre_of_World_War_I.csv', row.names=FALSE)
```
### All world
```{r data_3}
all_battles <- rbind(Middle_Eastern_theatre_of_World_War_I, Asian_and_Pacific_theatre_of_World_War_I, European_theatre_of_World_War_I, African_theatre_of_World_War_I)
head(all_battles, 3)
write.csv(df,file='all_battles_WWI.csv', row.names=FALSE)
```