~enricoschumann/SNBdata

8209c712d8a27910143d49aa1b8c567c156ccaaa — Enrico Schumann 11 months ago e29a8f4
Update documentation
4 files changed, 113 insertions(+), 34 deletions(-)

M DESCRIPTION
M NEWS
M R/functions.R
M man/SNBdata.Rd
M DESCRIPTION => DESCRIPTION +1 -1
@@ 2,7 2,7 @@ Package: SNBdata
Type: Package
Title: Download Data from the Swiss National Bank (SNB)
Version: 0.2.0
Date: 2023-09-18
Date: 2023-09-20
Maintainer: Enrico Schumann <es@enricoschumann.net>
Authors@R: person("Enrico", "Schumann",
                  role  = c("aut", "cre"),

M NEWS => NEWS +7 -2
@@ 1,11 1,16 @@
v0.2.0  (2023-09-18; not released yet)
v0.2.0  (2023-09-20; not released yet)

  o New functions "fetch_data", "fetch_last_update" and
    "fetch_info", which replace the previous functions
    that worked for only tables or datasets.

  o Retrieved data get an attribute "dimensions", which
    includes the available information about
    identifiers/ code lists.  (If package "jsonlite" is
    available, that information is properly arranged.)

  o Experimental: "fetch_data" gains an argument
    "do.timeseries". If TRUE, tables and datasets are
    "timeseries".  If TRUE, tables and datasets are
    transformed into time-series.

  o ....

M R/functions.R => R/functions.R +10 -10
@@ 3,11 3,11 @@ fetch_data <- function(id,
                       dest.dir = NULL,
                       return.class = NULL,
                       verbose = TRUE,
                       method,
                       language = "en",
                       name.sep = " :: ",
                       method,
                       na.drop = TRUE,
                       do.timeseries = FALSE, ...) {
                       timeseries = FALSE, ...) {

    if (type == "table") {
        site <- paste0("https://data.snb.ch/api/cube/",


@@ 17,7 17,7 @@ fetch_data <- function(id,
                       gsub("@", ".", id),
                       "/data/csv/", language)
    } else {
        stop("either table of dataset must be specified")
        stop("type must be either table or dataset")
    }

    info <- fetch_info(id = id, type = type,


@@ 74,13 74,14 @@ fetch_data <- function(id,
                       stringsAsFactors = FALSE,
                       as.is = TRUE, skip = empty, ...)

    if (do.timeseries) {
    if (timeseries) {
        date.col <- grep("Date", colnames(dats))
        if (!length(date.col)) {
            message("no ", sQuote("Date"),
                    " column: cannot create timeseries")
        value.col <- grep("Value", colnames(dats))
        if (!length(date.col) || !length(value.col)) {
            message("both ", sQuote("Date"),
                    " and ", sQuote("Value"),
                    " column required for timeseries")
        } else {
            value.col <- grep("Value", colnames(dats))
            other.col <- setdiff(colnames(dats),
                                 colnames(dats)[c(date.col,
                                                  value.col)])


@@ 143,7 144,7 @@ fetch_data <- function(id,

    }

    attr(result, "info") <- unlist(info)
    attr(result, "dimensions") <- unlist(info)
    result
}



@@ 151,7 152,6 @@ fetch_data <- function(id,

fetch_last_update <- function(id,
                              type = "table",
                              dataset,
                              dest.dir = NULL,
                              verbose = TRUE,
                              language = "en", ...) {

M man/SNBdata.Rd => man/SNBdata.Rd +95 -21
@@ 11,24 11,29 @@
\description{

  Download datasets from the Swiss National Bank (SNB)
  and convert to data frames.
  and convert to data-frames.

}
\usage{
fetch_data(id, type = "table",
           dest.dir = NULL,
fetch_data(id,
           type = "table", dest.dir = NULL,
           return.class = NULL, verbose = TRUE,
           method,
           language = "en",
           name.sep = " :: ",
           na.drop = TRUE, ...)
           method,
           na.drop = TRUE,
           timeseries = FALSE, ...)

fetch_last_update(id,
                  dest.dir = NULL,
                  verbose = TRUE,
                  language = "en", ...)
fetch_info(id, dest.dir = NULL,
           return.class = NULL, verbose = TRUE,
           language = "en", method, ...)
                  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{
  \item{dest.dir}{%


@@ 46,9 51,9 @@ fetch_info(id, dest.dir = NULL,
  \item{verbose}{%
    logical: print messages, e.g. about download progress?
  }
  \item{return.class}{\code{NULL} or character:
      not yet implemented (but in future versions,
    \code{\link[zoo]{zoo}} will be supported)
  \item{return.class}{%
    \code{NULL} or character: not yet implemented (but in
    future versions, \code{\link[zoo]{zoo}} will be supported)
  }
  \item{language}{%
    string: \code{en}, \code{fr}, or \code{de}


@@ 59,19 64,49 @@ fetch_info(id, dest.dir = NULL,
  \item{name.sep}{%
    string used when pasting description hierarchies
  }
  \item{timeseries}{%
    logical. If \code{TRUE}, data are transformed into
    time-series.
  }
  \item{na.drop}{%
    logical. If \code{TRUE}, rows with no finite values at all
    are dropped.
    are dropped.  Only used if \code{timeseries} is
    \code{TRUE}.
  }
}
\details{

  The \acronym{SNB} provides data as tables and datasets.
  The Swiss National Bank (SNB) provides data as either
  so-called tables or datasets.  \code{fetch_table} can handle
  both, but the \code{type} argument must be specified.

  \code{fetch_table} downloads data, which typically are in
  CSV format, from the SNBs's website and stores them, with a
  date prefix, in directory \code{dest.dir}.  If the latter is
  \code{NULL}, a temporary directory is used (through
  \code{\link{tempdir}}); but much better is to use a
  more-persistant storage location.  If a file with today's
  date exists in \code{dest.dir}, that file is read, and
  nothing is downloaded.

  When argument \code{timeseries} is \code{TRUE},
  \code{fetch_table} will rearrange the data into time-series.
  This requires columns named \dQuote{\code{Date}} and
  \dQuote{\code{Value}} in the particular dataset/table.

}
\value{
  typically a \code{\link{data.frame}}.

  typically a \code{\link{data.frame}},
  potentially with attributes additional attributes:
  \describe{
    \item{\code{dimensions}}{%
      information about identifiers used by the SNB
    }
    \item{\code{columns}}{%
      if \code{timeseries} is \code{TRUE}, the names of
      the columns used for grouping the rows
    }
  }
}
\references{
  \url{https://data.snb.ch/en/}


@@ 89,7 124,46 @@ fetch_info(id, dest.dir = NULL,
\examples{
\donttest{
## (Internet connection required)
fetch_table("rendoblim",
            dest.dir = "~/Downloads/SNBdata/",
            language = "de")

### 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")
##      Date D0 Value
## 1 1988-01 1J 2.887
## 2 1988-01 2J 3.218
## 3 1988-01 3J 3.393
## 4 1988-01 4J 3.554
## 5 1988-01 5J 3.695
## ....

### transform into time-series
rates <- fetch_data("rendoblim",
                    type = "table",
                    dest.dir = data.dir,
                    language = "en",
                    timeseries = TRUE)
##      Date    1J    2J    3J ....
## 1 1988-01 2.887 3.218 3.393 ....
## 2 1988-02 2.638 2.990 3.271 ....
## 3 1988-03 2.641 3.263 3.573 ....
## ....

attr(rates, "dimensions")
##                                                           1J
##  "Overview :: CHF Swiss Confederation bond issues :: 1 year"
##                                                           2J
## "Overview :: CHF Swiss Confederation bond issues :: 2 years"
## ....

stock.markets <- fetch_data("capchstocki",
                            type = "table",
                            dest.dir = data.dir,
                            timeseries = TRUE)
stock.markets[, "GDR"]  ## total return index
}}