69 lines
3.1 KiB
R
69 lines
3.1 KiB
R
`%$%` <- function(f, x) f(x)
|
|
`%.%` <- function(f, g) function(...) f(g(...))
|
|
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.")
|
|
}
|
|
}
|
|
|
|
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)
|
|
else path
|
|
}
|
|
|
|
# check if res is of expected format
|
|
is_valid_res <- function(res) {
|
|
stopifnot("Data is missing relevant tables. Is this a return value of read_all or repair?"
|
|
= all(c("speaker", "speeches", "talks", "comments", "applause") %in% names(res)))
|
|
stopifnot("Some entries of res are no tibbles."
|
|
= all(sapply(res, typeof) == "list" & "tbl" %in% sapply(res, class)))
|
|
stopifnot("Speaker table is of wrong format."
|
|
= all(c("id", "prename", "lastname", "fraction", "title", "role_short", "role_long")
|
|
%in% names(res$speaker)) &&
|
|
all(sapply(res$speaker, is.character)))
|
|
stopifnot("Speeches table is of wrong format."
|
|
= all(c("id", "speaker", "date") %in% names(res$speeches)) &&
|
|
is.character(res$speeches$id) &&
|
|
is.character(res$speeches$speaker) &&
|
|
lubridate::is.Date(res$speeches$date))
|
|
stopifnot("Talks table is of wrong format."
|
|
= all(c("speech_id", "speaker", "content") %in% names(res$talks)) &&
|
|
all(sapply(res$talks, is.character)))
|
|
stopifnot("Comments table is of wrong format."
|
|
= all(c("speech_id", "on_speaker", "fraction", "commenter", "content")
|
|
%in% names(res$comments)) &&
|
|
all(sapply(res$comments, is.character)))
|
|
stopifnot("Applause table is of wrong format."
|
|
= all(c("speech_id", "on_speaker", "CDU_CSU", "SPD", "FDP", "DIE_LINKE", "BUENDNIS_90_DIE_GRUENEN", "AfD")
|
|
%in% names(res$applause)) &&
|
|
is.character(res$applause$speech_id) &&
|
|
is.character(res$applause$on_speaker) &&
|
|
is.logical(res$applause$`CDU_CSU`) &&
|
|
is.logical(res$applause$`SPD`) &&
|
|
is.logical(res$applause$`FDP`) &&
|
|
is.logical(res$applause$`DIE_LINKE`) &&
|
|
is.logical(res$applause$`AfD`) &&
|
|
is.logical(res$applause$`BUENDNIS_90_DIE_GRUENEN`))
|
|
}
|