Add ci workflow

Change-Id: Id9a4c7380367d55810dfccd7a62bce6b1b0ff17a
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..1cb7a22
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,45 @@
+# use the verse rocker image, as it contains tidyverse, devtools and some texlive
+image: rocker/verse
+
+# define stages of runner. at the moment,
+# just build (no test or deploy).
+stages:
+  - build
+
+build-job:
+  stage: build
+
+  cache:
+    key: icc
+    paths:
+      - ./cache
+
+  artifacts:
+    paths:
+      - "target/*"
+
+  before_script:
+    - source `find .. -name section_helper.sh`
+
+    - start_section install_linux_packages "Installing missing Linux packages"
+    - apt-get update
+    - apt-get install -y build-essential libglpk40 libcurl4-gnutls-dev libxml2-dev libsodium-dev libsecret-1-dev libfontconfig1-dev libssl-dev libxt6 libpq-dev imagemagick
+    - locale-gen de_DE.utf8 en_GB.utf8 en_US.utf8
+
+    - end_section install_linux_packages
+
+    - start_section install_fonts "Installing Libertinus and Fira fonts"
+    - wget -O- https://github.com/mozilla/Fira/archive/refs/tags/4.202.tar.gz | tar -C /usr/share/fonts -zx --wildcards "*.otf"
+    - fc-cache
+    - end_section install_fonts
+
+    - start_section install_r_packages "Installing missing R packages"
+    - R -e "install.packages(c('RKorAPClient', 'httr', 'tidytext', 'httpuv', 'scales', 'sp', 'raster', 'kableExtra', 'svglite'))"
+    - R -e 'devtools::install_git("https://korap.ids-mannheim.de/gerrit/IDS-Mannheim/idsThemeR")'
+    - end_section install_r_packages
+
+  script:
+    - start_section render "Running scripts"
+    - R -f ./R/icc_stats.R
+    - end_section render
+
diff --git a/R/icc_stats.R b/R/icc_stats.R
index d775964..a1d8374 100644
--- a/R/icc_stats.R
+++ b/R/icc_stats.R
@@ -47,9 +47,7 @@
 
 icc <- icc %>%
   rowwise() %>%
-  mutate(token = icc_token(lang, app_id)) %>%
-  add_column(tokens=0)
-
+  mutate(token = icc_token(lang, app_id))
 
 genre <- c("Blog",
              "Creative:Novels_ShortStories",
@@ -73,11 +71,19 @@
   rowwise() %>%
   mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
 
-icc_genre %>% ggplot(aes(x=lang, fill=genre, y=tokens)) +
+plot <- icc_genre %>% ggplot(aes(x=lang, fill=genre, y=tokens)) +
   geom_col() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
   theme_ids() +
   geom_text(aes(label=if_else(tokens > 0, as.character(tokens), ""), y=tokens), position= position_stack(reverse = F, vjust = 0.5), color="white", size=3.2, family="Fira Sans Condensed")
 
+ggsave("target/tokens_per_genre.png", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+ggsave("target/tokens_per_genre.svg", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+ggsave("target/tokens_per_genre.pdf", device = cairo_pdf, width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+
+if(rstudioapi::isAvailable()) {
+  print(plot)
+}
+
 year <- c(1988:2022)
 
 icc_year <- icc %>%
@@ -86,6 +92,15 @@
   rowwise() %>%
   mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
 
-icc_year %>% ggplot(aes(x=year, fill=lang, color=lang, y=tokens)) +
-  geom_line() + geom_point() + geom_area() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
+plot <- icc_year %>% ggplot(aes(x=year, fill=lang, color=lang, y=tokens)) +
+  geom_line() + geom_point() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
   theme_ids()
+
+ggsave("target/tokens_per_year.png", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+ggsave("target/tokens_per_year.svg", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+ggsave("target/tokens_per_year.pdf", device = cairo_pdf, width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
+
+if(rstudioapi::isAvailable()) {
+  print(plot)
+}
+
diff --git a/ci/section_helper.sh b/ci/section_helper.sh
new file mode 100644
index 0000000..ddefe0d
--- /dev/null
+++ b/ci/section_helper.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+# Reference: https://docs.gitlab.com/ee/ci/jobs/#custom-collapsible-sections
+
+#
+# Takes 2 Parameters a new section id and a heading/title
+#
+function start_section() {
+  id=$1
+  title=$2
+  echo -e "\e[0Ksection_start:$(date +%s):${id}[collapsed=true]\r\e[0K\e[36;1m${title}\e[0m"
+}
+
+#
+# Takes 1 Parameter, the unique section id of the section that should end
+#
+function end_section() {
+  id=$1
+  echo -e "\e[0Ksection_end:$(date +%s):${id}\r\e[0K"
+}