-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmp_questions.R
168 lines (142 loc) · 4.44 KB
/
mp_questions.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#' Questions asked by a given MP or MPs
#'
#' Accepts an ID number for a member of the House of Commons, and returns a
#' tibble of of all their oral and written questions.
#'
#' @param mp_id The ID number of a member of the House of Commons, or a vector
#' of IDs. Defaults to `NULL`.
#'
#' @param question_type Accepts the arguments `'all'`, `'oral'` and
#' `'written'`. This parameter is not case sensitive.
#' Defaults to `'all'`.
#'
#' @param start_date Only includes questions answered 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 questions answered 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 all questions asked by a
#' member of the House of Commons.
#'
#' @seealso [all_answered_questions()]
#' @seealso [commons_answered_questions()]
#' @seealso [commons_oral_questions()]
#' @seealso [commons_oral_question_times()]
#' @seealso [commons_written_questions()]
#' @seealso [lords_written_questions()]
#'
#' @export
#' @examples
#' \dontrun{
#' x <- mp_questions(c(172, 3967), "all")
#'
#' y <- mp_questions(mp_id = 172, question_type = "all")
#'
#' z <- mp_questions(c(172, 3967), "written")
#' }
#'
mp_questions <- function(mp_id = NULL, question_type = "all",
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)
}
question_type <- tolower(question_type)
if (length(mp_id) > 1) {
# for lists of more than 1 ID
df <- mp_question_multi(
mp_id = mp_id, question_type = question_type,
start_date = start_date, end_date = end_date,
extra_args = extra_args, verbose = verbose
)
} else {
if (question_type == "all") {
veb(verbose)
df_oral <- hansard::mp_questions(
mp_id = mp_id,
question_type = "oral",
start_date = start_date,
end_date = end_date,
extra_args = extra_args,
tidy = FALSE,
tidy_style = tidy_style
)
if (verbose) {
message("Retrieving written questions")
}
df_writ <- hansard::mp_questions(
mp_id = mp_id,
question_type = "written",
start_date = start_date,
end_date = end_date,
extra_args = extra_args,
tidy = FALSE,
tidy_style = tidy_style
)
if (verbose) {
message("Combining oral and written questions")
}
if (is.null(df_oral)) {
df <- df_writ
} else if (is.null(df_writ)) {
df <- df_oral
} else {
common <- intersect(colnames(df_writ), colnames(df_oral))
df <- rbind(
subset(df_writ, select = common),
subset(df_oral, select = common)
)
}
df
} else if (question_type == "oral") {
dates <- paste0(
"&_properties=AnswerDate&max-AnswerDate=",
as.Date(end_date),
"&min-AnswerDate=",
as.Date(start_date)
)
query <- paste0(
url_util, "commonsoralquestions.json?mnisId=",
mp_id, dates, extra_args
)
oral <- jsonlite::fromJSON(paste0(
query, "&_pageSize=1"
))
df <- loop_query(query, verbose) # in utils-loop.R
} else if (question_type == "written") {
dates <- paste0(
"&_properties=dateTabled&max-dateTabled=",
as.Date(end_date),
"&min-dateTabled=",
as.Date(start_date)
)
query <- paste0(
url_util, "commonswrittenquestions.json?mnisId=",
mp_id, dates, extra_args
)
wr <- jsonlite::fromJSON(query)
pages <- list()
df <- loop_query(query, verbose) # in utils-loop.R
}
}
if (nrow(df) == 0) {
message("The request did not return any data.
Please check your parameters.")
} else {
if (tidy) {
df <- mp_question_tidy(df, tidy_style)
}
df
}
}
#' @rdname mp_questions
#' @export
hansard_mp_questions <- mp_questions