From 933d5a244dc17be3b3de0974a00aef45d0ca1817 Mon Sep 17 00:00:00 2001 From: flavis Date: Wed, 28 Jul 2021 20:07:55 +0200 Subject: [PATCH] add read / write from / to csv and move checking directory to helpers --- .gitignore | 1 + R/fetch.R | 13 +------------ R/helpers.R | 15 +++++++++++++++ R/parse.R | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 1373f18..6373193 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /reports/ !/reports/*.pdf !/reports/*.tex +/csv/* diff --git a/R/fetch.R b/R/fetch.R index 7eb00cc..09f9cfe 100644 --- a/R/fetch.R +++ b/R/fetch.R @@ -43,18 +43,7 @@ fetch_all <- function(download_dir="records/", create=FALSE) { if (str_sub(download_dir, -1) != .Platform$file.sep) download_dir <- str_c(download_dir, .Platform$file.sep) - # check if download_dir exists - if(file.access(download_dir, mode=0) == -1) { - if (create) { - tryCatch(dir.create(download_dir), - error = stop_dir_not_creatable, - warning = stop_dir_not_creatable) - } else { - stop("Directory does not exist. Use create = TRUE if you wish to create the directory.") - } - } else if (file.access(download_dir, mode=2) == -1) { - stop("Directory exists, but is not writeable.") - } + check_directory(download_dir, create) cat("Fetching all available records from bundestag.de. This may take a while ...\n") # create progress bar diff --git a/R/helpers.R b/R/helpers.R index 18fdda3..1e0120f 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -3,3 +3,18 @@ flip <- function(f) function(x, y) f(y, x) clear_na <- function(xs) xs[!is.na(xs)] + +check_directory <- function(path, create=F) { + # check if download_dir exists + if(file.access(path, mode=0) == -1) { + if (create) { + tryCatch(dir.create(path), + error = stop_dir_not_creatable, + warning = stop_dir_not_creatable) + } else { + stop("Directory does not exist. Use create = TRUE if you wish to create the directory.") + } + } else if (file.access(path, mode=2) == -1) { + stop("Directory exists, but is not writeable.") + } +} diff --git a/R/parse.R b/R/parse.R index 66daece..1a5b561 100644 --- a/R/parse.R +++ b/R/parse.R @@ -189,6 +189,25 @@ parse_rednerliste <- function(rednerliste_xml) { rolle_lang = d["rolle_lang",]) } +write_to_csv <- function(tables, path="csv/", create=F) { + check_directory(path, create) + write.table(tables$redner, str_c(path, "redner.csv")) + write.table(tables$reden, str_c(path, "reden.csv")) + write.table(tables$talks, str_c(path, "talks.csv")) + write.table(tables$comments, str_c(path, "comments.csv")) +} + +read_from_csv <- function(path="csv/") { + list(redner = read.table(str_c(path, "redner.csv")) %>% + tibble() %>% + mutate(id = as.character(id)), + reden = read.table(str_c(path, "reden.csv")) %>% + tibble() %>% + mutate(redner = as.character(redner)), + talks = tibble %$% read.table(str_c(path, "talks.csv")), + comments = tibble %$% read.table(str_c(path, "comments.csv"))) +} + # ------------------------------- # EXAMPLE USE