add check for valid structure of tables

This commit is contained in:
2021-08-10 12:42:41 +02:00
parent 4093cb603c
commit e038d86339
3 changed files with 39 additions and 0 deletions
+35
View File
@@ -24,3 +24,38 @@ 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`))
}