A => .gitignore +2 -0
A => DESCRIPTION +22 -0
@@ 1,22 @@
+Package: SNBdata
+Type: Package
+Title: Download Data from the Swiss National Bank (SNB)
+Version: 0.0-1
+Date: 2023-05-14
+Maintainer: Enrico Schumann <es@enricoschumann.net>
+Authors@R: person("Enrico", "Schumann",
+ role = c("aut", "cre"),
+ email = "es@enricoschumann.net",
+ comment = c(ORCID = "0000-0001-7601-6576"))
+Description: Functions for downloading data from the Swiss
+ National Bank (SNB; <https://www.snb.ch/>), 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.
+Suggests: jsonlite,zoo
+License: GPL-3
+URL: http://enricoschumann.net/R/packages/SNBdata/
+LazyLoad: yes
A => NAMESPACE +6 -0
@@ 1,6 @@
+importFrom("utils", "askYesNo", "download.file", "read.table", "unzip")
+export(
+ "fetch_table",
+ "fetch_table_date",
+ "fetch_table_info"
+)
A => NEWS +4 -0
@@ 1,4 @@
+v0.0-1 (2023-05-14; not yet released)
+
+ o initial version: allows to download tables and datasets
+ from the SNB website; see https://data.snb.ch
A => R/functions.R +121 -0
@@ 1,121 @@
+
+## https://data.snb.ch/en/help#data_api
+## https://data.snb.ch/de/topics/snb/cube/snbbipo
+
+## https://data.snb.ch/en/topics/ziredev/cube/rendoblid
+## https://data.snb.ch/api/cube/rendoblid/dimensions/en
+
+fetch_table_date <- function(table = "rendoblid",
+ dest.dir = NULL,
+ verbose = TRUE,
+ language = "en", ...) {
+
+
+ if (!is.null(dest.dir))
+ message("currently not supported")
+
+ site <- paste0("https://data.snb.ch/api/cube/", table, "/lastUpdate")
+ con <- url(site)
+ ans <- try(readLines(con, warn = FALSE), silent = TRUE)
+ try(close(con), silent = TRUE)
+ em <- geterrmessage()
+ jsonlite::fromJSON(ans)
+}
+
+fetch_table_info <- function(table = "rendoblid",
+ dest.dir = NULL,
+ return.class = NULL,
+ verbose = TRUE,
+ language = "en", ...) {
+
+ if (!is.null(dest.dir))
+ message("currently not supported")
+
+ site <- paste0("https://data.snb.ch/api/cube/", table, "/dimensions/", language)
+
+ con <- url(site)
+ dats <- try(readLines(con, warn = FALSE), silent = TRUE)
+ close(con)
+ em <- geterrmessage()
+ jsonlite::fromJSON(dats, FALSE)
+}
+
+fetch_table <- function(table = "rendoblid",
+ dest.dir = NULL,
+ return.class = NULL,
+ verbose = TRUE,
+ method, ...) {
+
+ site <- paste0("https://data.snb.ch/api/cube/",
+ table, "/data/csv/en")
+
+ if (!is.null(dest.dir)) {
+ filename <- paste0(format(Sys.Date(), "%Y%m%d"),
+ "__", table,
+ ".csv")
+ filename <- file.path(dest.dir, filename)
+ if (!file.exists(filename)) {
+ if (verbose)
+ message("Downloading data from SNB ... ", appendLF = FALSE)
+ download.file(url = site, destfile = filename,
+ method = method,
+ quiet = TRUE)
+ } else
+ if (verbose)
+ message("Using cache ... ", appendLF = FALSE)
+
+ dats <- try(readLines(filename), silent = TRUE)
+ em <- geterrmessage()
+
+ } else {
+ if (verbose)
+ message("Downloading data from SNB ... ", appendLF = FALSE)
+
+ con <- url(site)
+ dats <- try(readLines(con), silent = TRUE)
+ close(con)
+ em <- geterrmessage()
+ }
+
+ if (inherits(dats, "try-error")) {
+ if (verbose) {
+ message("failed")
+ message(em)
+ }
+ return(invisible(NULL))
+ } else {
+ if (verbose)
+ message("done")
+ }
+
+ empty <- grep("^ *$", dats)
+ dats <- read.table(text = dats,
+ sep = ";",
+ header = TRUE,
+ stringsAsFactors = FALSE,
+ as.is = TRUE, skip = empty)
+ ans <- tapply(dats$Value, list(dats$Date, dats[, 2]), identity)
+ i <- min(which(!is.na(dats$Value)))
+ ans <- ans[, dats[1:(i-1), 2]]
+
+ if (!is.null(return.class)) {
+ stop("not yet supported")
+ if (return.class == "zoo")
+ if (requireNamespace("zoo"))
+ stop("not yet implemented")
+ else
+ stop("package ", sQuote("zoo"), " not available")
+
+ else if (return.class == "data.frame")
+ result <- ans
+
+ else if (return.class == "list")
+ result <- NA
+
+ } else
+ result <- ans
+
+ ## attr(result, "info") <- doc
+ result
+
+}
A => README.md +20 -0
@@ 1,20 @@
+# SNBdata
+
+Functions for downloading data from the Swiss National Bank
+(SNB; <https://data.snb.ch/>), 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.
+
+## Installation
+
+To install the package from a running R session, type:
+
+ install.packages('SNBdata',
+ repos = c('http://enricoschumann.net/R',
+ getOption('repos')))
+
+
+or clone/build the repository's latest version.
A => man/SNBdata.Rd +79 -0
@@ 1,79 @@
+\name{SNBdata}
+\alias{SNBdata-package}
+\alias{SNBdata}
+\alias{fetch_table}
+\alias{fetch_table_date}
+\alias{fetch_table_info}
+\concept{SNB}
+\title{
+ Download Data from the Swiss National Bank (SNB)
+}
+\description{
+
+ Download datasets from the Swiss National Bank (SNB)
+ and convert to data frames.
+
+}
+\usage{
+fetch_table(table = "rendoblid", dest.dir = NULL,
+ return.class = NULL, verbose = TRUE,
+ method, ...)
+fetch_table_date(table = "rendoblid", dest.dir = NULL,
+ verbose = TRUE,
+ language = "en", ...)
+fetch_table_info(table = "rendoblid", dest.dir = NULL,
+ return.class = NULL, verbose = TRUE,
+ language = "en", ...)
+}
+\arguments{
+ \item{dest.dir}{%
+ file path: where to store the files?
+ }
+ \item{table}{%
+ the name of the table
+ }
+ \item{method}{%
+ see \code{\link{download.file}}
+ }
+ \item{verbose}{
+ logical: print messages about download progress?
+ }
+ \item{return.class}{\code{NULL} or character:
+ not yet implemented (but in future version,, only
+ \code{\link[zoo]{zoo}} will be supported)
+ }
+ \item{language}{
+ string: \code{en}, \code{fr}, or \code{de}
+ }
+ \item{\dots}{
+ passed on to \code{\link{read.table}}
+ }
+}
+\details{
+
+ The \acronym{SNB} provides data as tables and datasets
+}
+\value{
+ typically a \code{\link{data.frame}}.
+
+ \code{bis_datasets} returns a \code{\link{data.frame}}
+ with three columns:
+ \item{filename}{the dataset filename}
+ \item{description}{a brief description of the dataset}
+ \item{updated}{if available, the date when the
+ dataset was last updated}
+
+}
+\author{
+ Enrico Schumann
+}
+%% \seealso{
+%% package
+%% }
+\examples{
+\donttest{
+## (Internet connection required)
+datasets()
+bis.data <- fetch_dataset(dest.dir = tempdir(),
+ "full_bis_total_credit_csv.zip")
+}}