| Title: | Get Data from the Swiss Federal Statistical Office |
|---|---|
| Description: | Search and download data from the Swiss Federal Statistical Office (BFS) APIs <https://www.bfs.admin.ch/>. |
| Authors: | Felix Luginbuhl [aut, cre, cph] (ORCID: <https://orcid.org/0009-0008-6625-2899>), Marius Hodel [ctb] (ORCID: <https://orcid.org/0000-0001-8789-506X>), Janosch Brenzel-Weiss [ctb], Joao Martins [ctb], Philipp Baumann [ctb] (ORCID: <https://orcid.org/0000-0002-3194-8975>) |
| Maintainer: | Felix Luginbuhl <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.7.1 |
| Built: | 2026-06-05 07:08:09 UTC |
| Source: | https://github.com/lgnbhl/BFS |
This function uses the DAM API https://dam-api.bfs.admin.ch/hub/swagger-ui/index.html to download a BFS file by asset number or BFS number. The file is downloaded using 'curl::curl_download()' under the hood.
bfs_download_asset( number_asset = NULL, number_bfs = NULL, destfile, quiet = TRUE, mode = "wb", handle = curl::new_handle() )bfs_download_asset( number_asset = NULL, number_bfs = NULL, destfile, quiet = TRUE, mode = "wb", handle = curl::new_handle() )
number_asset |
The asset number of a dataset |
number_bfs |
The BFS number of a dataset. |
destfile |
A character string with the name where the downloaded file is saved. Tilde-expansion is performed. |
quiet |
If TRUE, suppress status messages (if any), and the progress bar. |
mode |
A character string specifying the mode with which to write the file. Useful values are "w", "wb" (binary), "a" (append) and "ab". |
handle |
a curl handle object |
Returns the file path where the file has been downloaded. Returns NULL if no connection.
Download assets from the STAC API on the geodata catalog of the Swiss Confederation (https://data.geo.admin.ch/api/stac/v0.9/).
bfs_download_geodata( collection_id = collection_id, output_dir = tempdir(), overwrite = FALSE, create_json = FALSE, bbox = NULL, asset_names = NULL, datetime = NULL, feature_id = NULL )bfs_download_geodata( collection_id = collection_id, output_dir = tempdir(), overwrite = FALSE, create_json = FALSE, bbox = NULL, asset_names = NULL, datetime = NULL, feature_id = NULL )
collection_id |
collection_id |
output_dir |
output_dir |
overwrite |
overwrite |
create_json |
create_json |
bbox |
bbox |
asset_names |
asset_names |
datetime |
datetime |
feature_id |
feature_id |
The use of the data and services is free but subject to the provisions on fair use (see https://www.geo.admin.ch/terms-of-use).
Returns the file path where the file has been downloaded. Returns NULL if no connection.
This function uses the DAM API https://dam-api.bfs.admin.ch/hub/swagger-ui/index.html to get the metadata of a BFS file by asset number or BFS number in a given language.
bfs_get_asset_metadata( number_asset = NULL, number_bfs = NULL, language = c("de", "fr", "it", "en") )bfs_get_asset_metadata( number_asset = NULL, number_bfs = NULL, language = c("de", "fr", "it", "en") )
number_asset |
The asset number of a dataset |
number_bfs |
The BFS number of a dataset. |
language |
character The language of a BFS catalog, i.e. "de", "fr", "it" or "en". |
list Returns a list containing asset metadata information. Returns NULL if no connection.
[bfs_download_asset()]
This functions helps to get base maps data from the ThemaKart project as an sf object. The geom names and the general structure of the files can be found in the offical BFS documentation, see https://www.bfs.admin.ch/bfs/en/home/statistics/regional-statistics/base-maps/cartographic-bases.html. When using this data, please read the condition of use and copyright mentions.
bfs_get_base_maps( geom = NULL, category = "gf", type = "Poly", date = NULL, most_recent = TRUE, format = "shp", asset_number = "30566934", return_sf = TRUE )bfs_get_base_maps( geom = NULL, category = "gf", type = "Poly", date = NULL, most_recent = TRUE, format = "shp", asset_number = "30566934", return_sf = TRUE )
geom |
Geometry such as "suis", "kant", "bezk", "polg", "voge", etc. |
category |
Category such as 'total_area' ("gf" for "Gesamtflaeche") or 'vegetation_area' ("vf" for "Vegetationsflaeche"). |
type |
The type of data, i.e. "Poly" or "Pnts". |
date |
Date (yyyymmdd) of reference / validity. If not specified, the 'most_recent' argument is used. |
most_recent |
Get the most recent by sorting the files in decreasing order, if FALSE then read the first file available. |
format |
Format of the file, by default SHP format. |
asset_number |
Asset number of the base maps zip file. |
return_sf |
if TRUE, read file path and return sf object. If FALSE, return file path |
If you want to get ThemaKart data from previous years, you can change the 'asset_number' for the related zip file. For example, for the map set of year 2020, the asset number is "11927607".
This function is caching the base map data using 'tools::R_user_dir(package = "BFS")'.
sf object with geometries. Returns NULL if no connection.
This function scraps a given RSS Feed of the Swiss Federal Statistical Office.
bfs_get_catalog(language = "de", type = "data")bfs_get_catalog(language = "de", type = "data")
language |
character The language of a BFS catalog. |
type |
character A BFS catalog |
A data frame. Returns NULL if no connection.
Get the list of the data available in the official DAM-API of the Swiss Federal Statistical Office asset catalog.
bfs_get_catalog_data( language = "de", title = NULL, extended_search = NULL, spatial_division = NULL, prodima = NULL, inquiry = NULL, institution = NULL, publishing_year_start = NULL, publishing_year_end = NULL, order_nr = NULL, limit = 1000, article_model = 900033, article_model_group = 900029, return_raw = FALSE )bfs_get_catalog_data( language = "de", title = NULL, extended_search = NULL, spatial_division = NULL, prodima = NULL, inquiry = NULL, institution = NULL, publishing_year_start = NULL, publishing_year_end = NULL, order_nr = NULL, limit = 1000, article_model = 900033, article_model_group = 900029, return_raw = FALSE )
language |
character The language of a BFS catalog, i.e. "de", "fr", "it" or "en". |
title |
character String to search in (sub/super)title |
extended_search |
character String for an extended search in (sub/super)title, orderNr, summary, shortSummary, shortTextGNP |
spatial_division |
BFS datasets by spatial division, choose between "Switzerland", "Cantons", "Districts", "Communes", "Other spatial divisions" or "International" |
prodima |
numeric Get only specific BFS themes using one or multiple prodima numbers |
inquiry |
character BFS datasets for an inquiry |
institution |
character BFS datasets for an institution |
publishing_year_start |
character BFS datasets for a publishing year start |
publishing_year_end |
character BFS datasets for a publishing year end |
order_nr |
character Filter by BFS Number (FSO number) |
limit |
integer limit of query results (1000 by default) |
article_model |
integer articleModel parameter query |
article_model_group |
integer articleModel parameter query |
return_raw |
boolean Return raw data from json structure as a tibble data.frame |
A data frame. Returns NULL if no connection.
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
A character column with the title of the BFS dataset
A character column with the language of the BFS dataset
The BFS asset number
The BFS number (FSO number), named orderNr in the API
The published date of the BFS dataset, named embargo in the API
A list The list of all languages available for the BFS dataset
A character column with the URL of the PX file
A character column with the URL of the json structure of the BFS dataset
DAM API unique ID
bfs_get_catalog_data(language = "en", title = "students", prodima = c(900212))bfs_get_catalog_data(language = "en", title = "students", prodima = c(900212))
Display geo-information catalog of the Swiss Confederation (https://data.geo.admin.ch/), including some geographic datasets provided by the Swiss Federal Statistical Office. Note that this geodata catalog is not hosted by the Swiss Federal Statistical Office.
bfs_get_catalog_geodata(include_metadata = TRUE)bfs_get_catalog_geodata(include_metadata = TRUE)
include_metadata |
boolean TRUE to loop on each geodata to add metadata. |
For now only Stac API datasets are accessible.
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
Get the list of the tables available in the official DAM-API of the Swiss Federal Statistical Office asset catalog.
bfs_get_catalog_tables( language = "de", title = NULL, extended_search = NULL, spatial_division = NULL, prodima = NULL, inquiry = NULL, institution = NULL, publishing_year_start = NULL, publishing_year_end = NULL, order_nr = NULL, limit = 1000, article_model = 900030, article_model_group = 900029, return_raw = FALSE )bfs_get_catalog_tables( language = "de", title = NULL, extended_search = NULL, spatial_division = NULL, prodima = NULL, inquiry = NULL, institution = NULL, publishing_year_start = NULL, publishing_year_end = NULL, order_nr = NULL, limit = 1000, article_model = 900030, article_model_group = 900029, return_raw = FALSE )
language |
character The language of a BFS catalog, i.e. "de", "fr", "it" or "en". |
title |
character String to search in (sub/super)title |
extended_search |
character String for an extended search in (sub/super)title, orderNr, summary, shortSummary, shortTextGNP |
spatial_division |
BFS datasets by spatial division, choose between "Switzerland", "Cantons", "Districts", "Communes", "Other spatial divisions" or "International" |
prodima |
numeric Get only specific BFS themes using one or multiple prodima numbers |
inquiry |
character BFS datasets for an inquiry |
institution |
character BFS datasets for an institution |
publishing_year_start |
character BFS datasets for a publishing year start |
publishing_year_end |
character BFS datasets for a publishing year end |
order_nr |
character Filter by BFS Number (FSO number) |
limit |
integer limit of query results (1000 by default) |
article_model |
integer articleModel parameter query |
article_model_group |
integer articleModel parameter query |
return_raw |
boolean Return raw data from json structure as a tibble data.frame |
A data frame. Returns NULL if no connection.
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
A character column with the title of the BFS dataset
A character column with the language of the BFS dataset
The published date of the BFS dataset in the data catalog
The BFS asset number
bfs_get_catalog_data(language = "en", title = "students", prodima = c(900212))bfs_get_catalog_data(language = "en", title = "students", prodima = c(900212))
Get a dataset using the PXWEB API v1. You should choose either the BFS number (FSO number) of the BFS offical url of a given dataset. You can query particulary variables using the 'query' argument.
bfs_get_data( number_bfs, language = "de", query = NULL, column_name_type = "text", variable_value_type = "text", clean_names = FALSE, delay = NULL )bfs_get_data( number_bfs, language = "de", query = NULL, column_name_type = "text", variable_value_type = "text", clean_names = FALSE, delay = NULL )
number_bfs |
The BFS number (FSO number) of a dataset. |
language |
Language of the dataset to be translated if exists, i.e. "de", "fr", "it" or "en". |
query |
A list with named values, a json query file or json query string using |
column_name_type |
Column name type as "text" or as "code". |
variable_value_type |
Variable value type as "text" or as "code". |
clean_names |
Clean column names using |
delay |
Integer Number of seconds to wait before query using |
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
Get the comments/footnotes of a BFS dataset using PXWEB BFS API v1.
bfs_get_data_comments( number_bfs, language = "de", query = NULL, clean_names = FALSE, delay = NULL )bfs_get_data_comments( number_bfs, language = "de", query = NULL, clean_names = FALSE, delay = NULL )
number_bfs |
The BFS number of a dataset. |
language |
Language of the dataset to be translated if exists, i.e. "de", "fr", "it" or "en". |
query |
a list with named values, a json query file or json query string using |
clean_names |
Clean column names using |
delay |
Integer Number of seconds to wait before query using |
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
Get the metadata of a BFS dataset using the PXWEB API v1. You should choose either the bfs number of the bfs offical url of a given dataset.
bfs_get_metadata(number_bfs, language = "de")bfs_get_metadata(number_bfs, language = "de")
number_bfs |
The BFS number of a dataset. |
language |
Language of the dataset to be translated if exists. |
A tbl_df (a type of data frame; see tibble or dplyr packages). Returns NULL if no connection.
Get Data from Swiss Stats Explorer (SSE) API
bfs_get_sse_data( number_bfs, language = "de", start_period = NULL, end_period = NULL, query = NULL, column_name_type = "code", variable_value_type = "text", clean_names = FALSE )bfs_get_sse_data( number_bfs, language = "de", start_period = NULL, end_period = NULL, query = NULL, column_name_type = "code", variable_value_type = "text", clean_names = FALSE )
number_bfs |
The BFS number (FSO number) of a dataset. |
language |
Language of the dataset to be translated if exists, i.e. "de", "fr", "it" or "en". |
start_period |
Start year of the requested data |
end_period |
End year of the requested data |
query |
A named list with dimension codes as names and desired values as values. |
column_name_type |
Column name type as "text" or as "code". |
variable_value_type |
Variable value type as "text" or as "code". |
clean_names |
Logical, if TRUE, the column names are cleaned using janitor::clean_names(). |
A tibble with the requested data.
bfs_get_sse_data( number_bfs = "DF_PASTA_552_MONTHLY", language = "en", query = list("FREQ" = "M", "ACCOMMODATION_TYPE" = c("552001"), "COUNTRY_ORIGIN" = c("CH", "AUSL")), start_period = "2020", end_period = "2023" )bfs_get_sse_data( number_bfs = "DF_PASTA_552_MONTHLY", language = "en", query = list("FREQ" = "M", "ACCOMMODATION_TYPE" = c("552001"), "COUNTRY_ORIGIN" = c("CH", "AUSL")), start_period = "2020", end_period = "2023" )
Retrieve metadata codelists for a specified BFS dataset from the Swiss Stats Explorer (SSE) API. This function is required to built queries for bfs_get_sse_data.
bfs_get_sse_metadata(number_bfs, language = "de")bfs_get_sse_metadata(number_bfs, language = "de")
number_bfs |
The BFS number (FSO number) of a dataset. |
language |
Language of the codelists ("de", "fr", "it", "en"). |
A tibble containing the codelists of the specified BFS dataset.
bfs_get_sse_metadata("DF_SSV_POL_LEG", language = "de")bfs_get_sse_metadata("DF_SSV_POL_LEG", language = "de")
Get the URL for the Swiss Stats Explorer (SSE) API based on the BFS number
bfs_get_sse_url(number_bfs, metadata = FALSE)bfs_get_sse_url(number_bfs, metadata = FALSE)
number_bfs |
The BFS number (FSO number) of a dataset. |
metadata |
Logical; if TRUE, returns the URL for metadata instead of data. |
A character string containing the URL for the API of the specified BFS number.
If the specified language is not found, it falls back to the first available name.
get_xml_name(node, language)get_xml_name(node, language)
node |
An XML node from which to extract the name. |
language |
The desired language code ("de", "fr", "it", "en"). |
A character string containing the name in the specified language or the first available name.
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_bznregister_bzn
## 'register_bzn' A data frame with 143 rows and 3 columns:
Kantonskuerzel
Bezirksnummer
Bezirksname
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_dicregister_dic
## 'register_dic' A data frame with 27 rows and 5 columns:
Language
Abbreviation
Title
Specifications
General notes
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_gderegister_gde
## 'register_gde' A data frame with 2,136 rows and 8 columns:
Kantonskuerzel
Bezirksnummer
BFS-Gemeindenummer
Amtlicher Gemeindename
Gemeindename, kurz
Bezirksname
Kantonsname
Datum der letzten Anderung
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_gde_otherregister_gde_other
## 'register_gde_other' A data frame with 3 rows and 4 columns:
BFS-Gemeindenummer
Amtlicher Gemeindename
Kantonsnummer
Bezirksnummer
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_ktregister_kt
## 'register_kt' A data frame with 26 rows and 3 columns:
Kantonsnummer
Kantonskuerzel
Kantonsname
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>
The official commune register is structured according to cantons and districts or comparable administrative entities. Various federal, cantonal and communal governments and private businesses use this register when identifying and referring to communes.
register_kt_seeanteileregister_kt_seeanteile
## 'register_kt_seeanteile' A data frame with 9 rows and 5 columns:
BFS-Gemeindenummer
Amtlicher Gemeindename
Kantonsnummer
The Federal Statistical Office assigns a number to each commune and creates, administers and publishes the Swiss official commune register.
<https://www.bfs.admin.ch/bfs/fr/home/bases-statistiques/repertoire-officiel-communes-suisse.html>