Code Monkey home page Code Monkey logo

parlitools's Introduction

License: MIT CRAN_Status_Badge GitHub tag Travis-CI Build Status AppVeyor Build Status Coverage Status DOI

parlitools

A collection of useful tools for UK politics, including base maps and datasets. Initially inspired by Bhaskar Karambelkar’s tilegrams package, but with the ability to create a hexagonal map of UK parliamentary constituencies and local authorities. The package also includes functions for data retrieval of current MPs and their consituency details (as it requires calls to two different APIs, this function is not included in my hansard or mnis packages), and local government composition. There are inbuilt datasets from the 2015 and 2017 UK General Elections and 2011 Census, courtesy of the British Election Study, estimates of EU referendum votes from Chris Hanretty, and hex codes for different party colours.

The most up-to-date documentation will always be at http://docs.evanodell.com/parlitools/.

Installing

parlitools is available on CRAN. To install on CRAN run:

install.packages("parlitools")

Or, if you use pacman:

pacman::p_load(parlitools)

To install the development version from GitHub, run:

##install.packages("parlitools")
devtools::install_github("evanodell/parlitools")

Functions and Data

For more details see the full documentation or the vignettes.

Included Data

party_colour - A tibble with the ID, name and hex code for the official colour of a variety of political parties, taken from Wikipedia. Includes all political parties with MPs and a number without MPs. (Sources: https://en.wikipedia.org/wiki/Wikipedia:Index_of_United_Kingdom_political_parties_meta_attributes, mnis::ref_parties())

bes_2015 - A tibble with the British Election Study 2015 Constituency Results Version 2.2. For information on all the variables in this dataset, see the bes-2015 vignette (Source: http://www.britishelectionstudy.com/data-object/2015-bes-constituency-results-with-census-and-candidate-data/)

bes_2017 - A tibble with Great Britain constituencies results from the 2017 general election. This data can be linked to 2011 census information in census_11.

census_11 - A tibble with constituency-level census data.

leave_votes_west - The percentage of votes cast for leave in the 2016 EU referendum. Some constituencies have actual results and others only have estimates by Chris Hanretty; in cases where the actual cote count is known, both the estimates and the actual results are reported. (Sources: Hanretty, C. (2017). Areal interpolation and the UK’s referendum on EU membership. Journal of Elections, Public Opinion and Parties, 27(4), 466–483. https://doi.org/10.1080/17457289.2017.1287081, http://www.bbc.co.uk/news/uk-northern-ireland-36616830)

Data Retrieval Functions

current_mps - Uses functions from hansard and mnis to create a tibble with data on all current MPs, their party affiliation and their constituency.

mps_on_date - Uses functions from hansard and mnis to create a tibble with data on all MPs from a given date, their party affiliation and their constituency.

west_hex_map - A hexagonal cartogram, stored as a simple feature and data frame, of Westminster parliamentary constituencies. west_hex_map can be used to create maps like this:

local_hex_map - Hexagonal cartogram, A hexagonal cartogram, stored as a simple feature and data frame, of all Local Authorities in England, Wales and Scotland.

Using parlitools

For more details, please see the introductory vignette, using parlitools with cartogram, mapping local authorities and the vignette detailing British Election Study 2015 variables.

Data Sources

There are a variety of potentially relevant data sources and datasets on UK politics, far too many for me to include them all in this package.

  • Electoral Commission

    • Electoral results dating back to 2005.
  • British Election Study

    • A large selection of open data, including panel surveys, linked data and aggregated Twitter data, covering elections and referenda.
  • My hansard & mnis data retrieval packages for parliamentary APIs.

  • Open Council Data has data on the names, parties, and wards of all UK councillors, updated more or less weekly. parlitools uses this site to power the council_seats() function.

parlitools's People

Contributors

dependabot[bot] avatar evanodell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parlitools's Issues

CRS error

Hello, I'm getting an CRS error when joining data to the base map -- is there a workaround for this? thank you!

> west_trump_no <- dplyr::left_join(west_hex_map, trump_no, by = "gss_code") #Joining to base map

gives error:

old-style crs object detected; please recreate object with a recent sf::st_crs()

Parlitool error: Error in open.connection(con, "rb") : HTTP error 404

Hello.

My name is James Prentice and I am a Sussex University PhD student. I am currently studying British politics elections and have been using the parlitools package on R to create maps for sometime now and it is great. I have never had any problems with the package until today. I thought I would contact you as your name was on the package details document I found online.

Today I have got the error message of

“ Error in open.connection(con, "rb") : HTTP error 404 “

when trying to download the constituency data. Have others had this issue recently? Could it be the location of the data has moved its web address location or could this be a problem with my internet/ computer? Sadly this error message prevents my code producing the maps I have produced as the MP data can not be downloaded and the colours on the map can not be attached to them.

I have downloaded all the updates for the packages and have restarted my computer and internet connection to make sure it was nothing basic like this and the same error message is produced.

If you have any time to get back to me I would be very grateful.

Problem loading Parlitools Package

Hi,

I am a student and have been working on some UK elections analyses recently, and to aid in doing this I have been attempting to use your package. Unfortunately, after having seemingly succeeded in installing it, when I try to load the package I get:

Error: package or namespace load failed for ‘parlitools’ in namespaceExport(ns, exports):
undefined exports: st_agr<-, st_crs<-, st_geometry<-, st_precision<-, .degAxis, .get_layout, .image_scale, .image_scale_factor, .stop_geos, NA_agr_, NA_bbox_, NA_crs_, NA_m_range_, NA_z_range_, as_Spatial, gdal_crs, gdal_inv_geotransform, gdal_metadata, gdal_polygonize, gdal_rasterize, gdal_read, gdal_subdatasets, gdal_utils, gdal_write, get_key_pos, plot_sf, rawToHex, read_sf, sf.colors, sf_add_proj_units, sf_extSoftVersion, sf_proj_info, sf_project, st_agr, st_area, st_as_binary, st_as_grob, st_as_sf, st_as_sfc, st_as_text, st_axis_order, st_bbox, st_bind_cols, st_boundary, st_buffer, st_cast, st_centroid, st_collection_extract, st_combine, st_contains, st_contains_properly, st_convex_hull, st_coordinates, st_covered_by, st_covers, st_crop, st_crosses, st_crs, st_difference, st_dimension, st_disjoint, st_distance, st_drivers, st_drop_geometry, st_equals, st_equals_exact, st_filter, st_geometry

I also get a warning for some S3 methods as well.

I have tried various methods but nothing seems to have solved it. Of course this may well not be the right place to put this (am new to this so apologies if so), but I hoped as the person who created the package you would have some insight. So if you are able to offer any advice I would be most grateful.

Bug in `current_mps()` and `council_seats()`

I just installed parlitools from CRAN and attempted to run the current_mps() function but received an error.

I ran this code:

library(parlitools)
current_mps <- parlitools::current_mps()

And received this error:

Error:
! Assigned data `as.character(df$DateOfBirth)` must be compatible with existing data.
✖ Existing data has 650 rows.
✖ Assigned data has 0 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Unknown or uninitialised column: `DateOfBirth`. 

And this is the related output of rlang::last_trace():

Backtrace:
     ▆
  1. ├─parlitools::current_mps()
  2. │ ├─base::`$<-`(`*tmp*`, "DateOfBirth", value = `<chr>`)
  3. │ └─tibble:::`$<-.tbl_df`(`*tmp*`, "DateOfBirth", value = `<chr>`)
  4. │   └─tibble:::tbl_subassign(...)
  5. │     └─tibble:::vectbl_recycle_rhs_rows(...)
  6. │       ├─base::withCallingHandlers(...)
  7. │       └─vctrs::vec_recycle(value[[j]], nrow)
  8. ├─vctrs:::stop_recycle_incompatible_size(...)
  9. │ └─vctrs:::stop_vctrs(...)
 10. │   └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))
 11. │     └─rlang:::signal_abort(cnd, .file)
 12. │       └─base::signalCondition(cnd)
 13. └─tibble (local) `<fn>`(`<vctrs___>`)
 14.   └─rlang::cnd_signal(...)

I also attempted to call council_seats() and received the following error:

> council_seats <- council_seats(councillors = TRUE)
Error in `council_data[c("id", "ons code", "name", "type")]`:                                                                                               
! Can't subset columns that don't exist.
✖ Column `type` doesn't exist.

And this is the related output of rlang::last_trace():

Backtrace:
     ▆
  1. ├─parlitools::council_seats(councillors = TRUE)
  2. │ ├─council_data[c("id", "ons code", "name", "type")]
  3. │ ├─readr:::`[.spec_tbl_df`(...)
  4. │ ├─base::NextMethod(`[`)
  5. │ └─tibble:::`[.tbl_df`(council_data, c("id", "ons code", "name", "type"))
  6. │   └─tibble:::vectbl_as_col_location(...)
  7. │     ├─tibble:::subclass_col_index_errors(...)
  8. │     │ └─base::withCallingHandlers(...)
  9. │     └─vctrs::vec_as_location(j, n, names, call = call)
 10. └─vctrs (local) `<fn>`()
 11.   └─vctrs:::stop_subscript_oob(...)
 12.     └─vctrs:::stop_subscript(...)
 13.       └─rlang::abort(...)

Possibly helpful for debugging, this is my sessionInfo():

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Ventura 13.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] parlitools_0.4.1 kableExtra_1.3.4 forcats_0.5.2    stringr_1.5.0    dplyr_1.0.10     purrr_0.3.5      readr_2.1.3      tidyr_1.2.1      tibble_3.1.8    
[10] ggplot2_3.4.0    tidyverse_1.3.2 

I would submit a PR but I don't know how to debug this! Sorry. Just flagging as it seems like this is a problem with the package

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.