Skip to content

Commit

Permalink
0.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Nov 26, 2017
1 parent 1ecbc81 commit 48990d5
Show file tree
Hide file tree
Showing 94 changed files with 4,409 additions and 2,911 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: hansard
Type: Package
Title: Provides Easy Downloading Capabilities for the UK Parliament API
Version: 0.5.6
Date: 2017-11-18
Authors@R: person("Evan Odell", email="[email protected]", role=c("aut", "cre"))
Version: 0.5.7
Date: 2017-11-26
Authors@R: person("Evan Odell", email="[email protected]", role=c("aut", "cre"), comment = c(ORCID="0000-0003-1845-808X"))
Author: Evan Odell [aut, cre]
Maintainer: Evan Odell <[email protected]>
Description: Provides functions to download data from the <http://www.data.parliament.uk/> APIs. Because of the structure of the API, there is a named function for each type of available data for ease of use, as well as some functions designed to retrieve specific pieces of commonly used data. Functions for each new API will be added as and when they become available.
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

# hansard 0.5.7

Internal rewrites to make maintenance easier and produce a few very small increases in speed in several functions.

Fixed some typos in documentation, brought documentation into line with the reccomended 80 character maximum width for each line.

# hansard 0.5.6

Fixed bug on `constituencies()` so that the `NULL` default value of parameter `current` works properly.


# hansard 0.5.5

## Function changes
Expand Down
199 changes: 130 additions & 69 deletions R/all_answered_questions.R

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions R/bill_stage_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@
#' }

bill_stage_types <- function(tidy = TRUE, tidy_style = "snake_case", verbose = FALSE) {

stages <- jsonlite::fromJSON("http://lda.data.parliament.uk/billstagetypes.json?_pageSize=500", flatten = TRUE)

df <- tibble::as_tibble(stages$result$items)

if (nrow(df) == 0 && verbose==TRUE) {

message("The request did not return any data. Please check your search parameters.")

} else {

if (tidy == TRUE) {

df <- hansard_tidy(df, tidy_style)


stages <- jsonlite::fromJSON("http://lda.data.parliament.uk/billstagetypes.json?_pageSize=500",
flatten = TRUE)

df <- tibble::as_tibble(stages$result$items)

if (nrow(df) == 0 && verbose == TRUE) {

message("The request did not return any data. Please check your search parameters.")

} else {

if (tidy == TRUE) {

df <- hansard_tidy(df, tidy_style)

}

df

}

df

}


}


Expand Down
61 changes: 38 additions & 23 deletions R/bills.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
#'
#' Imports data on House of Commons and House of Lords bills.
#'
#' @param ID The ID of a given bill to return data on. If \code{NULL}, returns all bills, subject to other parameters. Defaults to \code{NULL}.
#' @param amendments If \code{TRUE}, returns all bills with amendments. Defaults to \code{FALSE}.
#' @param start_date Only includes bills introduced on or after this date. Accepts character values in \code{'YYYY-MM-DD'} format, and objects of class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or anything else than can be coerced to a date with \code{as.Date()}. Defaults to \code{'1900-01-01'}.
#' @param end_date Only includes bills introduced on or before this date. Accepts character values in \code{'YYYY-MM-DD'} format, and objects of class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or anything else than can be coerced to a date with \code{as.Date()}. Defaults to the current system date.
#' @param ID The ID of a given bill to return data on. If \code{NULL},
#' returns all bills, subject to other parameters. Defaults to \code{NULL}.
#' @param amendments If \code{TRUE}, returns all bills with amendments.
#' Defaults to \code{FALSE}.
#' @param start_date Only includes bills introduced on or after this date.
#' Accepts character values in \code{'YYYY-MM-DD'} format, and objects of
#' class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or
#' anything else that can be coerced to a date with \code{as.Date()}.
#' Defaults to \code{'1900-01-01'}.
#' @param end_date Only includes bills introduced on or before this date.
#' Accepts character values in \code{'YYYY-MM-DD'} format, and objects of
#' class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or
#' anything else that can be coerced to a date with \code{as.Date()}.
#' Defaults to the current system date.
#' @inheritParams all_answered_questions
#' @return A tibble with details on bills before the House of Lords and the House of Commons.
### @keywords bills
#' @return A tibble with details on bills before the House of Lords
#' and the House of Commons.
#' @seealso \code{\link{bill_stage_types}}
#' @export
#' @examples \dontrun{
Expand All @@ -23,9 +33,14 @@
#' x <- bills(start_date ='2016-01-01')
#' }

bills <- function(ID = NULL, amendments = FALSE, start_date = "1900-01-01", end_date = Sys.Date(), extra_args = NULL, tidy = TRUE, tidy_style = "snake_case", verbose = FALSE) {
bills <- function(ID = NULL, amendments = FALSE, start_date = "1900-01-01",
end_date = Sys.Date(), extra_args = NULL, tidy = TRUE,
tidy_style = "snake_case", verbose = FALSE) {

dates <- paste0("&_properties=date&max-date=", as.Date(end_date), "&min-date=", as.Date(start_date))
dates <- paste0("&_properties=date&max-date=",
as.Date(end_date),
"&min-date=",
as.Date(start_date))

if (is.null(ID) == FALSE) {

Expand All @@ -39,45 +54,45 @@ bills <- function(ID = NULL, amendments = FALSE, start_date = "1900-01-01", end_

if (amendments == TRUE) {

amend_query <- "withamendments.json?"
amend_query <- "withamendments.json?"

} else {

amend_query <- ".json?"
amend_query <- ".json?"

}

baseurl <- "http://lda.data.parliament.uk/bills"

if(verbose==TRUE){message("Connecting to API")}
if (verbose == TRUE) {
message("Connecting to API")
}

bills <- jsonlite::fromJSON(paste0(baseurl, amend_query, dates, id_query, extra_args), flatten = TRUE)
bills <- jsonlite::fromJSON(paste0(baseurl, amend_query, dates,
id_query, extra_args, "&_pageSize=1"),
flatten = TRUE)

jpage <- floor(bills$result$totalResults/500)

pages <- list()

for (i in 0:jpage) {
mydata <- jsonlite::fromJSON(paste0(baseurl, amend_query, dates, id_query, extra_args, "&_pageSize=500&_page=", i), flatten = TRUE)
if(verbose==TRUE){message("Retrieving page ", i + 1, " of ", jpage + 1)}
pages[[i + 1]] <- mydata$result$items
}
query <- paste0(baseurl, amend_query,
dates, id_query, extra_args,
"&_pageSize=500&_page=")

df <- tibble::as_tibble(dplyr::bind_rows(pages))
df <- loop_query(query, jpage, verbose) # in utils-loop.R

if (nrow(df) == 0 && verbose==TRUE) {
if (nrow(df) == 0 && verbose == TRUE) {

message("The request did not return any data. Please check your search parameters.")

} else {

if (tidy == TRUE) {

df <- bills_tidy(df, tidy_style)### in utils-bills.R
df <- bills_tidy(df, tidy_style) ### in utils-bills.R

}

df
df

}

Expand Down
125 changes: 73 additions & 52 deletions R/commons_answered_questions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

#' House of Commons answered questions
#'
#' Imports data on House of Commons answered questions. If all parameters are left empty, imports all available answered questions in a tibble.
#' Imports data on House of Commons answered questions. If all parameters
#' are left empty, imports all available answered questions in a tibble.
#'
#' If \code{answering_department} and/or \code{answered_by} are given a list with multiple deparments/IDs, all possible combination of those criteria are returned.
#' If \code{answering_department} and/or \code{answered_by} are given
#' a list with multiple deparments/IDs, all possible combination of those
#' criteria are returned.
#'
#' @param answering_department Accepts the name of a department or a list of department names. Returns a tibble with all answered questions in the House of Commons from the given department. Defaults to \code{NULL}.
#' @param answered_by Accepts the ID of an MP, or a list of IDs. Returns a tibble with all answered questions in the House of Commons by the given MP(s). Defaults to \code{NULL}.
#' @param start_date Only includes questions answered introduced on or after this date. Accepts character values in \code{'YYYY-MM-DD'} format, and objects of class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or anything else than can be coerced to a date with \code{as.Date()}. Defaults to \code{'1900-01-01'}.
#' @param end_date Only includes questions answered on or before this date. Accepts character values in \code{'YYYY-MM-DD'} format, and objects of class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or anything else than can be coerced to a date with \code{as.Date()}. Defaults to the current system date.
#' @param answering_department Accepts the name of a department or a
#' list of department names. Returns a tibble with all answered questions
#' in the House of Commons from the given department. Defaults to \code{NULL}.
#' @param answered_by Accepts the ID of an MP, or a list of IDs. Returns a
#' tibble with all answered questions in the House of Commons by the given
#' MP(s). Defaults to \code{NULL}.
#' @param start_date Only includes questions answered introduced on or after
#' this date. Accepts character values in \code{'YYYY-MM-DD'} format, and
#' objects of class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt}
#' or anything else that can be coerced to a date with \code{as.Date()}.
#' Defaults to \code{'1900-01-01'}.
#' @param end_date Only includes questions answered on or before this date.
#' Accepts character values in \code{'YYYY-MM-DD'} format, and objects of
#' class \code{Date}, \code{POSIXt}, \code{POSIXct}, \code{POSIXlt} or
#' anything else that can be coerced to a date with \code{as.Date()}.
#' Defaults to the current system date.
#' @inheritParams all_answered_questions
#' @return A tibble with details on all answered questions in the House of Commons.
#' @seealso \code{\link{all_answered_questions}}
Expand All @@ -21,85 +36,91 @@
#' @export
#' @examples \dontrun{
#' x <- commons_answered_questions(answering_department = c('health','education'),
#' answered_by = c('4019', '1542', '111'), start_date = '2017-01-01')
#' answered_by = c('4019', '1542', '111'),
#' start_date = '2017-01-01')
#'
#' x <- commons_answered_questions(start_date = '2017-03-26', end_date='2017-04-01')
#' }

commons_answered_questions <- function(answering_department = NULL,
answered_by = NULL,
start_date = "1900-01-01",
end_date = Sys.Date(),
extra_args = NULL,
tidy = TRUE,
tidy_style = "snake_case",
verbose = FALSE) {

commons_answered_questions <- function(answering_department = NULL, answered_by = NULL, start_date = "1900-01-01", end_date = Sys.Date(), extra_args = NULL, tidy = TRUE, tidy_style = "snake_case", verbose = FALSE) {
if (length(answered_by) > 1 || length(answering_department) > 1) {

if (length(answered_by) > 1 || length(answering_department) > 1) {

df <- caq_multi(answering_department, answered_by, start_date, end_date, extra_args, verbose)

} else {

dates <- paste0("&max-dateOfAnswer=", as.Date(end_date), "&min-dateOfAnswer=", as.Date(start_date))

if (is.null(answered_by) == FALSE && is.na(answered_by)==FALSE) {

answered_by <- paste0("&answeringMember=http://data.parliament.uk/members/", answered_by)
df <- caq_multi(answering_department, answered_by,
start_date, end_date,
extra_args, verbose)

} else {

answered_by <- NULL
dates <- paste0("&max-dateOfAnswer=",
as.Date(end_date),
"&min-dateOfAnswer=",
as.Date(start_date))

}

if (is.null(answering_department) == FALSE && is.na(answering_department)==FALSE) {
if (is.null(answered_by) == FALSE && is.na(answered_by) == FALSE) {

query <- "/answeringdepartment"
answered_by <- paste0("&answeringMember=http://data.parliament.uk/members/",
answered_by)

answering_department <- paste0("q=", answering_department)
} else {

} else {
answered_by <- NULL

query <- NULL
}

answering_department <- NULL
if (is.null(answering_department) == FALSE && is.na(answering_department) ==
FALSE) {

}
dept_query <- "/answeringdepartment"

baseurl <- "http://lda.data.parliament.uk/commonsansweredquestions"
answering_department <- paste0("q=", answering_department)

if (verbose == TRUE) {
message("Connecting to API")
}
} else {

answered <- jsonlite::fromJSON(paste0(baseurl, query, ".json?", answering_department, answered_by, dates, extra_args), flatten = TRUE)
dept_query <- NULL

jpage <- floor(answered$result$totalResults/500)
answering_department <- NULL

pages <- list()
}

if (answered$result$totalResults > 0) {
baseurl <- "http://lda.data.parliament.uk/commonsansweredquestions"

for (i in 0:jpage) {
mydata <- jsonlite::fromJSON(paste0(baseurl, query, ".json?", answering_department, answered_by, dates, extra_args, "&_pageSize=500&_page=", i), flatten = TRUE)
if (verbose == TRUE) {
message("Retrieving page ", i + 1, " of ", jpage + 1)
message("Connecting to API")
}
pages[[i + 1]] <- mydata$result$items
}

}
answered <- jsonlite::fromJSON(paste0(baseurl, dept_query, ".json?",
answering_department, answered_by,
dates, extra_args, "&_pageSize=1"),
flatten = TRUE)

df <- tibble::as_tibble(dplyr::bind_rows(pages))
jpage <- floor(answered$result$totalResults/500)

}
query <- paste0(baseurl, dept_query, ".json?", answering_department,
answered_by, dates, extra_args, "&_pageSize=500&_page=")

if (nrow(df) == 0 && verbose == TRUE) {
message("The request did not return any data. Please check your search parameters.")
}
df <- loop_query(query, jpage, verbose)

if (tidy == TRUE) {
}

df <- caq_tidy(df, tidy_style) ## in utils-commons.R
if (nrow(df) == 0 && verbose == TRUE) {
message("The request did not return any data. Please check your search parameters.")
}

if (tidy == TRUE) {

}
df <- caq_tidy(df, tidy_style) ## in utils-commons.R

}

df
df

}

Expand Down
Loading

0 comments on commit 48990d5

Please sign in to comment.