Package {statcanR}


Type: Package
Title: Client for Statistics Canada's Open Economic Data
Version: 0.3.0
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>.
Depends: R (≥ 4.0.0)
License: MIT + file LICENSE
Encoding: UTF-8
Suggests: knitr, rmarkdown, testthat (≥ 3.0.0)
VignetteBuilder: knitr
Imports: data.table, jsonlite, DT, httr
URL: https://warint.github.io/statcanR/, https://github.com/warint/statcanR
BugReports: https://github.com/warint/statcanR/issues
Config/testthat/edition: 3
Config/Needs/website: pkgdown
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-07-17 16:05:39 UTC; warint
Author: Thierry Warin ORCID iD [aut, cre]
Maintainer: Thierry Warin <thierry.warin@hec.ca>
Repository: CRAN
Date/Publication: 2026-07-17 22:30:02 UTC

Download a Statistics Canada data table

Description

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").

Usage

statcan_data(tableNumber, lang)

Arguments

tableNumber

A Statistics Canada table number or Product ID. Both "27-10-0014-01" and "27100014" are accepted.

lang

Language of the downloaded table: "eng" or "fra".

Details

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.

Value

A data frame containing the complete Statistics Canada table.

Examples

## Not run: 
science <- statcan_data("27-10-0014-01", "eng")
science_fr <- statcan_data("27100014", "fra")

## End(Not run)

Download a Statistics Canada table and save a CSV file

Description

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.

Usage

statcan_download_data(tableNumber, lang, path = ".")

Arguments

tableNumber

A Statistics Canada table number or Product ID. Both "27-10-0014-01" and "27100014" are accepted.

lang

Language of the downloaded table: "eng" or "fra".

path

Directory in which to save the CSV file. The directory must already exist. Defaults to the current working directory.

Value

The downloaded table. The CSV path is available in the statcan_file attribute of the returned data frame.

Examples

## 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)

Find Statistics Canada tables using a natural-language query

Description

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.

Usage

statcan_find(query, lang = c("eng", "fra"), n = 5L, refresh = FALSE)

Arguments

query

One non-empty character string describing the desired data.

lang

Language of the table titles: "eng" or "fra".

n

Maximum number of candidates to return, from 1 to 20.

refresh

Logical; if TRUE, request a fresh catalogue and fresh candidate metadata from WDS.

Details

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.

Value

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.

Examples

## Not run: 
matches <- statcan_find(
  "R&D expenditures in Quebec since 2020",
  lang = "eng"
)
matches[, c("title", "id", "match_reason")]

## End(Not run)

Description

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.

Usage

statcan_search(keywords, lang = c("eng", "fra"), refresh = FALSE)

Arguments

keywords

Character vector of words that must appear in the table title. Matching is case-insensitive.

lang

Language of the returned titles: "eng" or "fra".

refresh

Logical; if TRUE, ignore a fresh cache and request the catalogue from WDS.

Value

A DT::datatable containing matching table titles, Product IDs, release dates, and language.

Examples

## Not run: 
statcan_search(c("economy", "export"), "eng")
statcan_search("population", "fra")

## End(Not run)