SNBdata {SNBdata} | R Documentation |
Download Data from the Swiss National Bank (SNB)
Description
Download data (‘tables’ and ‘datasets’) from the Swiss National Bank (SNB) and convert to data-frames.
Usage
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, ...)
Arguments
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 |
Details
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
.
Value
typically a data.frame
,
potentially with attributes additional attributes:
info
-
information about identifiers used by the SNB
columns
-
if
time.series
isTRUE
, the names of the columns used for grouping the rows
Should the download fail, the function returns
NULL
invisibly.
Author(s)
Enrico Schumann
References
for help
on the API,
seehttps://data.snb.ch/en/help#data_api
Examples
## (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