Short War Story in the Code Narrative

Grzegorz Smoliński

Introduction

The table below utilizes {tableboom} functionality to tell the story about the war and about the code.

There is also a tutorial in the form of vignette about some aspects of metaprogramming used during developing the package: Treating Code As Data - Notes.

Table

children_from_ukr_temp_prot_eu.R
1
library(dplyr)
≀ ⊳ library(dplyr)
3

4
path <- file.path(system.file(package = "tableboom","table_contest_2022", "inner_script"),
"eurostat_data.R")
≀ file.path(system.file(...

≀ ⊳ system.file(package = "tableboom", "table_contest_2022", "inner_script")

chr "C:/Users/gsmolinski/Documents/R/R-4.2.1/library/tableboom/table_contest_2022/inner_script"

⊳ file.path(system.file(

package = "tableboom", "table_contest_2022",
"inner_script"
), "eurostat_data.R")
chr "C:/Users/gsmolinski/Documents/R/R-4.2.1/library/tableboom/table_contest_2022/inner_script/eurostat_data.R"
6
source(path) # data - children (< 18) from Ukraine which found temp protection
≀ ⊳ source(path)
8

9
names(child_ukr_prot) <- c("country", "march_2022", "april_2022",
"may_2022","june_2022", "july_2022", "august_2022")
≀ ⊳ c(

"country", "march_2022", "april_2022", "may_2022", "june_2022",
"july_2022", "august_2022"
)
chr [1:7] "country" "march_2022" "april_2022" "may_2022" "june_2022" ...
11

12
child_ukr_prot[5, "country"] <- stringi::stri_replace_all_regex(child_ukr_prot[5, "country"],
"\\s+.+", "")
≀ stringi::stri_replace_all_regex(...

≀ ⊳ child_ukr_prot[5, "country"]

chr "Germany (until 1990 former territory of the FRG)"

⊳ stringi::stri_replace_all_regex(

child_ukr_prot[5, "country"],
"\\s+.+", ""
)
chr "Germany"
14

15

16
child_ukr_prot <- child_ukr_prot |>
mutate(across(matches("\\d$"), \(e) if_else(e == ":", NA_character_, e)),
across(matches("\\d$"), as.integer))
≀ mutate(child_ukr_prot, across(matches("\\d$"), function(e) {...

≀ function(e) if_else(e == ":", NA_character_, e)

⊳ function(e) if_else(e == ":", NA_character_, e)

function (e)
- attr(*, "srcref")= 'srcref' int [1:8] 15 34 15 73 34 73 15 15
- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000189fc0b9f60>

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE FALSE FALSE TRUE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "10155" "12685" "96740" "475" NA "6165" NA "0" "12975" NA ...

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE TRUE FALSE TRUE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "3925" "24555" NA "3685" NA "3630" NA "3075" "18505" NA "2135" ...

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE TRUE FALSE TRUE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "2190" "7175" NA "3845" NA "1005" NA "1870" "10810" NA "830" ...

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE TRUE FALSE TRUE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "1165" "1790" NA "1530" NA "620" NA "490" "3435" NA "455" ...

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE TRUE FALSE TRUE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "860" "1850" NA "435" NA "740" NA "265" "1660" NA "395" "3870" ...

≀ if_else(e == ":", NA_character_, e)

≀ ⊳ e == ":"

logi [1:31] FALSE FALSE TRUE FALSE FALSE FALSE ...

⊳ if_else(e == ":", NA_character_, e)

chr [1:31] "795" "1030" NA "460" "18545" "775" "0" "230" "1615" NA "225" ...

⊳ mutate(child_ukr_prot, across(matches("\\d$"), function(e) {

if_else(e ==
":", NA_character_, e)
}), across(matches("\\d$"), as.integer))
Rows: 31
Columns: 7
$ country <chr> "Belgium", "Bulgaria", "Czechia", "Denmark", "Germany", "E…
$ march_2022 <int> 10155, 12685, 96740, 475, NA, 6165, NA, 0, 12975, NA, 1875…
$ april_2022 <int> 3925, 24555, NA, 3685, NA, 3630, NA, 3075, 18505, NA, 2135…
$ may_2022 <int> 2190, 7175, NA, 3845, NA, 1005, NA, 1870, 10810, NA, 830, …
$ june_2022 <int> 1165, 1790, NA, 1530, NA, 620, NA, 490, 3435, NA, 455, 964…
$ july_2022 <int> 860, 1850, NA, 435, NA, 740, NA, 265, 1660, NA, 395, 3870,…
$ august_2022 <int> 795, 1030, NA, 460, 18545, 775, 0, 230, 1615, NA, 225, NA,…
18
maxs <- vector("integer", ncol(child_ukr_prot) - 1)
≀ vector("integer", ncol(child_ukr_prot) - 1)

≀ ncol(child_ukr_prot) - 1

≀ ⊳ ncol(child_ukr_prot)

int 7

⊳ ncol(child_ukr_prot) - 1

num 6

⊳ vector("integer", ncol(child_ukr_prot) - 1)

int [1:6] 0 0 0 0 0 0
20

21

22
for (i in 2:(ncol(child_ukr_prot))) {
maxs[[i - 1]] <- max(child_ukr_prot[[i]], na.rm = TRUE)
}
≀ 2:(ncol(child_ukr_prot))

≀ ⊳ ncol(child_ukr_prot)

int 7

⊳ 2:(ncol(child_ukr_prot))

int [1:6] 2 3 4 5 6 7

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 10155 12685 96740 475 NA 6165 NA 0 12975 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 361565

≀ ⊳ i - 1

num 1

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 3925 24555 NA 3685 NA 3630 NA 3075 18505 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 189630

≀ ⊳ i - 1

num 2

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 2190 7175 NA 3845 NA 1005 NA 1870 10810 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 30170

≀ ⊳ i - 1

num 3

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 1165 1790 NA 1530 NA 620 NA 490 3435 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 15445

≀ ⊳ i - 1

num 4

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 860 1850 NA 435 NA 740 NA 265 1660 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 15615

≀ ⊳ i - 1

num 5

≀ max(child_ukr_prot[[i]], na.rm = TRUE)

≀ ⊳ child_ukr_prot[[i]]

int [1:31] 795 1030 NA 460 18545 775 0 230 1615 NA ...

⊳ max(child_ukr_prot[[i]], na.rm = TRUE)

int 19120

≀ ⊳ i - 1

num 6
24
maxs
int [1:6] 361565 189630 30170 15445 15615 19120
26
total_months <- colSums(child_ukr_prot[, 2:ncol(child_ukr_prot)], na.rm = TRUE)
≀ colSums(child_ukr_prot[, 2:ncol(child_ukr_prot)], na.rm = TRUE)

≀ child_ukr_prot[, 2:ncol(child_ukr_prot)]

≀ 2:ncol(child_ukr_prot)

≀ ⊳ ncol(child_ukr_prot)

int 7

⊳ 2:ncol(child_ukr_prot)

int [1:6] 2 3 4 5 6 7

⊳ child_ukr_prot[, 2:ncol(child_ukr_prot)]

Rows: 31
Columns: 6
$ march_2022 <int> 10155, 12685, 96740, 475, NA, 6165, NA, 0, 12975, NA, 1875…
$ april_2022 <int> 3925, 24555, NA, 3685, NA, 3630, NA, 3075, 18505, NA, 2135…
$ may_2022 <int> 2190, 7175, NA, 3845, NA, 1005, NA, 1870, 10810, NA, 830, …
$ june_2022 <int> 1165, 1790, NA, 1530, NA, 620, NA, 490, 3435, NA, 455, 964…
$ july_2022 <int> 860, 1850, NA, 435, NA, 740, NA, 265, 1660, NA, 395, 3870,…
$ august_2022 <int> 795, 1030, NA, 460, 18545, 775, 0, 230, 1615, NA, 225, NA,…

⊳ colSums(child_ukr_prot[, 2:ncol(child_ukr_prot)], na.rm = TRUE)

Named num [1:6] 579495 325955 103950 47280 34600 ...
- attr(*, "names")= chr [1:6] "march_2022" "april_2022" "may_2022" "june_2022" ...
28
format(sum(total_months), big.mark = " ")
≀ format(sum(total_months), big.mark = " ")

≀ ⊳ sum(total_months)

num 1144425

⊳ format(sum(total_months), big.mark = " ")

chr "1 144 425"
30
path()
≀ ⊳ path()

Error: simpleError/error/condition
children_from_ukr_temp_prot_eu.R
# install.packages("devtools")
# devtools::install_github("gsmolinski/tableboom")

dir_path <- system.file(package = "tableboom", "table_contest_2022")

script_to_inspect <- file.path(dir_path, "children_from_ukr_temp_prot_eu.R")

tableboom::inspect_r(script_to_inspect, id = "tableboom")
library(dplyr)

path <- file.path(system.file(package = "tableboom","table_contest_2022", "inner_script"),
                  "eurostat_data.R")
# Unfortunately, comments in separate lines are not included
source(path) # data - children (< 18) from Ukraine which found temp protection

names(child_ukr_prot) <- c("country", "march_2022", "april_2022",
                           "may_2022","june_2022", "july_2022", "august_2022")

child_ukr_prot[5, "country"] <- stringi::stri_replace_all_regex(child_ukr_prot[5, "country"],
                                                                "\\s+.+", "")

child_ukr_prot <- child_ukr_prot |>
  mutate(across(matches("\\d$"), \(e) if_else(e == ":", NA_character_, e)),
         across(matches("\\d$"), as.integer))

maxs <- vector("integer", ncol(child_ukr_prot) - 1)

for (i in 2:(ncol(child_ukr_prot))) {
  maxs[[i - 1]] <- max(child_ukr_prot[[i]], na.rm = TRUE)
}

maxs

total_months <- colSums(child_ukr_prot[, 2:ncol(child_ukr_prot)], na.rm = TRUE)

format(sum(total_months), big.mark = " ")

path()