| Title: | Download Data from the Swiss National Bank (SNB) |
|---|---|
| Description: | Download data (tables and datasets) from the Swiss National Bank (SNB; <https://www.snb.ch/en>), the Swiss central bank. The package is lightweight and comes with few dependencies; suggested packages are used only if data is to be transformed into particular data structures, for instance into 'zoo' objects. Downloaded data can optionally be cached, to avoid repeated downloads of the same files. |
| Authors: | Enrico Schumann [aut, cre] (ORCID: <https://orcid.org/0000-0001-7601-6576>) |
| Maintainer: | Enrico Schumann <[email protected]> |
| License: | GPL-3 |
| Version: | 0.2.1 |
| Built: | 2026-06-03 07:19:00 UTC |
| Source: | https://github.com/enricoschumann/snbdata |
Download data (‘tables’ and ‘datasets’) from the Swiss National Bank (SNB) and convert to data-frames.
fetch_data(id, type = "table", dest.dir = NULL, return.class = NULL, verbose = TRUE, language = "en", name.sep = " :: ", method, na.drop = TRUE, time.series = FALSE, ...) fetch_last_update(id, type = "table", dest.dir = NULL, verbose = TRUE, language = "en", ...) fetch_info(id, type = "table", dest.dir = NULL, verbose = TRUE, language = "en", name.sep = " :: ", method, ...)fetch_data(id, type = "table", dest.dir = NULL, return.class = NULL, verbose = TRUE, language = "en", name.sep = " :: ", method, na.drop = TRUE, time.series = FALSE, ...) fetch_last_update(id, type = "table", dest.dir = NULL, verbose = TRUE, language = "en", ...) fetch_info(id, type = "table", dest.dir = NULL, verbose = TRUE, language = "en", name.sep = " :: ", method, ...)
dest.dir |
file path: where to store the downloaded files? See Details. |
id |
string: identifier of the table/dataset |
type |
string: ‘ |
method |
see |
verbose |
logical: print messages, e.g. about download progress? |
return.class |
|
language |
string: |
... |
passed on to |
name.sep |
string used when pasting description hierarchies |
time.series |
logical. If |
na.drop |
logical. If |
The Swiss National Bank (SNB) provides data as either
so-called tables or datasets. fetch_table can handle
both, but the type argument must be specified.
fetch_table downloads data, which typically are in
csv format, from the SNB's website and
stores them, with a date prefix, in directory dest.dir.
If the latter is NULL, a temporary directory is used
(through tempdir); but much better is to use a
more-persistent storage location. If a file with today's date
exists in dest.dir, that file is read, and nothing is
downloaded.
For downloading, function download.file is used.
See download.file for options; in particular, see
the hints about timeout when downloading large files.
When argument time.series is TRUE,
fetch_table will rearrange the data into time-series.
This requires columns named “Date” and
“Value” in the particular dataset/table. If the
“Date” column has a YYYY-MM-DD pattern,
it will will be transformed with as.Date.
typically a data.frame,
potentially with attributes additional attributes:
infoinformation about identifiers used by the SNB
columnsif time.series is TRUE, the names of the
columns used for grouping the rows
Should the download fail, the function returns
NULL invisibly.
Enrico Schumann
for help on the API,
seehttps://data.snb.ch/en/help#data_api
## (Internet connection required) ### set directory for storing the files. This is ### only an example: Much better is to use a permanent ### storage-location, such as '~/Downloads/SNBdata' data.dir <- tempdir() rates <- fetch_data("rendoblim", type = "table", dest.dir = data.dir, language = "en") ### ==> have data transformed into time-series rates <- fetch_data("rendoblim", type = "table", dest.dir = data.dir, language = "en", time.series = TRUE) if (!is.null(rates)) ## check: if download failed, results ## are NULL attr(rates, "info") stock.markets <- fetch_data("capchstocki", type = "table", dest.dir = data.dir, time.series = TRUE) ## e.g.: stock.markets[, "GDR"] ## total return index## (Internet connection required) ### set directory for storing the files. This is ### only an example: Much better is to use a permanent ### storage-location, such as '~/Downloads/SNBdata' data.dir <- tempdir() rates <- fetch_data("rendoblim", type = "table", dest.dir = data.dir, language = "en") ### ==> have data transformed into time-series rates <- fetch_data("rendoblim", type = "table", dest.dir = data.dir, language = "en", time.series = TRUE) if (!is.null(rates)) ## check: if download failed, results ## are NULL attr(rates, "info") stock.markets <- fetch_data("capchstocki", type = "table", dest.dir = data.dir, time.series = TRUE) ## e.g.: stock.markets[, "GDR"] ## total return index