~enricoschumann/SNBdata

dc479dfce77e34f400163ddf5bc930a4ed58d5d6 — Enrico Schumann 11 months ago 8209c71
Update documentation
4 files changed, 24 insertions(+), 31 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-20
Date: 2023-10-10
Maintainer: Enrico Schumann <es@enricoschumann.net>
Authors@R: person("Enrico", "Schumann",
                  role  = c("aut", "cre"),

M NEWS => NEWS +2 -2
@@ 1,4 1,4 @@
v0.2.0  (2023-09-20; not released yet)
v0.2.0  (2023-10-10; not released yet)

  o New functions "fetch_data", "fetch_last_update" and
    "fetch_info", which replace the previous functions


@@ 10,7 10,7 @@ v0.2.0  (2023-09-20; not released yet)
    available, that information is properly arranged.)

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

  o ....

M R/functions.R => R/functions.R +6 -2
@@ 7,7 7,7 @@ fetch_data <- function(id,
                       name.sep = " :: ",
                       method,
                       na.drop = TRUE,
                       timeseries = FALSE, ...) {
                       time.series = FALSE, ...) {

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


@@ 71,10 71,11 @@ fetch_data <- function(id,
    dats <- read.table(text = dats,
                       sep = ";",
                       header = TRUE,
                       quote = 
                       stringsAsFactors = FALSE,
                       as.is = TRUE, skip = empty, ...)

    if (timeseries) {
    if (time.series) {
        date.col <- grep("Date", colnames(dats))
        value.col <- grep("Value", colnames(dats))
        if (!length(date.col) || !length(value.col)) {


@@ 87,6 88,9 @@ fetch_data <- function(id,
                                                  value.col)])

            dates <- sort(unique(dats[, date.col]))
            if (all(grepl("[12][0-9]{3}-[0-9]{2}-[0-9]{2}",
                          dates, perl = TRUE)))
                dates <- as.Date(dates)
            if (length(other.col) > 1L)
                groups <- apply(dats[, other.col], 1, paste0,
                                collapse = name.sep)

M man/SNBdata.Rd => man/SNBdata.Rd +15 -26
@@ 22,7 22,7 @@ fetch_data(id,
           name.sep = " :: ",
           method,
           na.drop = TRUE,
           timeseries = FALSE, ...)
           time.series = FALSE, ...)

fetch_last_update(id,
                  type = "table", dest.dir = NULL,


@@ 64,7 64,7 @@ fetch_info(id,
  \item{name.sep}{%
    string used when pasting description hierarchies
  }
  \item{timeseries}{%
  \item{time.series}{%
    logical. If \code{TRUE}, data are transformed into
    time-series.
  }


@@ 89,10 89,13 @@ fetch_info(id,
  date exists in \code{dest.dir}, that file is read, and
  nothing is downloaded.

  When argument \code{timeseries} is \code{TRUE},
  When argument \code{time.series} 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.
  \dQuote{\code{Value}} in the particular dataset/table.  If
  the \dQuote{\code{Date}} column has a \code{YYYY-MM-DD}
  pattern, it will will be transformed with
  \code{\link{as.Date}}.

}
\value{


@@ 134,36 137,22 @@ 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

### ==> have data transformed 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"
## ....

if (!is.null(rates))  ## check: if download failed, results
                      ##        are NULL
    attr(rates, "dimensions")


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