From 6aa80534f8a9c45782a0f6cb1e865ca2039bb4f7 Mon Sep 17 00:00:00 2001 From: flavis Date: Tue, 10 Aug 2021 21:51:00 +0200 Subject: [PATCH] add checks to read/write csv and refactor --- R/fetch.R | 7 ------- R/helpers.R | 7 +++++++ R/parse.R | 12 +++++++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/R/fetch.R b/R/fetch.R index eb58921..45a2e04 100644 --- a/R/fetch.R +++ b/R/fetch.R @@ -61,10 +61,3 @@ fetch_all <- function(download_dir="inst/records/", create=FALSE) { # if successful, set progressbar to 100% setTimerProgressBar(pb, 250) } - -stop_dir_not_creatable <- function(cond) { - # currently this has call: dir.create(download_dir) - # do we want to change this to fetch_all(...) ? - cond$message <- "Directory does not exist and can't be created. Probably because the path is not writeable." - stop(cond) -} diff --git a/R/helpers.R b/R/helpers.R index db097dd..7fa65cc 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -19,6 +19,13 @@ check_directory <- function(path, create=F) { } } +stop_dir_not_creatable <- function(cond) { + # currently this has call: dir.create(download_dir) + # do we want to change this to fetch_all(...) ? + cond$message <- "Directory does not exist and can't be created. Probably because the path is not writeable." + stop(cond) +} + # appends a file seperator at end of path if needed make_directory_path <- function(path) { if (!str_ends(path, .Platform$file.sep)) str_c(path, .Platform$file.sep) diff --git a/R/parse.R b/R/parse.R index d7fb267..80bb2f6 100644 --- a/R/parse.R +++ b/R/parse.R @@ -233,6 +233,11 @@ parse_speakerlist <- function(speakerliste_xml) { #' #' @export write_to_csv <- function(tables, path="inst/csv/", create=F) { + is_valid_res(tables) + stopifnot("path must be of type character" = is.character(path)) + stopifnot("create must be of type logical" = is.logical(create)) + + path <- make_directory_path(path) check_directory(path, create) write.table(tables$speaker, str_c(path, "speaker.csv")) write.table(tables$speeches, str_c(path, "speeches.csv")) @@ -250,6 +255,9 @@ write_to_csv <- function(tables, path="inst/csv/", create=F) { #' #' @export read_from_csv <- function(path="inst/csv/") { + stopifnot("path must be of type character" = is.character(path)) + + path <- make_directory_path(path) list(speaker = read.table(str_c(path, "speaker.csv")) %>% tibble() %>% mutate(id = as.character(id)), @@ -259,5 +267,7 @@ read_from_csv <- function(path="inst/csv/") { date = as.Date(date)), talks = tibble %$% read.table(str_c(path, "talks.csv")), comments = tibble %$% read.table(str_c(path, "comments.csv")), - applause = tibble %$% read.table(str_c(path, "applause.csv"))) + applause = tibble %$% read.table(str_c(path, "applause.csv"))) -> res + is_valid_res(res) + res }