| Type: | Package |
| Title: | Retrieve Results from the Small-Area Global Elections Archive |
| Version: | 0.2.1 |
| Description: | Retrieves election results from the Small-Area Global Elections (SAGE) archive, a harmonized collection of results at the level of the polling station and precinct, without reading the complete country-level files. The package reads the 'Parquet' release, which is partitioned by country and year, from a public bucket or from a source set using an environment variable, sage_set_source(), or a local copy of the archive. The function sage_load() returns the results for a given country, years, and columns as a 'tibble'. Further functions retrieve the boundary polygons and the votes received by individual candidates. See https://github.com/noahdasanaike/sage for the archive itself. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Language: | en-US |
| Depends: | R (≥ 4.0) |
| Imports: | arrow (≥ 14.0.0), dplyr, rlang, tibble, utils |
| Suggests: | curl, sf, sfarrow, testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| RoxygenNote: | 7.2.3 |
| URL: | https://github.com/noahdasanaike/sage |
| BugReports: | https://github.com/noahdasanaike/sage/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-12-01 15:34:14 UTC; Noah |
| Author: | Noah Dasanaike [aut, cre, cph], Dmitry Shkolnik [ctb] |
| Maintainer: | Noah Dasanaike <dasanaike.noah@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-12-01 15:50:02 UTC |
Retrieve Election Results from the Small-Area Global Elections (SAGE) Archive
Description
The functions in this package retrieve election results from the
Small-Area Global Elections (SAGE) archive, which is published as a Parquet
dataset partitioned by country and year. By default, the package reads the
public release in a Google Cloud Storage bucket. A different source can be
set using sage_set_source() or the SAGE_SOURCE environment
variable. Because of the partitioning, a request for one country in a few
years requires reading only the corresponding files, instead of the full
country-level RDS files.
Details
Remote files are downloaded over HTTPS, one partition at a time, using the
_index.csv manifest to locate them. The package therefore works with
any build of arrow and does not require that the S3 or GCS filesystem
of Arrow be compiled in.
When the source is the public bucket and the installed arrow includes
GCS support, sage_columns() reads only the schemas of the Parquet
files, using the random-access reader of Arrow, instead of downloading whole
partitions. If the GCS filesystem or an individual read is unavailable, the
function downloads the partitions over HTTPS instead.
Source layout
The release is a Parquet dataset partitioned by country and year:
<source>/ country=Afghanistan/year=2014/part-0.parquet country=Afghanistan/year=2019/part-0.parquet country=Albania/year=2009/part-0.parquet ... _index.csv
A request for a single country and year therefore requires reading a single file.
Offline excerpt
The package includes a small excerpt of the archive (Iceland and Samoa) in
extdata/mini_archive. The examples in this documentation use this
excerpt, except those marked as requiring network access, and therefore run
offline:
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
To use the full release, set the source to
"gs://sage-archive/parquet".
List the columns in the archive
Description
Not every column is populated for every country. The function returns the
union of the columns of the largest partitions of six countries, which
includes the optional columns of deeper administrative hierarchies
(NAME4, NAME5, and so on). The result is cached for the
session and discarded when a new source is set using sage_set_source().
Usage
sage_columns()
Value
A character vector of column names, with "country" and
"year" first.
See Also
sage_load(), whose columns argument accepts these names.
Examples
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
sage_columns()
List the countries in the archive
Description
Reads the _index.csv manifest of the archive. The function therefore
requires a single small request instead of a listing of the whole bucket.
Usage
sage_countries()
Value
A sorted character vector of country names.
See Also
Examples
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
sage_countries()
Load the election results for one country
Description
Reads only the partitions required for the request, one file per country and year, and returns them as a single tibble.
Usage
sage_load(
country,
years = NULL,
columns = NULL,
election_type = NULL,
min_match_confidence = NULL,
drop_all_na = TRUE
)
Arguments
country |
A single country name. Required. |
years |
An optional integer vector of years. By default, all available years are returned. |
columns |
An optional character vector of column names. By default,
all columns are returned. The |
election_type |
An optional character vector of election types (for
instance, |
min_match_confidence |
If not |
drop_all_na |
If |
Details
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. All of these elections are returned
by default. Use election_type to restrict the result to particular
types of election.
Value
A tibble, with country and year as its first two
columns.
See Also
sage_columns() for the available columns, and sage_polygons()
for the boundaries.
Examples
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
is_2020 <- sage_load("Iceland", years = 2020)
dim(is_2020)
names(is_2020)
# Restrict the result to two columns. The country and year columns are
# always included.
sage_load("Iceland", years = 2020, columns = c("party", "votes"))
## Not run:
# Using the full release (requires network access).
sage_set_source("gs://sage-archive/parquet")
de_2021 <- sage_load("Germany", years = 2021, columns = c("party", "votes", "NAME3"))
de_ep <- sage_load("Germany", election_type = "European Parliament")
de_high <- sage_load("Germany", min_match_confidence = "high")
## End(Not run)
Load the polygons for one country
Description
In addition to the main Parquet release, SAGE publishes a separate
polygons/ directory with one GeoParquet file per country. Each
polygon file contains only the key columns, year, and
geometry, with one row per distinct polygon. The function is intended
for mapping. The rows returned by sage_load() contain the latitude
and longitude of each unit but no boundaries.
Usage
sage_polygons(country, years = NULL)
Arguments
country |
Country name. |
years |
An optional integer vector of years. |
Details
In countries whose rows contain a polygon_id column (since SAGE
v2.0), join the polygons to the rows returned by sage_load() 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. In the remaining countries, join on year and the
NAME columns that the polygon file contains.
Requires the sf and sfarrow packages.
Value
An sf data frame, or NULL (invisibly) if the source
contains no polygons for the country.
See Also
sage_load() for the results to which these polygons are joined.
Examples
## Not run:
# Requires network access, as well as the sf and sfarrow packages. Polygons
# are not part of the excerpt included in the package.
sage_set_source("gs://sage-archive/parquet")
is_poly <- sage_polygons("Iceland", years = 2021)
## End(Not run)
Load the votes received by individual candidates
Description
For Germany and the Netherlands, the archive also contains the votes received by individual candidates, in addition to the party totals in the main release. For Germany, these are first votes (Erststimmen) by constituency (Wahlkreis). For the Netherlands, they are preference votes by polling station (stembureau). For any other country, the function returns an error.
Usage
sage_preference_votes(country)
Arguments
country |
Country name: |
Value
A tibble of votes by candidate.
See Also
sage_load() for the results by party.
Examples
## Not run:
# Requires network access. These files are not part of the excerpt included
# in the package.
sage_set_source("gs://sage-archive/parquet")
de_cand <- sage_preference_votes("Germany")
## End(Not run)
Set the location of the SAGE archive
Description
Sets the archive from which the other functions in the package read. The
setting applies to the current session and remains in place until it is
changed. If no source has been set, the package uses the
SAGE_SOURCE environment variable and, if that variable is also unset,
the public bucket.
Usage
sage_set_source(source)
Arguments
source |
A local directory or a remote URI, such as
|
Value
The source, invisibly.
See Also
sage_load(), which reads from the source set here.
Examples
# Use the excerpt of the archive that is included in the package.
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
sage_countries()
## Not run:
# Use the full public release (requires network access).
sage_set_source("gs://sage-archive/parquet")
## End(Not run)
List the election years available for a country
Description
List the election years available for a country
Usage
sage_years(country)
Arguments
country |
Country name, as returned by |
Value
A sorted integer vector of years. For a country that is not in the source, the vector has length zero.
See Also
Examples
sage_set_source(system.file("extdata", "mini_archive", package = "sage"))
sage_years("Iceland")