-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmp_vote_record.R
142 lines (119 loc) · 3.75 KB
/
mp_vote_record.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#' Individual MP voting records
#'
#' Accepts an ID number for a member of the House of Commons, and returns a
#' tibble of their votes.
#'
#'
#' @param mp_id The ID number of a member of the House of Commons.
#'
#' @param lobby Accepts one of `'all'`, `'aye'` or `'no'`.
#' `'aye'` returns votes where the MP voted `'aye'`, `'no'`
#' returns votes where the MP voted `'no'`, `'all'` returns all
#' available votes by the MP. This parameter is not case sensitive.
#' Defaults to `'all'`.
#'
#' @param session The parliamentary session to return votes from, in
#' `'YYYY/YY'` format. Defaults to `NULL`.
#'
#' @param start_date Only includes divisions on or after this date. Accepts
#' character values in `'YYYY-MM-DD'` format, and objects of class
#' `Date`, `POSIXt`, `POSIXct`, `POSIXlt` or anything
#' else that can be coerced to a date with `as.Date()`. Defaults to
#' `'1900-01-01'`.
#'
#' @param end_date Only includes divisions on or before this date. Accepts
#' character values in `'YYYY-MM-DD'` format, and objects of class
#' `Date`, `POSIXt`, `POSIXct`, `POSIXlt` or anything
#' else that can be coerced to a date with `as.Date()`. Defaults to
#' the current system date.
#' @inheritParams all_answered_questions
#' @return A tibble with details on the voting record of the given MP.
#' @export
#' @examples
#' \dontrun{
#' x <- mp_vote_record(172, lobby = "all")
#'
#' x <- mp_vote_record(172, lobby = "aye")
#'
#' x <- mp_vote_record(172, lobby = "no")
#'
#' x <- mp_vote_record(172, session = "2016/17")
#' }
#'
mp_vote_record <- function(mp_id = NULL, lobby = "all", session = NULL,
start_date = "1900-01-01", end_date = Sys.Date(),
extra_args = NULL, tidy = TRUE,
tidy_style = "snake", verbose = TRUE) {
if (is.null(mp_id)) {
stop("mp_id must not be empty", call. = FALSE)
}
if (!is.null(extra_args)) {
extra_args <- utils::URLencode(extra_args)
}
if (!is.null(session)) {
session_query <- paste0("&session=", session)
} else {
session_query <- ""
}
lobby <- tolower(lobby)
dates <- paste0(
"&_properties=date&max-date=",
as.Date(end_date), "&min-date=",
as.Date(start_date)
)
if (lobby == "aye") {
query <- paste0(
url_util, "commonsdivisions/aye.json?mnisId=",
mp_id, dates, session_query, extra_args
)
df <- loop_query(query, verbose) # in utils-loop.R
} else if (lobby == "no") {
query <- paste0(
url_util, "commonsdivisions/no.json?mnisId=",
mp_id, dates, session_query, extra_args
)
df <- loop_query(query, verbose) # in utils-loop.R
} else {
if (verbose) {
message("Retrieving aye votes:")
}
df_aye <- mp_vote_record(
mp_id = mp_id, lobby = "aye",
session = session, start_date = start_date,
end_date = end_date, extra_args = extra_args,
tidy = FALSE, tidy_style = tidy_style,
verbose = verbose
)
df_aye$vote <- "aye"
if (verbose) {
message("Retrieving no votes:")
}
df_no <- mp_vote_record(
mp_id = mp_id, lobby = "no",
session = session, start_date = start_date,
end_date = end_date, extra_args = extra_args,
tidy = FALSE, tidy_style = tidy_style,
verbose = verbose
)
df_no$divisionNumber <- NULL
df_no$vote <- "no"
df <- dplyr::bind_rows(df_aye, df_no)
if (tidy) {
df$vote <- as.factor(df$vote)
}
}
if (nrow(df) == 0) {
message("The request did not return any data.
Please check your parameters.")
} else {
if (tidy) {
df$date._datatype <- "POSIXct"
df$date._value <- as.POSIXct(df$date._value)
df <- hansard_tidy(df, tidy_style)
}
df
}
}
#' @rdname mp_vote_record
#' @export
hansard_mp_vote_record <- mp_vote_record