| Title: | Client for Statistics Canada's Open Economic Data |
|---|---|
| Description: | Provides an R client for Statistics Canada's Web Data Service. Users can describe the data they need in natural language, search the official table catalogue, and download complete data tables in English or French as data frames. Tables formerly known as CANSIM tables are identified by Product IDs. Warin (2024) <doi:10.5070/T5.1868>. |
| Authors: | Thierry Warin [aut, cre] (ORCID: <https://orcid.org/0000-0002-5921-3428>) |
| Maintainer: | Thierry Warin <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-07-18 04:40:49 UTC |
| Source: | https://github.com/cran/statcanR |
Downloads a complete table from Statistics Canada's Web Data Service (WDS)
and returns it as a data frame. Product IDs can be supplied in the familiar
hyphenated form (for example, "27-10-0014-01") or as an eight-digit PID
(for example, "27100014").
statcan_data(tableNumber, lang)statcan_data(tableNumber, lang)
tableNumber |
A Statistics Canada table number or Product ID. Both
|
lang |
Language of the downloaded table: |
The function keeps the interface used by earlier statcanR releases. English
and French downloads share the same processing rules. In particular, the
first column is named REF_DATE, coordinates are stored as character, and
the table title from the metadata file is added as INDICATOR.
A data frame containing the complete Statistics Canada table.
## Not run: science <- statcan_data("27-10-0014-01", "eng") science_fr <- statcan_data("27100014", "fra") ## End(Not run)## Not run: science <- statcan_data("27-10-0014-01", "eng") science_fr <- statcan_data("27100014", "fra") ## End(Not run)
Calls statcan_data() and also writes the returned table to a CSV file.
Existing calls with only tableNumber and lang remain supported; path
can be used to select a different output directory.
statcan_download_data(tableNumber, lang, path = ".")statcan_download_data(tableNumber, lang, path = ".")
tableNumber |
A Statistics Canada table number or Product ID. Both
|
lang |
Language of the downloaded table: |
path |
Directory in which to save the CSV file. The directory must already exist. Defaults to the current working directory. |
The downloaded table. The CSV path is available in the
statcan_file attribute of the returned data frame.
## Not run: science <- statcan_download_data("27-10-0014-01", "eng") science <- statcan_download_data( "27-10-0014-01", "eng", path = tempdir() ) attr(science, "statcan_file") ## End(Not run)## Not run: science <- statcan_download_data("27-10-0014-01", "eng") science <- statcan_download_data( "27-10-0014-01", "eng", path = tempdir() ) attr(science, "statcan_file") ## End(Not run)
Interprets a short description of the data needed and returns ranked
Statistics Canada table candidates. The query can contain a topic, Canadian
geography, and reference year or range. For example,
"R&D expenditures in Quebec since 2020" is interpreted as a research and
development expenditure topic, a Quebec geography constraint, and coverage
beginning in 2020.
statcan_find(query, lang = c("eng", "fra"), n = 5L, refresh = FALSE)statcan_find(query, lang = c("eng", "fra"), n = 5L, refresh = FALSE)
query |
One non-empty character string describing the desired data. |
lang |
Language of the table titles: |
n |
Maximum number of candidates to return, from 1 to 20. |
refresh |
Logical; if |
This function finds tables; it does not download or filter their
observations. Use the returned id with statcan_data(), then apply any
required geography and date filters to the downloaded data. Rankings are a
discovery aid, so review the candidate title before downloading a large
table.
The catalogue is cached for 24 hours. When a geography is present in the
query, metadata for a small set of leading candidates is retrieved through
WDS to confirm that the geography is a table member. That metadata is cached
for seven days. If metadata is temporarily unavailable, candidates can
still be returned with geography_match = NA.
A data frame of ranked candidates. id contains the table number,
score is the relevance score, and match_reason explains the ranking.
Coverage dates describe the table as a whole. geography_match is TRUE
when WDS metadata confirms every geography in the query, FALSE when it
does not, and NA when no geography was requested or validation was not
possible.
## Not run: matches <- statcan_find( "R&D expenditures in Quebec since 2020", lang = "eng" ) matches[, c("title", "id", "match_reason")] ## End(Not run)## Not run: matches <- statcan_find( "R&D expenditures in Quebec since 2020", lang = "eng" ) matches[, c("title", "id", "match_reason")] ## End(Not run)
Searches the catalogue of tables published through Statistics Canada's Web Data Service (WDS). The catalogue is cached for 24 hours in the user's cache directory. If WDS is temporarily unavailable, the most recent valid cache is used.
statcan_search(keywords, lang = c("eng", "fra"), refresh = FALSE)statcan_search(keywords, lang = c("eng", "fra"), refresh = FALSE)
keywords |
Character vector of words that must appear in the table title. Matching is case-insensitive. |
lang |
Language of the returned titles: |
refresh |
Logical; if |
A DT::datatable containing matching table titles, Product IDs,
release dates, and language.
## Not run: statcan_search(c("economy", "export"), "eng") statcan_search("population", "fra") ## End(Not run)## Not run: statcan_search(c("economy", "export"), "eng") statcan_search("population", "fra") ## End(Not run)