Search and download data from the Swiss Federal Statistical Office

The BFS package allows to search and download public data from the Swiss Federal Statistical Office (BFS) in a dynamic and reproducible way.

Installation

To get a bug fix, or use a feature from the development version, you can install BFS from GitHub.

Usage

library(BFS)

Download metadata

To search and download data from the Swiss Federal Statistical Office, you first need to retrieve information about the available public datasets. The function bfs_get_metadata() returns a data frame/tibble containing the titles, publication dates, observation periods, data sources, website urls and download urls of all the BFS datasets available in a given language. You can get the metadata in German (“de”, by default), French (“fr”), Italian (“it”) or English (“en”). Note that Italian and English metadata give access to less datasets.

meta_en <- bfs_get_metadata(language = "en")

head(meta_en)

Search for data

To search for a specific dataset title in the BFS metadata, you can use the bfs_search() function. This function leverages the R base function grepl() but calls the data argument first to allow the use of the pipe operator %>%.

library(magrittr)

meta_en_uni <- bfs_get_metadata("en") %>%
  bfs_search("university students")

print(meta_en_uni)

Download dataset

To download a BFS dataset, add the related url link from the url_px column of the downloaded metadata as an argument to the bfs_get_dataset() function. You can choose the language (German, French, Italian or English if any) in which the dataset is downloaded with the language argument.

df_uni <- bfs_get_dataset(url_px = meta_en_uni$url_px[1], language = "en")

print(df_uni)

You can access additional information about the downloaded dataset using the R base attributes() function.

attributes(df_uni) %>%
  str()

In case the function fails to download the dataset, you can have a look at its related BFS webpage using the url_bfs link.

browseURL(meta_en_uni$url_bfs[1]) # open webpage

Sometimes the PC-Axis file of the dataset doesn’t exist. You should then use the “STAT-TAB - interactive table” service provided by BFS to download manually the dataset.

Data caching

Data caching is handled using the pins R package. To open the folder containing all the BFS datasets you already downloaded, you can use the bfs_open_dir() function.

If a dataset has already been downloaded during the day, the functions bfs_get_metadata() and bfs_get_dataset() retrieve the already downloaded datasets from your local pins caching folder. Caching speeds up code and reduces BFS server requests. However, if a given dataset has not been downloaded during the day, the functions download it again to be sure that you have the lastest data available. You can also force the download using the force argument.

Other information

A blog article showing a concret example about how to use the BFS package.

Alternative R package: pxweb.

This package is in no way officially related to or endorsed by the Swiss Federal Statistical Office (BFS).