-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile_website.R
535 lines (475 loc) · 20 KB
/
compile_website.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# install packages ---------------------------------------------------------
packages <- c("tidyverse", "testthat", "lingglosses", "lingtypology", "bib2df",
"DT", "knitr", "ymlthis", "rmarkdown", "RefManageR", "stringi",
"readxl", "jsTreeR", "rrapply", "spelling")
to_install <- packages[!(packages %in% installed.packages())]
if(length(to_install) > 0){
install.packages(to_install, dependencies = TRUE)
}
suppressPackageStartupMessages(library(tidyverse))
# CREATE VILLAGE DATASET --------------------------------------------------
# Moroz, George, & Verhees, Samira. (2020). East Caucasian villages dataset (Version v2.0) [Data set]. Zenodo. https://doi.org/10.5281/zenodo.5588473
# read_tsv("https://raw.githubusercontent.com/sverhees/master_villages/master/data/TALD/tald_villages.tsv",
# progress = FALSE, show_col_types = FALSE) |>
# write_tsv("data/tald_villages.csv")
#
# read_tsv("https://raw.githubusercontent.com/sverhees/master_villages/master/data/villages.tsv",
# progress = FALSE, show_col_types = FALSE) |>
# select(village, rus_village, lat, lon, gltc_lang, gltc_dialect, version) |>
# rename(village_dataset_version = version) |>
# write_csv("data/villages.csv")
#
# 17.12.2023 George Moroz: I don't think that we need to download the dataset every time. It is better to have the version for TALD that can be easily changed.
# RUN TESTS ----------------------------------------------------------------
# file.remove("test_logs.txt")
# testthat::test_dir("tests")
# test_logs <- read_lines("test_logs.txt")
# write_lines(test_logs[test_logs != "everything is ok"], "test_logs.txt")
# convert .bib.tsv to .bib -------------------------------------------------
library(bib2df)
walk(list.files("data/orig_bib_tsv", full.names = TRUE), function(bib_tsv){
bib_tsv_df <- read_tsv(bib_tsv, progress = FALSE, show_col_types = FALSE)
bib_tsv |>
str_remove_all("[_\\.]tsv") |>
str_replace("_bib$", "\\.bib") ->
result_file
if(nrow(bib_tsv_df) == 0) {
write_lines("", result_file)
} else {
bib_tsv_df |>
mutate(TITLE = ifelse(is.na(TITLE_TRANSLATION), TITLE, str_c(TITLE, " [", TITLE_TRANSLATION, "]")),
BOOKTITLE = ifelse(is.na(BOOKTITLE_TRANSLATION), BOOKTITLE, str_c(BOOKTITLE, " [", BOOKTITLE_TRANSLATION, "]"))) |>
df2bib(result_file)
}
})
readxl::read_xlsx("data/biblib.xlsx",
col_types = c(rep("text", 4),
"numeric", # YEAR
rep("text", 9),
"numeric", # VOLUME
rep("text", 8))) |>
mutate(TITLE = ifelse(is.na(TITLE_TRANSLATION),
TITLE,
str_c(TITLE, " [", TITLE_TRANSLATION, "]")),
BOOKTITLE = ifelse(is.na(BOOKTITLE_TRANSLATION),
BOOKTITLE,
str_c(BOOKTITLE, " [", BOOKTITLE_TRANSLATION, "]"))) |>
df2bib("data/bibliography.bib")
# convert cyrillic to latin -----------------------------------------------
library(stringi)
cyr_latin_coresp <- "
щ > šč;
ю > ju;
я > ja;
х > x;
Э > È;
э > è;
Щ > Šč;
Ю > Ju;
Я > Ja;
Х > X;
:: cyrillic-latin;
"
# dataset with some transcription fixes like: Lander, Jurij -> Lander, Yury
tr_patches <- read_csv("data/transliteration_patches.csv",
show_col_types = FALSE)
vector_of_patches <- tr_patches$to
names(vector_of_patches) <- tr_patches$from
walk(c(list.files("data/orig_bib", full.names = TRUE), "data/bibliography.bib"), function(i){
read_lines(i, progress = FALSE) |>
stri_trans_general(cyr_latin_coresp, rules=TRUE) |>
str_replace_all(vector_of_patches) |>
write_lines(i)
})
# embrace uppercased letters with curly braces ----------------------------
regular_expression <- str_c("((?<=[ \\[\\-\\(\\</])[", str_c(c(LETTERS, "Ž", "Č", "Š", "Ë", "É"), collapse = ""), "])")
walk(c(list.files("data/orig_bib", full.names = TRUE), "data/bibliography.bib"), function(i){
if(file.info(i)$size > 7){
bib2df(file = i) |>
mutate(TITLE = ifelse(!is.na(TITLE),
str_replace_all(TITLE, regular_expression, "\\{\\1\\}"),
NA),
BOOKTITLE = ifelse(!is.na(BOOKTITLE),
str_replace_all(BOOKTITLE, regular_expression, "\\{\\1\\}"),
NA)) |>
df2bib(file = i)
}
})
# GENERATION OF THE RMD ----------------------------------------------------
library(tidyverse)
# remove everything that starts with number and ends with Rmd --------------
file.remove(grep("\\d{1,}_.*.Rmd", list.files(), value = TRUE))
# read our fetures data ----------------------------------------------------
readxl::read_xlsx("data/contributors.xlsx") |>
filter(render == 1) |>
mutate(created_date = as.integer(created_date)) ->
features
# deal with major topics --------------------------------------------------
features |>
filter(!is.na(major_topic_text)) |>
pull(filename) ->
major_topics
features |>
filter(is.na(major_topic_text)) ->
features
file.copy(str_c("data/orig_rmd/", major_topics, ".Rmd"),
str_c(major_topics, ".Rmd"),
overwrite = TRUE)
# create variable with leading 0 -------------------------------------------
# remove +1 when we will have more then 100 topics
features$id_0 <- sprintf(paste0("%0", nchar(max(features$id))+1, "d_"),
features$id)
features |>
mutate(filename = str_c(filename, "_map")) |>
bind_rows(features) ->
features
# create Rmd names ---------------------------------------------------------
rmd_filenames <- c(str_c(features$id_0, features$filename, ".Rmd"))
# create key for bibtex ----------------------------------------------------
first_authors <- tolower(str_remove(map(str_split(features$author, " "), 2), ","))
# create orig_rmd/..._map.Rmd files ----------------------------------------------------
walk(rmd_filenames[str_detect(rmd_filenames, "_map.Rmd")], function(i){
read_tsv(str_c("data/orig_table/",
str_remove(str_remove(i, "_map.Rmd"), "\\d{1,}_"),
".tsv"),
progress = FALSE, show_col_types = FALSE) |>
select(matches("^value\\d{1,}_")) |>
distinct() |>
pivot_longer(names_to = "values", values_to = "titles", everything()) |>
mutate(values = as.double(str_extract(values, "\\d{1,}"))) ->
multiple_values
write_lines(
c("
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning=FALSE, fig.width = 9.5)
library(tidyverse)
library(lingtypology)
```
## {-}
Visualization 1 (**General datapoints**) and 3 (**Extrapolated data**) are both based on the principle 'one value – one language'. Visualization 2 (**Data granularity**) takes into account dialect levels. On the **Data granularity** maps you can see the village and the data type (village data, general language data, etc.) when you click on a dot. Hover over or click on a dot to see the language. By unticking the box “show languages” you can remove the inner dots and visualize the distribution of different values in the area without the distraction of genealogical information.
```{r}
",
str_c('read_tsv("../orig_table/',
str_remove(str_remove(i, "_map.Rmd"), "\\d{1,}_"),
'.tsv", show_col_types = FALSE, guess_max = 2000) |>'),
" mutate(type = case_when(type == 'language' ~ 'language',
type == 'dialect_toplevel' ~ 'top level dialect',
type == 'dialect_nt1' ~ 'non top level 1 dialect',
type == 'dialect_nt2' ~ 'non top level 2 dialect',
type == 'dialect_nt3' ~ 'non top level 3 dialect',
type == 'village' ~ 'village dialect')) |>
filter(map != 'no') ->
feature_dataset
read_tsv('../tald_villages.csv', show_col_types = FALSE, guess_max = 2000) |>
select(village, rus_village, lat, lon, gltc_lang, lang, aff, family, standard, default_level, dialect_toplevel, dialect_nt1, dialect_nt2, dialect_nt3, village_dialect, lang_col, aff_col) |>
pivot_longer(names_to = 'type', values_to = 'idiom', standard:village_dialect) |>
filter(!is.na(idiom)) |>
mutate(type = case_when(type == 'standard' ~ 'language',
type == 'default_level' ~ 'language',
type == 'dialect_toplevel' ~ 'top level dialect',
type == 'dialect_nt1' ~ 'non top level 1 dialect',
type == 'dialect_nt2' ~ 'non top level 2 dialect',
type == 'dialect_nt3' ~ 'non top level 3 dialect',
type == 'village_dialect' ~ 'village dialect')) ->
coordinates
coordinates |>
distinct(gltc_lang) |>
mutate(lang4map = lang.gltc(gltc_lang)) ->
lang4map
coordinates |>
group_by(idiom, type, gltc_lang, lang_col) |>
reframe(lat = mean(lat),
lon = mean(lon)) |>
left_join(lang4map)->
coordinates_averaged
feature_dataset |>
mutate(idiom = str_remove(idiom, 'Standard ')) |>
inner_join(coordinates, by = c('type', 'idiom', 'lang'), relationship = 'many-to-many') |>
left_join(lang4map) |>
mutate(display = 'show languages') ->
all_data
```
",
map(multiple_values$values, function(i){
str_c(
"## ", multiple_values$titles[i], "{.tabset .tabset-fade .tabset-pills #m", multiple_values$values[i], "}
### General datapoints {-}
```{r}
feature_dataset |>
filter(map == 'yes',
genlang_point == 'yes') |>
add_count(value",
multiple_values$values[i],
") |>
inner_join(coordinates_averaged) |>
mutate(popup = ifelse((lang4map == idiom | str_detect(idiom, 'Standard')),
str_c('data level: ', type),
str_c(idiom, '<br> data level: ', type)),
value1 = str_c(value1, ' (', n, ')'),
display = 'show languages') |>
filter(!is.na(value",
multiple_values$values[i],
"),
!is.na(lang4map)) ->
general_datapoints_map
map.feature(general_datapoints_map$lang4map,
latitude = general_datapoints_map$lat,
longitude = general_datapoints_map$lon,
features = general_datapoints_map$value",
multiple_values$values[i],
",
title = general_datapoints_map$value",
multiple_values$values[i],
"_name[1],
color = 'magma',
stroke.features = 'a',
stroke.color = 'black',
stroke.radius = 1,
stroke.legend = FALSE,
tile = 'Esri.WorldGrayCanvas',
legend = TRUE,
legend.position = 'bottomleft',
zoom.control = TRUE,
width = 8) %>%
map.feature(general_datapoints_map$lang4map,
latitude = general_datapoints_map$lat,
longitude = general_datapoints_map$lon,
features = general_datapoints_map$lang,
color = general_datapoints_map$lang_col,
tile = 'Esri.WorldGrayCanvas',
legend = FALSE,
width = 5,
pipe.data = .,
control = general_datapoints_map$display)
```
### Extrapolated data {-}
```{r}
all_data |>
filter(!is.na(value",
multiple_values$values[i],
")) ->
all_data_filtered
map.feature(all_data_filtered$lang4map,
latitude = all_data_filtered$lat,
longitude = all_data_filtered$lon,
features = all_data_filtered$value",
multiple_values$values[i],
",
color = 'magma',
stroke.features = 'a',
stroke.color = 'black',
stroke.radius = 1,
stroke.legend = FALSE,
tile = 'Esri.WorldGrayCanvas',
title = all_data_filtered$value",
multiple_values$values[i],
"_name[1],
legend = TRUE,
legend.position = 'bottomleft',
label = all_data_filtered$lang,
zoom.control = TRUE,
width = 8,
popup = paste(all_data_filtered$village, '|',
all_data_filtered$rus_village, '<br>',
'data:', all_data_filtered$type)) %>%
map.feature(all_data_filtered$lang4map,
latitude = all_data_filtered$lat,
longitude = all_data_filtered$lon,
features = all_data_filtered$lang,
color = all_data_filtered$lang_col,
tile = 'Esri.WorldGrayCanvas',
legend = FALSE,
width = 5,
pipe.data = .,
control = all_data_filtered$display,
popup = paste(all_data_filtered$village, '|',
all_data_filtered$rus_village, '<br>',
'data:', all_data_filtered$type))
```
### Data granularity {-}
```{r}
feature_dataset |>
filter(map == 'yes') |>
inner_join(coordinates_averaged) |>
mutate(popup = ifelse((lang4map == idiom | str_detect(idiom, 'Standard')),
str_c('data level: ', type),
str_c(idiom, '<br> data level: ', type)),
type = factor(type, levels = c('language',
'top level dialect',
'non top level 1 dialect',
'non top level 2 dialect',
'non top level 3 dialect',
'village dialect'))) |>
filter(!is.na(value",
multiple_values$values[i],
"),
!is.na(lang4map)) |>
arrange(type) ->
data_granularity_map
map.feature(data_granularity_map$lang4map,
latitude = data_granularity_map$lat,
longitude = data_granularity_map$lon,
features = data_granularity_map$value",
multiple_values$values[i],
",
color = 'magma',
stroke.features = 'a',
stroke.color = 'black',
stroke.radius = 1,
stroke.legend = FALSE,
tile = 'Esri.WorldGrayCanvas',
title = data_granularity_map$value",
multiple_values$values[i],
"_name[1],
legend = TRUE,
legend.position = 'bottomleft',
label = data_granularity_map$lang,
zoom.control = TRUE,
control = data_granularity_map$type,
popup = data_granularity_map$popup)
```
")
}),
"
## Datatable
",
str_c('[Download](https://raw.githubusercontent.com/LingConLab/TALD/master/data/orig_table/',
str_remove(str_remove(i, "_map.Rmd"), "\\d{1,}_"),
'.tsv) the whole dataset.'),
"
```{r}
bib <- RefManageR::ReadBib(file = '../bibliography.bib')
feature_dataset |>
select(str_which(colnames(feature_dataset), 'value\\\\d{1,}_name$')) |>
pivot_longer(cols = everything()) |>
distinct() |>
mutate(name = str_remove(name, '_name')) ->
columns_rename
feature_dataset |>
select(lang, idiom, source, page, matches('value\\\\d{1,}$')) |>
filter(!if_all(matches('value\\\\d{1,}$'), is.na)) |>
rename_with(function(x){columns_rename$value[match(x, columns_rename$name)]}, matches('value\\\\d{1,}$')) |>
rename(Language=lang,
Idiom = idiom,
Source = source) |>
mutate(page = str_replace_all(page, '--', '–'),
Source = str_split(Source, '; '),
page = str_split(page, '; ')) |>
unnest_longer(col = c(Source, page)) |>
rowwise() |>
mutate(page = ifelse(page == 'NA', NA_character_, page),
Source = case_when(str_detect(Source, '[Ff]ield [Dd]ata') ~ Source,
str_detect(Source, 'p.\\\\s?c.$') ~ Source,
TRUE ~ Cite(bib, Source,
after = ifelse(!is.na(page), str_c(': ', page), '')))) |>
select(-page) |>
DT::datatable(class = 'cell-border stripe',
rownames = FALSE,
filter = 'top',
extensions = 'Buttons',
options = list(pageLength = 100,
autoWidth = TRUE,
info = FALSE,
dom = 'fBltp',
buttons = list(list(extend = 'collection',
buttons = c('csv', 'excel', 'pdf'),
text = '<i class=\"fas fa-download\"></i>')),
paginate = TRUE))
```
## References {-}
"),
file = str_c("data/orig_rmd/", str_remove(i, "\\d{1,}_"))
)
})
# create Rmd files ---------------------------------------------------------
options(ymlthis.rmd_body = "
```{r, include = FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, comment = '')
library(lingglosses)
```
")
walk(seq_along(rmd_filenames), function(i){
ymlthis::yml_empty() |>
ymlthis::yml_title(ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
str_c(features$title[i], " (Maps & Data)"),
features$title[i])) |>
ymlthis::yml_author(features$author[i]) |>
ymlthis::yml_date(paste0('Last update: ',
ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
features$updated_map[i],
features$updated_text[i]))) |>
ymlthis::yml_citations(bibliography = paste0("./data/orig_bib/",
str_remove(features$filename[i], "_map"),
".bib"),
csl = "apa.csl",
link_citations = TRUE) |>
ymlthis::yml_output(html_document(number_sections = TRUE,
anchor_sections = TRUE,
pandoc_args = "--shift-heading-level-by=-1")) |>
ymlthis::use_rmarkdown(path = rmd_filenames[i],
open_doc = FALSE,
quiet = TRUE,
include_body = FALSE,
body = NULL)
write_lines(c(
# add link to map/chapter page
paste0("See [",
ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
"chapter",
"data and maps"),
"](",
str_remove(rmd_filenames[i], "(_map)?.Rmd"),
ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
".html)",
"_map.html).")),
"",
# create and add citation
"```{r}",
"library(RefManageR)",
"BibOptions(check.entries = FALSE, style = 'text', bib.style = 'authoryear')",
"article_citation <- BibEntry(bibtype = 'Incollection', ",
paste0(" key='", first_authors[i], features$created_date[i], "',"),
paste0(" title='",
ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
str_c(features$title[i], " (Maps & Data)"),
features$title[i]),
"',"),
paste0(" author='", str_replace(features$author[i], ",", " and"), "',"),
paste0(" year='", features$created_date[i], "',"),
" editor= 'Daniel, Michael and Filatov, Konstantin and Maisak, Timur and Moroz, George and Mukhin, Timofey and Naccarato, Chiara and Verhees, Samira',",
" publisher='Linguistic Convergence Laboratory, NRU HSE',",
" address='Moscow',",
" booktitle= 'Typological Atlas of the Languages of Daghestan (TALD)',",
" url='http://lingconlab.ru/dagatlas',",
" doi='10.5281/zenodo.6807070')",
"```",
"",
"## {.tabset .tabset-fade .tabset-pills -} ",
"",
"### Plain text {-}",
"```{r, results = 'asis'}",
"print(article_citation, .opts = list(style = 'text'))",
"```",
"",
"### BibTeX {-}",
"",
"```{r}",
"print(article_citation, .opts = list(style = 'Bibtex'))",
"```",
# add text of the Rmd
"",
str_c("```{r, child='data/orig_rmd/", features$filename[i], ".Rmd'}"),
"```",
"",
ifelse(str_detect(rmd_filenames[i], "_map.Rmd"),
str_c(
"```{r, results='asis'}\n",
"PrintBibliography(bib)\n",
"```"),
""),
""),
rmd_filenames[i], append = TRUE)
})
# RENDER AND CLEAN ---------------------------------------------------------
rmarkdown::render_site()
beepr::beep()