sage (R package)

The sage package retrieves election results from the Small-Area Global Elections (SAGE) archive. The archive is published as a Parquet dataset partitioned by country and year. Using Apache Arrow, the package therefore reads only the files that a given request requires, instead of the full country-level RDS files.

Installation

install.packages("sage")

# Development version:
# remotes::install_github("noahdasanaike/sage", subdir = "sage_R")

Usage

library(sage)

# Use the public release (or any other location that Arrow can read).
sage_set_source("https://storage.googleapis.com/sage-archive/parquet")
# Or use a local copy of the archive:
# sage_set_source("path/to/parquet")

sage_countries()        # the 134 country names
sage_years("Germany")   # the years with results for Germany
sage_columns()          # the columns in the release

# Results for Germany in 2021, restricted to four columns.
de_2021 <- sage_load(
  "Germany",
  years   = 2021,
  columns = c("party", "votes", "NAME3", "NAME5")
)

# Italian lower-house results for parties matched to Party Facts with high or
# medium confidence.
it_lower <- sage_load(
  "Italy",
  election_type        = "Legislative (lower house)",
  min_match_confidence = "medium"
)

sage_load() returns a tibble. Because the archive is partitioned by country and year, the most common request (one country in one or a few years) requires reading only a few files and completes in seconds.

Election types

Since SAGE v2.0, the archive includes regional, local, and upper-house elections, referendums, and other elections, in addition to national legislative and presidential elections. sage_load() returns all of these elections by default. To restrict the result to particular types of election, set the election_type argument, for instance election_type = "Legislative (lower house)".

Polygons

sage_polygons() returns the polygons for one country as an sf data frame. In countries whose rows contain a polygon_id column (since SAGE v2.0), join the polygons to the results on country, year, and polygon_id. The NAME columns can match two different areas when two elections in the same year used the same names.

Using the package without network access

The package includes a 26 KB excerpt of the archive (Iceland and Samoa), which allows the functions to be used offline. The examples in the help pages use this excerpt, except those marked as requiring network access.

sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
sage_countries()   # "Iceland" "Samoa"

Local copies of the archive

A local copy of the archive should include the _index.csv manifest at the root of the release. Without the manifest, the package opens the directory as a single Arrow dataset, whose schema is taken from a single file. The columns that only some countries contain (candidate, NAME4, and so on) would therefore be dropped.

Development

devtools::document()   # regenerate man/ and NAMESPACE from the roxygen comments
devtools::test()       # offline tests, plus tests against the live archive when it can be reached
devtools::check()