27 Accessing Open Data Canada databases

This section provides code snippets to read, process and visualize various Open Canada databases of high common interest to the GC community. Where possible, an App is developed to help visualizing and understanding the data. See also Shiny Apps section of this book.

27.1 With curl, fread, readxls

Many databases can be accessed via direct link to an .csv, .zip, .xls file, using the code shown below.

# txt
dt <- data.table::fread ("https://health-infobase.canada.ca/src/data/covidLive/vaccination-coverage-byAgeAndSex-overTimeDownload.csv") 

# compressed Rds
curl::curl_download("https://github.com/open-canada/datasets/raw/main/statcan/13100810-20211206.Rds", "13100810-20211206.Rds")
dt <- readRDS("13100810-20211206.Rds") 

# All other archives
download.file("https://www150.statcan.gc.ca/n1/tbl/csv/13100810-eng.zip", temp) # or using curl::curl_download()
dt <- fread(unzip(temp, files = "13100810.csv"))
rm(temp)

27.2 With cansim package

Most Statistics Canada databases can be accessed using cansim package with a code like the one shown below.

See L&L of 2 Dec 2021 dedicated to CAMSIM

Note that when StatCan website is unavailable however (which is often the case), you wont be able to run any cansim commands. Hence, most poupular StatCan datasets, such as those we use for R training and which we building Apps, are also cached at https://github.com/open-canada/datasets, where they can be accessed using the method described above.

27.3 Via API using library(ckanr)

Many databases can also be accessed using ckanr package (e.g. Open Ontario Database), as shown in the code below.

See L&L in June dedicated to CKAN to learn more. See also this blog

res <-resource_show(id ="274b819c-5d69-4539-a4db-f2950794138c")
res$url
## [1] "https://data.ontario.ca/dataset/752ce2b7-c15a-4965-a3dc-397bf405e7cc/resource/274b819c-5d69-4539-a4db-f2950794138c/download/vac_status_hosp_icu.csv"
destfile <- "Hospitalizations by vaccination status.csv"
curl::curl_download(res$url, destfile)
dtHosp_Vac <- fread(destfile)

27.3.1 Via API using library(“rgovcan”)