rTrafa is an R package for discovering, inspecting and downloading Swedish transport statistics from the Trafa API — the open database maintained by Trafikanalys, Sweden’s agency for transport analysis. It covers road, rail, sea and air transport; vehicles in traffic; new registrations; passenger and freight volumes; and more.
To learn more about using rTrafa, it is recommended you use the following resources in order:
Getting started with rTrafa
- To get up and running quickly with rTrafa, please see the vignette A quick start guide to rTrafa.
- For an introduction to rTrafa and the design principles of functions included, please see Introduction to rTrafa.
- See the Reference section of the package homepage to learn about the full set of functionality included with the package.
rTrafa is open source licensed under the Affero Gnu Public License version 3. This means you are free to download the source, modify and redistribute it as you please, but any copies or modifications must retain the original license. Please see the file LICENSE.md for further information.
Installation
rTrafa is on CRAN. To install it, run the following code in R:
install.packages("rTrafa")To install the latest development version from GitHub, use the remotes package:
Quick start
library(rTrafa)
# List the available statistical products
products <- get_products()
# Inspect the measures available for "Buses" (product t10011)
measures <- get_measures("t10011")
# Download "Vehicles in traffic" for the last ten years
bus_data <- get_data("t10011", "itrfslut",
ar = as.character(2016:2025)
)Features
- Search-then-fetch workflow: Discover products, inspect measures and dimensions, then download data
-
Three-level data model: Products → measures → dimensions, mirrored by
get_products(),get_measures()andget_dimensions() -
Filter shortcuts: Use
ar = "senaste"for the latest period without hardcoding years -
Dimension validation:
prepare_query()checks that your filters are compatible with the selected measure before hitting the data endpoint -
Offline-safe examples:
trafa_available()guards all network calls so examples and tests degrade gracefully when the API is down
Enhanced caching with nordstatExtras
For multi-user web applications or workflows that benefit from a shared, persistent cache, rTrafa integrates with the nordstatExtras package. When installed, get_data(), get_products(), and other functions can write to a shared SQLite file instead of per-session .rds files:
# install.packages("devtools")
devtools::install_github("LCHansson/nordstatExtras")
library(nordstatExtras)
handle <- nxt_open("cache.sqlite")
# Data and metadata are cached in the same SQLite file
products <- get_products(cache = TRUE, cache_location = handle)
bus_data <- get_data("t10011", "itrfslut",
ar = as.character(2020:2025),
cache = TRUE, cache_location = handle
)
nxt_close(handle)Features include cell-level deduplication across overlapping queries, cross-query freshness propagation, and FTS5-powered typeahead search via nxt_search(). See the nordstatExtras README for details.
Contributing
You are welcome to contribute to the further development of the rTrafa package in any of the following ways:
- Open an issue
- Clone this repo, make modifications and create a pull request
- Spread the word!
Related packages
rTrafa is part of a family of R packages for Swedish and Nordic open statistics that share the same design philosophy — tibble-based, pipe-friendly, and offline-safe:
- rKolada — R client for the Kolada database of Swedish municipal and regional Key Performance Indicators
- pixieweb — R client for PX-Web APIs (Statistics Sweden, Statistics Norway, Statistics Finland, and more)
Code of Conduct
Please note that the rTrafa project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
