-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.R
425 lines (387 loc) · 18.1 KB
/
helper.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
########################## Creates the African symbol ##########################
# 0. Initial setup ##########
## Loads packages
library(rnaturalearth)
library(rmapshaper)
library(ggplot2)
## Loads the shapes of the African countries
africa <- rnaturalearth::ne_countries(
continent = "Africa",
returnclass = "sf"
)
# 1. Data handling ##########
## Dissolves the internal boundaries and simplifies the shape
africa <- africa |>
rmapshaper::ms_dissolve() |>
rmapshaper::ms_simplify(keep = 0.4)
## Plots the shape
africa |>
ggplot()
geom_sf(color = NA, fill = "#ffedd6")
coord_sf(xlim = c(-18, 52), ylim = c(-36,38), expand = FALSE)
theme_void()
## Saves the plot
w <- 40
ggsave("www/africa.png", width = w, height = w*(104/70), units = "px")
############################## Shapes the images ##############################
# 0. Initial setup ##########
## Loads packages
library(dplyr)
library(glue)
library(magick)
library(purrr)
library(stringr)
## Lists all original images
original_images <- list.files(path = "originals", pattern = ".png", full.names = TRUE)
## Lists all new images
new_images <- list.files(recursive = TRUE, pattern = ".png", full.names = TRUE)
new_images <- new_images |>
stringr::str_subset("week[:digit:]{2}") |>
stringr::str_subset("_pt.png|week00", negate = TRUE) |>
stringr::str_subset("/. /. /. /", negate = TRUE)
# 1. Images handling ##########
## Defines the final dimensions of thumbnails
dim_num <- 200
dim_str <- as.character(dim_num)
## Creates a new image with white background and a black circle
mask <- magick::image_draw(magick::image_blank(dim_num, dim_num))
symbols(dim_num/2, dim_num/2, circles=(dim_num/2)-3, bg="black", inches=FALSE, add=TRUE)
dev.off()
## Creates a function that adjusts the images
adjuster <- function(img, final_path, thumb) {
### Gets image dimensions
info = magick::image_info(img)
w = info$width
h = info$height
### Defines final dimensions
if (thumb) {
h_std = dim_num
w_std = dim_num
} else {
h_std = 700
w_std = 550
}
### Defines the scale geometry
if (thumb) {
scale = 1
} else {
scale = 550/700
}
if (h/w > scale) {
geom_scales = glue::glue("{w_std}x")
} else {
geom_scales = glue::glue("x{h_std}")
}
### Manipulates the image
img = img |>
magick::image_scale(geometry = geom_scales) |>
magick::image_extent(glue::glue("{w_std}x{h_std}"), color = '#f2f2f2') |>
magick::image_crop(geometry = glue::glue("{w_std}x{h_std}"),
gravity = "Center")
### Creates circular versions for the thumbnails
if (thumb) {
#### Creates an image composite using the image and the circular mask
img = magick::image_composite(img, mask, operator = "copyopacity")
#### Sets the background as transparent
img = magick::image_background(img, "transparent")
}
### Saves the image
magick::image_write(img, final_path)
### Forces the memory cleaning process
gc(verbose = FALSE)
}
## Scales and crops the original images
original_images |>
purrr::walk(function (path) {
### Reads the image
img = magick::image_read(path)
### Creates the path to the final image
path = stringr::str_remove(path, "originals/")
final_path = glue::glue("www/dubois/{path}")
### Adjusts the image
adjuster(img, final_path, FALSE)
adjuster(img, glue::glue("www/dubois/thumb_{path}"), TRUE)
})
## Scales and crops the new images
new_images |>
purrr::walk(function (path) {
### Reads the image
img = magick::image_read(path)
### Creates the path to the final image
year = stringr::str_extract(path, "/[:digit:]{4}/") |>
stringr::str_remove_all("/") |>
stringr::str_sub(3, 4)
week = stringr::str_extract(path, "/week[:digit:]{2}/") |>
stringr::str_remove_all("/|week")
path = glue::glue("y{year}wk{week}.png")
final_path = glue::glue("www/new/{path}")
### Adjusts the image
adjuster(img, final_path, FALSE)
adjuster(img, glue::glue("www/new/thumb_{path}"), TRUE)
})
########## Creates an JS object that has the database for the images ##########
# 0. Initial setup ##########
## Loads packages
library(dplyr)
library(glue)
library(jsonlite)
library(tidyr)
# 1. Data handling ##########
## Creates a tibble with the data
df <- dplyr::tibble(
ids = c(
"y22wk01", "y22wk02", "y22wk03", "y22wk04", "y22wk05",
"y22wk06", "y22wk07", "y22wk08", "y22wk09", "y22wk10",
"y23wk01", "y23wk02", "y23wk03", "y23wk04", "y23wk05",
"y23wk06", "y23wk07", "y23wk08", "y23wk09", "y23wk10",
"y24wk01", "y24wk03", "y24wk05", "y24wk06"
),
`original-title` = c(
"THE GEORGIA NEGRO",
"ASSESSED VALUATION OF ALL TAXABLE PROPERTY OWNED BY GEORGIA NEGROES",
"RELATIVE NEGRO POPULATION OF THE STATES OF THE UNITED STATES",
"VALUATION OF TOWN AND CITY PROPERTY OWNED BY GEORGIA NEGROES",
"SLAVES AND FREE NEGROES",
"ILLITERACY",
"CONJUGAL CONDITION OF AMERICAN NEGROES ACCORDING TO AGE PERIODS",
"ASSESSED VALUE OF HOUSEHOLD AND KITCHEN FURNITURE OWNED BY GEORGIA NEGROES",
"NUMBER OF NEGRO STUDENTS TAKING THE VARIOUS COURSES OF STUDY OFFERED IN GEORGIA SCHOOLS",
"PROPORTION OF TOTAL NEGRO CHILDREN OF SCHOOL AGE WHO ARE ENROLLED IN THE PUBLIC SCHOOLS",
"INCOME AND EXPENDITURE OF 150 NEGRO FAMILIES IN ATLANTA, GA., U.S.A.",
"VALUE OF LAND OWNED BY GEORGIA NEGROES",
"CITY AND RURAL POPULATION 1890",
"DISTRIBUTION OF NEGROES IN THE UNITED STATES",
"NEGRO BUSINESS MEN IN THE UNITED STATES",
"CRIME AMONG AMERICAN NEGROES",
"OCCUPATIONS OF NEGROES AND WHITES IN GEORGIA",
"COMPARATIVE RATE OF INCREASE OF THE WHITE AND NEGRO ELEMENTS OF THE POPULATION OF THE UNITED STATES",
"OCCUPATIONS IN WHICH AMERICAN NEGROES ARE ENGAGED",
"MIGRATION OF NEGROES",
"NEGRO POPULATION OF GEORGIA BY COUNTIES",
"ACRES OF LAND OWNED BY NEGROES IN GEORGIA",
"RACE AMALGAMATION IN GEORGIA",
"THE AMALGAMATION OF THE WHITE AND BLACK ELEMENTS OF THE POPULATION IN THE UNITED STATES"
),
`new-title` = c(
"THE AFRO-BRAZILIANS",
"REAL MONTHLY INCOME PER CAPITA OF BLACK HOUSEHOLDS IN BRAZIL",
"ACCESS OF BLACK PEOPLE TO A PERSONAL MOBILE PHONE AND INTERNET IN THE STATES OF BRAZIL",
"REAL INCOME OF BLACK PEOPLE WHOSE MAIN WORK IS INFORMAL",
"PARTICIPATION IN MANAGERIAL POSITIONS BY RACE IN BRAZIL",
"ILLITERACY RATE OF BLACK PEOPLE IN BRAZIL",
"CAPITAL OF CANDIDATURES FOR THE LOWER HOUSE OF THE BRAZILIAN CONGRESS BY RACE AND STATE",
"RATE OF HOMICIDES OF BLACKS BY PER 100K PEOPLE",
"OCCUPIED BLACKS PER 1000 PEOPLE BY EDUCATIONAL LEVEL IN BRAZIL",
"PROPORTION OF BLACKS AND NON-BLACKS CANDIDATES AND ELECTED FOR THE LOWER HOUSE OF THE BRAZILIAN CONGRESS",
"INCOME AND EXPENDITURE OF BRAZILIAN FAMILIES",
"REAL AVERAGE INCOME OF BLACK BRAZILIANS",
"HOMICIDE VICTIMS BY RACE AND SEX",
"DISTRIBUTION OF BLACKS IN BRAZIL",
"RIO SAMBA SCHOOLS PARADE WINNERS",
"INCARCERATED PEOPLE BY RACE IN BRAZIL",
"HIGHER EDUCATION ENROLLMENTS BY RACE AND FIELD",
"GRADUATES BY RACE IN BRAZIL",
"FINANCES OF BLACK CANDIDATURES FOR MAYOR BY SEX IN BRAZIL",
"MIGRATION IN THE BRAZILIAN STATES",
"BLACK POPULATION OF BAHIA BY IMMEDIATE REGION",
"PERCENTAGE OF BLACKS IN EXTREME POVERTY",
"SAMBAS IN WHICH AFRO-BRAZILIAN TERMS APPEAR",
"THE BLACK AND WHITE ELEMENTS OF THE BRAZILIAN POPULATION"
),
packages = list(
list("dplyr", "geomtextpath", "ggbump", "ggfx", "ggplot2",
"ggtext", "readxl", "rmapshaper", "rnaturalearth",
"scales", "sf", "showtext", "sysfonts", "tidyr"),
list("dplyr", "ggforce", "ggplot2", "ggtext", "glue",
"scales", "showtext", "sysfonts"),
list("dplyr", "ggplot2", "ggtext", "glue", "patchwork",
"santoku", "sf", "showtext", "stringr", "sysfonts", "tidyr"),
list("dplyr", "emojifont", "ggplot2", "ggtext",
"glue", "showtext", "sysfonts"),
list("dplyr", "ggplot2", "ggtext", "glue",
"scales", "showtext", "sysfonts"),
list("dplyr", "ggforce", "ggplot2", "glue",
"scales", "showtext", "sysfonts"),
list("dplyr", "ggplot2", "ggtext", "glue", "scales", "seriation",
"showtext", "stringr","sysfonts", "tidyr"),
list("dplyr", "ggplot2", "ggtext", "glue", "purrr",
"scales", "showtext", "sysfonts", "tidyr"),
list("dplyr", "ggforce", "ggplot2", "ggtext", "glue",
"purrr", "scales", "showtext", "sysfonts", "tidyr"),
list("dplyr", "ggtext", "glue", "showtext", "sysfonts", "tidyr"),
list("dplyr", "forcats", "ggplot2", "ggtext", "ggview",
"glue", "junebug", "metR", "readxl", "scales",
"shadowtext", "stringr", "systemfonts"),
list("dplyr", "ggforce", "ggplot2", "ggtext", "ggview", "junebug",
"purrr", "readxl", "scales", "systemfonts", "tidyr"),
list("dplyr", "geomtextpath", "ggborderline","ggplot2", "ggtext",
"ggview", "glue", "junebug", "readxl", "scales", "systemfonts"),
list("dplyr", "geobr", "ggbeeswarm", "ggplot2", "ggforce", "ggtext",
"ggview", "junebug", "purrr", "readxl", "rmapshaper",
"santoku", "scales", "systemfonts", "tidyr"),
list("colorspace", "dplyr", "ggplot2", "ggtext", "ggview", "glue",
"junebug", "readxl", "scales", "stringr", "systemfonts"),
list("colorspace", "dplyr", "ggfx", "ggplot2", "ggtext",
"ggview", "glue", "junebug", "purrr", "readr",
"scales", "stringr", "systemfonts", "tidyr"),
list("dplyr", "ggforce", "ggplot2", "ggtext", "ggview", "glue", "junebug",
"purrr", "readxl", "scales", "stringr", "systemfonts", "tidyr"),
list("colorspace", "dplyr", "ggborderline", "ggplot2", "ggtext",
"ggview", "junebug", "readr", "scales", "systemfonts"),
list("dplyr", "forcats", "ggplot2", "ggtext", "ggview",
"glue","junebug", "readxl", "scales", "systemfonts"),
list("dplyr", "geobr", "geogrid", "ggnewscale", "ggplot2", "ggtext",
"ggview", "glue", "junebug", "metR", "purrr", "readxl",
"santoku", "scales", "sf", "stringi", "systemfonts", "tidyr"),
list("dplyr", "geobr", "ggbeeswarm", "ggplot2", "ggtext", "ggview",
"junebug", "patchwork", "purrr", "readxl", "rmapshaper",
"santoku", "scales", "systemfonts", "tidyr"),
list("dplyr", "forcats", "ggplot2", "ggtext", "ggview",
"glue", "junebug", "readxl", "scales", "systemfonts"),
list("dplyr", "ggplot2", "ggtext", "ggview", "glue",
"junebug", "patchwork", "systemfonts", "stringr"),
list("dplyr", "forcats", "ggplot2", "ggtext", "ggview", "glue",
"junebug", "readxl", "scales", "systemfonts", "tidyr")
),
downloads = list(
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week01/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk01.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week01/enslaved.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week02/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk02.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week02/income.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week03/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk03.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week03/access.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week04/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk04.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week04/incomes.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week05/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk05.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week05/managers.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week06/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk06.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week06/illiteracy.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week07/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk07.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week07/congress.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week08/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk08.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week08/violence.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week09/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk09.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week09/job.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2022/week10/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y22wk10.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2022/week10/elections.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week01/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk01.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week01/expenses.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week02/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk02.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week02/income.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week03/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk03.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week03/victims.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week04/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk04.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week04/distribution.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week05/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk05.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week05/samba_champions.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week06/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk06.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week06/incarcerated.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week07/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk07.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week07/enrollments.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week08/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk08.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week08/graduates.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week09/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk09.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week09/mayors.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2023/week10/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y23wk10.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2023/week10/migration.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2024/week01/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y24wk01.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2024/week01/week01.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2024/week03/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y24wk03.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2024/week03/week03.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2024/week05/data.csv",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y24wk05.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2024/week05/week05.png"
),
list(
"https://github.com/IcaroBernardes/webdubois/raw/main/2024/week06/data.xlsx",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/originals/y24wk06.png",
"https://raw.githubusercontent.com/IcaroBernardes/webdubois/main/2024/week06/week06.png"
)
)
)
## Creates a list-column for each image id
df <- df |>
dplyr::group_by(ids) |>
tidyr::nest()
## Creates a named list with the original data
listed <- df |> dplyr::pull(data)
names(listed) <- df |> dplyr::pull(ids)
## Creates the JS object as a string
listed <- listed |> jsonlite::toJSON(pretty = TRUE)
## Adds the 'var' declaration. Makes it a global Array
listed <- glue::glue("var detailsDATA = {listed}")
## Adds another global Array just for the IDs
ids <- df$ids |> jsonlite::toJSON(pretty = TRUE)
ids <- glue::glue("var imageids = {ids}")
listed <- glue::glue("{listed}\n{ids}")
## Writes the JS file with the data
fileConn <- file("www/js/dataset.js")
writeLines(listed, fileConn)
close(fileConn)