Introduction to hansard

Evan Odell

2017-07-10

hansard is an R package to pull data from the UK parliament through the http://www.data.parliament.uk/ API. It emphasises simplicity and ease of use, so that users unfamiliar with APIs can easily retrieve large volumes of high quality data. Each function accepts a single argument at a time, and functions that require additional information to retrieve the data you requested will ask for it after you execute the function. Functions retrieve data in json format and convert it to a tibble. The hansard_generic function supports the building of API requests for XML, csv or HTML formats if required. Note that the API is rate limited to returning 5500 rows per request in some circumstances.

Installing hansard

From CRAN

install.packages("hansard")

From GitHub (Development Version)

install.packages("devtools")
devtools::install_github("EvanOdell/hansard")

Load hansard

library(hansard)

Using hansard

hansard contains functions for calling data for the UK Parliament API. The functions are designed to call data from a specific http://www.data.parliament.uk/ API. The parameter options for each function vary, depending on the specific information available from each API, but there are two constant parameters in every function (with the exception of the hansard_generic() and research_topics_list() functions described below): tidy and tidy_style.

tidy is a logical parameter accepting either TRUE or FALSE, defaulting to TRUE. If TRUE, hansard will fix variable names, which by default contain non alpha-numeric characters and appear to use an inconsistent/idiosyncratic naming convention, at least by the standards of the various naming conventions used in R. Dates and datetimes are converted to POSIXct class. Some extra URL data included in the API is also stripped out.

The naming convention for variables used if tidy==TRUE is indicated by tidy_style. tidy_style accepts one of "snake_case", "camelCase" and "period.case", defaulting to "snake_case". All variable names will be converted to match the given naming convention.

In addition to the more generic sounding function names, each function in hansard has a wrapper where the name is prefixed with hansard_. For example, both bills() and hansard_bills() will return the same result.

Example using the commons_divisions() and mp_vote_record() functions

The commons_divisions() function returns divisions in the House of Commons, including the result of votes and information on what we being voted on. mp_vote_record() returns a data frame the voting record of a given MP on each division they voted in. The example below returns all Commons Divisions where Diane Abbott voted aye in 2017. To find the parliamentary ID of Diane Abbott (or any other member of the House of Commons or House of Lords), use the members_search() function described below.

library(hansard)
library(tibble)##for the `glimpse()` function
z <- mp_vote_record(172, "aye", start_date = "2017-01-01", end_date = "2017-05-03")
## Connecting to API
## Retrieving page 1 of 1
glimpse(z)
## Observations: 38
## Variables: 5
## $ about         <chr> "http://data.parliament.uk/resources/722300", "h...
## $ title         <chr> "Early Parliamentary General Election", "Pension...
## $ uin           <chr> "CD:2017-04-19:264", "CD:2017-03-29:260", "CD:20...
## $ date_value    <dttm> 2017-04-19, 2017-03-29, 2017-03-29, 2017-03-29,...
## $ date_datatype <chr> "POSIXct", "POSIXct", "POSIXct", "POSIXct", "POS...

Using commons_divisions(), we can see the result of one of those votes, ID 722300, the Early Parliamentary General Election bill that dissolved parliament for the 2017 General Election. The function default is to return a list of every MP and how they voted:

x <- commons_divisions(722300)
## Connecting to API
glimpse(x)
## Observations: 535
## Variables: 6
## $ vote_id              <chr> "722300/vote/1", "722300/vote/10", "72230...
## $ member_party         <chr> "Labour", "Labour", "Labour", "Labour (Co...
## $ type                 <chr> "aye_vote", "aye_vote", "aye_vote", "aye_...
## $ member_printed_value <chr> "Ms Diane Abbott", "Dr Rosena Allin-Khan"...
## $ about                <chr> "172", "4573", "1579", "4088", "3950", "1...
## $ label_value          <chr> "Biography information for Ms Diane Abbot...

With the summary parameter, we can return a brief summary table of votes:

y <- commons_divisions(722300, summary=TRUE)
## Connecting to API
glimpse(y)
## Observations: 2
## Variables: 17
## $ about                         <chr> "http://data.parliament.uk/resou...
## $ abstain_count                 <chr> "0", "0"
## $ ayes_count                    <chr> "522", "522"
## $ deferred_vote                 <lgl> FALSE, FALSE
## $ didnotvotecount               <chr> "0", "0"
## $ errorvotecount                <chr> "0", "0"
## $ margin                        <chr> "509", "509"
## $ noesvotecount                 <chr> "13", "13"
## $ noneligiblecount              <chr> "0", "0"
## $ suspendedorexpelledvotescount <chr> "0", "0"
## $ date                          <dttm> 2017-04-19, 2017-04-19
## $ division_number               <chr> "196", "196"
## $ is_primary_topic_of           <chr> "http://lda.data.parliament.uk/c...
## $ legislature                   <chr> "http://data.parliament.uk/terms...
## $ session                       <chr> "2016/17", "http://data.parliame...
## $ title                         <chr> "Early Parliamentary General Ele...
## $ uin                           <chr> "CD:2017-04-19:264", "CD:2017-04...

The results of votes in the House of Lords can be retrieved with the lords_divisions function. The voting record of individual Lords can be retrieved using the lords_vote_record functions.

Special functions

Several functions have special or experimental features:

The research_briefings() function

The research_briefings() function includes the experimental feature of requesting data using lists created using the research_briefings_lists functions:

research_topics_list <- research_topics_list()

research_subtopics_list <- research_subtopics_list()

research_types_list <- research_types_list()

research_topics_list[[7]]
## [1] "Defence"
research_subtopics_list[[7]][10]
## [1] "Falkland Islands"
research_types_list[[1]]
## [1] "Lords Library notes"

In this case I have given them the same name as their function, but you can assign any name you wish to them.

Having created the lists, they can be used to specify which topics and subtopics to call, although strings can also be used.

a <- research_briefings(topic = research_topics_list[[7]])
## Retrieving page 1 of 1
b <- research_briefings(topic = research_topics_list[[7]], subtopic=research_subtopics_list[[7]][10])
## Retrieving page 1 of 1
c <- research_briefings(topic = "Defence")
## Retrieving page 1 of 1

If a specific subtopic is called, but the topic is not specified, the function will still return all data within that specific subtopic. Note that this is slower than specifying the topic and subtopic.

research_topics_list <- research_topics_list()

research_subtopics_list <- research_subtopics_list()

system.time(without_topic <- research_briefings(subtopic = research_subtopics_list[[7]][10]))
## Retrieving page 1 of 1
##    user  system elapsed 
##   0.413   0.091  11.197
system.time(with_topic <- research_briefings(topic = research_topics_list[[7]], subtopic=research_subtopics_list[[7]][10]))
## Retrieving page 1 of 1
##    user  system elapsed 
##   0.028   0.002   0.190
identical(with_topic, without_topic)
## [1] TRUE

If a specified subtopic is not a subtopic of the specified topic, the function will not return any data.

The hansard_generic() function

The hansard_generic() function allows you to put in your own paths to the API. Information on all the paths available in the API can be found on the DDP Explorer website.

x <- hansard_generic("commonsansweredquestions.json")

Note that the API defaults to returning 10 items per page, but allows up to 500 items per page.

The members_search() function

Looking up information on an individual MP or Lord through the Parliamentary API requires knowing their parliamentary ID number. This can be hard to find on the web, but luckily you can look it up through the API. We want information on the voting record of the Labour MP for Hackney North and Stoke Newington Diane Abbott, but we don’t know her ID number, so we search for her:

library(hansard)
members_search("abbot")
## Connecting to API
## Retrieving page 1 of 1
## # A tibble: 4 x 12
##   mnis_id                         home_page additional_name_value
##     <chr>                             <chr>                 <chr>
## 1     172     http://www.dianeabbott.org.uk                 Julie
## 2    1651                              <NA>             Granville
## 3    4249 http://www.annemariemorris.co.uk/                  <NA>
## 4    3827       http://www.judiciary.gov.uk                Edmond
## # ... with 9 more variables: constituency_about <chr>,
## #   constituency_label_value <chr>, family_name_value <chr>,
## #   full_name_value <chr>, gender_value <chr>, given_name_value <chr>,
## #   label_value <chr>, party_value <chr>, twitter_value <chr>

The same function, without the use of the tidy parameter, illustrating the differences in the variable names and the presentation of information in the first column:

library(hansard)
members_search("abbot", tidy = FALSE)
## Connecting to API
## Retrieving page 1 of 1
## # A tibble: 4 x 12
##                                 `_about`                          homePage
##                                    <chr>                             <chr>
## 1  http://data.parliament.uk/members/172     http://www.dianeabbott.org.uk
## 2 http://data.parliament.uk/members/1651                              <NA>
## 3 http://data.parliament.uk/members/4249 http://www.annemariemorris.co.uk/
## 4 http://data.parliament.uk/members/3827       http://www.judiciary.gov.uk
## # ... with 10 more variables: additionalName._value <chr>,
## #   constituency._about <chr>, constituency.label._value <chr>,
## #   familyName._value <chr>, fullName._value <chr>, gender._value <chr>,
## #   givenName._value <chr>, label._value <chr>, party._value <chr>,
## #   twitter._value <chr>

The same function as above, but with tidy_style = "period.case", so it returns a different

library(hansard)
members_search("abbot", tidy = TRUE, tidy_style = "period.case")
## Connecting to API
## Retrieving page 1 of 1
## # A tibble: 4 x 12
##   mnis.id                         home.page additional.name.value
##     <chr>                             <chr>                 <chr>
## 1     172     http://www.dianeabbott.org.uk                 Julie
## 2    1651                              <NA>             Granville
## 3    4249 http://www.annemariemorris.co.uk/                  <NA>
## 4    3827       http://www.judiciary.gov.uk                Edmond
## # ... with 9 more variables: constituency.about <chr>,
## #   constituency.label.value <chr>, family.name.value <chr>,
## #   full.name.value <chr>, gender.value <chr>, given.name.value <chr>,
## #   label.value <chr>, party.value <chr>, twitter.value <chr>

The search function is not case sensitive, and searchs in the names and constituencies of all MPs and Lords. So even though we spelled her surname incorrectly, we can still find her. This API provides limited biographical details, to retrieve more detailed biographical information, use the mnis package to retrive data from the Members’ Names Information Service.