Introduction to Hansard

Evan Odell

2016-12-13

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.

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

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.

Using commons_divisions and mp_vote_record

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

Using research_briefings

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

Using the hansard_basic function

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.