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 data frame. The hansard_generic
function supports the building of API requests in XML, csv or HTML format if required.
install.packages(“hansard”)
install.packages(“devtools”) devtools::install_github(“EvanOdell/hansard”)
library(“hansard”)
There are 29 functions that currently work with the Parliamentary API:
all_answered_questions
bills
commons_answered_questions
commons_divisions
commons_oral_question_times
commons_oral_questions
commons_terms
commons_written_questions
constituencies
early_day_motions
election_results
elections
epetition
hansard_generic
hansard_basic
lords_ammendments
lords_attendance
lords_divisions
lords_vote_record
lords_written_questions
members
members_search
mp_questions
mp_vote_record
papers_laid
publication_logs
research_briefings
sessions_info
tv_programmes
The mp_vote_record
, mp_edms
, members_search
and lords_vote_record
functions are called entirely through function parameters. constituencies
, papers_laid
and publication_logs
only accept one parameter, which is set as the default, and so the function can be called as constituencies()
. All other functions except for hansard_basic
request initial information in the function and then request additional information through console input where required. hansard_basic
is called as an empty function, and then requests console information to walk through the process of requesting data from the API. It is a useful function for users unfamiliar with APIs or with using R to call data from an API.
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")
#> MP.ID additionalName constituency constituencyCode
#> 1 172 Julie Hackney North and Stoke Newington 146966
#> 2 1651 Granville <NA> NA
#> 3 4249 <NA> Newton Abbot 147092
#> 4 3827 Edmond <NA> NA
#> familyName fullName gender givenName
#> 1 Abbott Ms Diane Abbott Female Diane
#> 2 Hodgson Lord Hodgson of Astley Abbotts Male Robin
#> 3 Morris Anne Marie Morris Female Anne Marie
#> 4 Neuberger Lord Neuberger of Abbotsbury Male David
#> homePage
#> 1 http://www.dianeabbott.org.uk
#> 2 <NA>
#> 3 http://www.annemariemorris.co.uk/
#> 4 http://www.judiciary.gov.uk
#> label party
#> 1 Biography information for Ms Diane Abbott Labour
#> 2 Biography information for Lord Hodgson of Astley Abbotts <NA>
#> 3 Biography information for Anne Marie Morris Conservative
#> 4 Biography information for Lord Neuberger of Abbotsbury <NA>
#> twitter
#> 1 https://twitter.com/HackneyAbbott
#> 2 <NA>
#> 3 https://twitter.com/AMMorrisMP
#> 4 <NA>
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.
The commons_divisions
function returns divisions in the House of Commons and the divisions that a member votes no or aye in. The example below returns all Commons Divisions where Diane Abbott voted aye.
x <- commons_divisions("aye")
#> Enter Member ID: 172
#> Retrieving page 1 of 4
#> Retrieving page 2 of 4
#> Retrieving page 3 of 4
#> Retrieving page 4 of 4
#> head(x)
#> _about title
#> 1 http://data.parliament.uk/resources/626911 Opposition Motion: Community Pharmacies
#> 2 http://data.parliament.uk/resources/626953 Opposition Motion: Police Officer Safety
#> 3 http://data.parliament.uk/resources/621252 Opposition Motion: Yemen
#> 4 http://data.parliament.uk/resources/607490 Closure Motion: Sexual Offences (Pardons Etc.) Bill second reading
#> 5 http://data.parliament.uk/resources/582798 Opposition Motion: NHS sustainabiliy and transformation plans
#> 6 http://data.parliament.uk/resources/576587 Finance Bill: Report Stage Amdt 174
#> uin date._value date._datatype
#> 1 CD:2016-11-02:142 2016-11-02 dateTime
#> 2 CD:2016-11-02:143 2016-11-02 dateTime
#> 3 CD:2016-10-26:139 2016-10-26 dateTime
#> 4 CD:2016-10-21:138 2016-10-21 dateTime
#> 5 CD:2016-09-14:134 2016-09-14 dateTime
#> 6 CD:2016-09-06:125 2016-09-06 dateTime
The voting record of MPs and Lords can also be retrieved using the mp_vote_record
and lords_vote_record
functions, respectively. This function has several advantages over the commons_divisions
and lords_divisions
functions. All the required parameters for the API call are included in the function parameters, rather than using console input, and they can return all divisions that an MP or Lord voted in.
x <- mp_vote_record(172, "all")
#> Retrieving aye votes:
#> Connecting to API
#> Retrieving page 1 of 4
#> Retrieving page 2 of 4
#> Retrieving page 3 of 4
#> Retrieving page 4 of 4
#> Retrieving no votes:
#> Connecting to API
#> Retrieving page 1 of 3
#> Retrieving page 2 of 3
#> Retrieving page 3 of 3
#> head(x)
#> _about title
#> 1 http://data.parliament.uk/resources/653644 Opposition Motion: The Government's plan for Brexit (Prime Minister's Amdt)
#> 2 http://data.parliament.uk/resources/653645 Opposition motion: The Government's plan for Brexit motion as amended
#> 3 http://data.parliament.uk/resources/646440 Digital Economy Bill: Report Stage New Clause 8
#> 4 http://data.parliament.uk/resources/641464 Opposition Motion: Education and Social Mobility
#> 5 http://data.parliament.uk/resources/641522 Opposition Motion: National Health Service Funding
#> 6 http://data.parliament.uk/resources/640531 Higher Education and Research Bill: Report Stage New Clause 2
#> uin date._value date._datatype vote
#> 1 CD:2016-12-07:169 2016-12-07 dateTime aye
#> 2 CD:2016-12-07:170 2016-12-07 dateTime aye
#> 3 CD:2016-11-28:165 2016-11-28 dateTime aye
#> 4 CD:2016-11-22:161 2016-11-22 dateTime aye
#> 5 CD:2016-11-22:162 2016-11-22 dateTime aye
#> 6 CD:2016-11-21:154 2016-11-21 dateTime aye
The function research_briefings
is unusual in that it requests names instead of codes.
#> x <- research_briefings('topicSubTopic')
#> Sub-topics are case sensititve. To return list of sub-topics, enter yes.
#> Enter sub-topic: yes
#
#> [1] 'Agriculture, animals, food and rural affairs' 'Asylum, immigration and nationality'
#> [3] 'Business, industry and consumers' 'Communities and families'
#> [5] 'Crime, civil law, justice and rights' 'Culture, media and sport'
#> [7] 'Defence' 'Economy and finance'
#> [9] 'Education' 'Employment and training'
#> [11] 'Energy and environment' 'European Union'
#> [13] 'Health services and medicine' 'Housing and planning'
#> [15] 'International affairs' 'Parliament, government and politics'
#> [17] 'Science and technology' 'Social Security and pensions'
#> [19] 'Social services' 'Transport'
#> Enter Topic. For ease of use, copy and paste the topic: Education
#> Sub-topics are case sensititve. To return list of sub-topics, enter yes.
#> Enter sub-topic: yes
#> [1] "Adult education" "Further education"
#> [3] "Higher education" "Local authorities: education"
#> [5] "Ofsted" "Pre-school education"
#> [7] "Schools" "Special educational needs"
#> [9] "Students" "Teachers"
#> Enter sub-topic. For ease of use, copy and paste the sub-topic: Teachers
The hansard_basic
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 other hansard
functions include "_pageSize=500"
as a suffix in the path to reduce the number of page calls required.