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.

69 line
3.1KB

  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. stop_dir_not_creatable <- function(cond) {
  20. # currently this has call: dir.create(download_dir)
  21. # do we want to change this to fetch_all(...) ?
  22. cond$message <- "Directory does not exist and can't be created. Probably because the path is not writeable."
  23. stop(cond)
  24. }
  25. # appends a file seperator at end of path if needed
  26. make_directory_path <- function(path) {
  27. if (!str_ends(path, .Platform$file.sep)) str_c(path, .Platform$file.sep)
  28. else path
  29. }
  30. # check if res is of expected format
  31. is_valid_res <- function(res) {
  32. stopifnot("Data is missing relevant tables. Is this a return value of read_all or repair?"
  33. = all(c("speaker", "speeches", "talks", "comments", "applause") %in% names(res)))
  34. stopifnot("Some entries of res are no tibbles."
  35. = all(sapply(res, typeof) == "list" & "tbl" %in% sapply(res, class)))
  36. stopifnot("Speaker table is of wrong format."
  37. = all(c("id", "prename", "lastname", "fraction", "title", "role_short", "role_long")
  38. %in% names(res$speaker)) &&
  39. all(sapply(res$speaker, is.character)))
  40. stopifnot("Speeches table is of wrong format."
  41. = all(c("id", "speaker", "date") %in% names(res$speeches)) &&
  42. is.character(res$speeches$id) &&
  43. is.character(res$speeches$speaker) &&
  44. lubridate::is.Date(res$speeches$date))
  45. stopifnot("Talks table is of wrong format."
  46. = all(c("speech_id", "speaker", "content") %in% names(res$talks)) &&
  47. all(sapply(res$talks, is.character)))
  48. stopifnot("Comments table is of wrong format."
  49. = all(c("speech_id", "on_speaker", "fraction", "commenter", "content")
  50. %in% names(res$comments)) &&
  51. all(sapply(res$comments, is.character)))
  52. stopifnot("Applause table is of wrong format."
  53. = all(c("speech_id", "on_speaker", "CDU_CSU", "SPD", "FDP", "DIE_LINKE", "BUENDNIS_90_DIE_GRUENEN", "AfD")
  54. %in% names(res$applause)) &&
  55. is.character(res$applause$speech_id) &&
  56. is.character(res$applause$on_speaker) &&
  57. is.logical(res$applause$`CDU_CSU`) &&
  58. is.logical(res$applause$`SPD`) &&
  59. is.logical(res$applause$`FDP`) &&
  60. is.logical(res$applause$`DIE_LINKE`) &&
  61. is.logical(res$applause$`AfD`) &&
  62. is.logical(res$applause$`BUENDNIS_90_DIE_GRUENEN`))
  63. }