From ecb5677703f9f10ac0bff9fb58ce2a6b5419681a Mon Sep 17 00:00:00 2001 From: flavis Date: Wed, 11 Aug 2021 09:15:26 +0200 Subject: [PATCH] add bare read_from_csv_or_fetch --- NAMESPACE | 1 + R/parse.R | 15 +++++++++++++++ vignettes/explicittopic.Rmd | 5 +++-- vignettes/genderequality.Rmd | 7 +++++-- vignettes/generalquestions.Rmd | 5 +++-- vignettes/hitlercomparison.Rmd | 5 +++-- vignettes/interaction.Rmd | 5 +++-- 7 files changed, 33 insertions(+), 10 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 3d291b9..c5f1962 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(join_speaker) export(party_colors) export(read_all) export(read_from_csv) +export(read_from_csv_or_fetch) export(repair) export(word_usage_by_date) export(write_to_csv) diff --git a/R/parse.R b/R/parse.R index 80bb2f6..8ab01be 100644 --- a/R/parse.R +++ b/R/parse.R @@ -271,3 +271,18 @@ read_from_csv <- function(path="inst/csv/") { is_valid_res(res) res } + +#' @export +read_from_csv_or_fetch <- function(path="inst/") { + path <- make_directory_path(path) + res <- tryCatch(read_from_csv(str_c(path, "csv/")), + error = function(c) NULL) + if (!is.null(res)) return(res) + + fetch_all(str_c(path, "records/"), create=T) + read_all(str_c(path, "records/")) %>% + repair() -> + res + write_to_csv(res, str_c(path, "csv/"), create=T) + res +} diff --git a/vignettes/explicittopic.Rmd b/vignettes/explicittopic.Rmd index 18f330d..c406f33 100644 --- a/vignettes/explicittopic.Rmd +++ b/vignettes/explicittopic.Rmd @@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res ``` We also used `repair` to fix a bunch of formatting issues in the records. -For development purposes, we load the tables from csv files. +For development purposes, we only fetch records if they are not already +stored as csv files: ```{r} -res <- read_from_csv('../inst/csv/') +res <- read_from_csv_or_fetch('../inst/') ``` ## Analysis diff --git a/vignettes/genderequality.Rmd b/vignettes/genderequality.Rmd index 0e9ea15..4c49634 100644 --- a/vignettes/genderequality.Rmd +++ b/vignettes/genderequality.Rmd @@ -35,10 +35,13 @@ read_all("../records/") %>% repair() -> res ``` We also used `repair` to fix a bunch of formatting issues in the records. -For development purposes, we load the tables from csv files. +For development purposes, we only fetch records if they are not already +stored as csv files: ```{r} -res <- read_from_csv('../inst/csv/') +res <- read_from_csv_or_fetch('../inst/') ``` + + and unpack our tibbles ```{r} comments <- res$comments diff --git a/vignettes/generalquestions.Rmd b/vignettes/generalquestions.Rmd index 09164f8..14187e7 100644 --- a/vignettes/generalquestions.Rmd +++ b/vignettes/generalquestions.Rmd @@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res ``` We also used `repair` to fix a bunch of formatting issues in the records. -For development purposes, we load the tables from csv files. +For development purposes, we only fetch records if they are not already +stored as csv files: ```{r} -res <- read_from_csv('../inst/csv/') +res <- read_from_csv_or_fetch('../inst/') ``` ## Analysis diff --git a/vignettes/hitlercomparison.Rmd b/vignettes/hitlercomparison.Rmd index f7b2999..7f77cc9 100644 --- a/vignettes/hitlercomparison.Rmd +++ b/vignettes/hitlercomparison.Rmd @@ -38,9 +38,10 @@ talks <- res$talks We also used `repair` to fix a bunch of formatting issues in the records and unpacked the result into more descriptive variables. -For development purposes, we load the tables from csv files. +For development purposes, we only fetch records if they are not already +stored as csv files: ```{r} -tables <- read_from_csv('../inst/csv/') +tables <- read_from_csv_or_fetch('../inst/') comments <- tables$comments speeches <- tables$speeches diff --git a/vignettes/interaction.Rmd b/vignettes/interaction.Rmd index 2b60ddf..b308d3d 100644 --- a/vignettes/interaction.Rmd +++ b/vignettes/interaction.Rmd @@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res ``` We also used `repair` to fix a bunch of formatting issues in the records. -For development purposes, we load the tables from csv files. +For development purposes, we only fetch records if they are not already +stored as csv files: ```{r} -res <- read_from_csv('../inst/csv/') +res <- read_from_csv_or_fetch('../inst/') ``` ## Analysis