An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.8KB

  1. `%$%` <- function(f, x) f(x)
  2. `%.%` <- function(f, g) function(...) f(g(...))
  3. flip <- function(f) function(x, y) f(y, x)
  4. clear_na <- function(xs) xs[!is.na(xs)]
  5. check_directory <- function(path, create=F) {
  6. # check if download_dir exists
  7. if(file.access(path, mode=0) == -1) {
  8. if (create) {
  9. tryCatch(dir.create(path),
  10. error = stop_dir_not_creatable,
  11. warning = stop_dir_not_creatable)
  12. } else {
  13. stop("Directory does not exist. Use create = TRUE if you wish to create the directory.")
  14. }
  15. } else if (file.access(path, mode=2) == -1) {
  16. stop("Directory exists, but is not writeable.")
  17. }
  18. }
  19. # appends a file seperator at end of path if needed
  20. make_directory_path <- function(path) {
  21. if (!str_ends(path, .Platform$file.sep)) str_c(path, .Platform$file.sep)
  22. else path
  23. }
  24. # check if res is of expected format
  25. is_valid_res <- function(res) {
  26. stopifnot("Data is missing relevant tables. Is this a return value of read_all or repair?"
  27. = all(c("speaker", "speeches", "talks", "comments", "applause") %in% names(res)))
  28. stopifnot("Some entries of res are no tibbles."
  29. = all(sapply(res, typeof) == "list" & "tbl" %in% sapply(res, class)))
  30. stopifnot("Speaker table is of wrong format."
  31. = all(c("id", "prename", "lastname", "fraction", "title", "role_short", "role_long")
  32. %in% names(res$speaker)) &&
  33. all(sapply(res$speaker, is.character)))
  34. stopifnot("Speeches table is of wrong format."
  35. = all(c("id", "speaker", "date") %in% names(res$speeches)) &&
  36. is.character(res$speeches$id) &&
  37. is.character(res$speeches$speaker) &&
  38. lubridate::is.Date(res$speeches$date))
  39. stopifnot("Talks table is of wrong format."
  40. = all(c("speech_id", "speaker", "content") %in% names(res$talks)) &&
  41. all(sapply(res$talks, is.character)))
  42. stopifnot("Comments table is of wrong format."
  43. = all(c("speech_id", "on_speaker", "fraction", "commenter", "content")
  44. %in% names(res$comments)) &&
  45. all(sapply(res$comments, is.character)))
  46. stopifnot("Applause table is of wrong format."
  47. = all(c("speech_id", "on_speaker", "CDU_CSU", "SPD", "FDP", "DIE_LINKE", "BUENDNIS_90_DIE_GRUENEN", "AfD")
  48. %in% names(res$applause)) &&
  49. is.character(res$applause$speech_id) &&
  50. is.character(res$applause$on_speaker) &&
  51. is.logical(res$applause$`CDU_CSU`) &&
  52. is.logical(res$applause$`SPD`) &&
  53. is.logical(res$applause$`FDP`) &&
  54. is.logical(res$applause$`DIE_LINKE`) &&
  55. is.logical(res$applause$`AfD`) &&
  56. is.logical(res$applause$`BUENDNIS_90_DIE_GRUENEN`))
  57. }