Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/bfs_get_sse_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bfs_get_sse_data <-
# Get observation names instead of codes if specified
if (variable_value_type == "text") {
dim_df <- dim_df %>%
dplyr::mutate(dim_value = dplyr::coalesce(.data$valueTexts, .data$dim_value))
dplyr::mutate(dim_value = dplyr::coalesce(.data$valueText, .data$dim_value))
}

# Get dimension names instead of codes if specified
Expand Down
2 changes: 1 addition & 1 deletion R/bfs_get_sse_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bfs_get_sse_metadata <- function(number_bfs, language = "de") {
dplyr::left_join(dim_info, by = c("codelist_id" = "dimension_id")) %>%
dplyr::left_join(all_codes, by = c("codelist_ref" = "codelist_id", "code")) %>%
dplyr::select(code = "codelist_id", text = "codelist_text",
value = "code", valueTexts = "label",
value = "code", valueText = "label",
position_dimension = "position") %>%
dplyr::distinct()

Expand Down
34 changes: 23 additions & 11 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ library(BFS)

## Access Swiss Stats Exporer API (NEW)

Two new functions are currently tested to access the new [Swiss Stats Explorer API](https://stats.swiss/).
Two new functions are currently tested to access the new [Swiss Stats Explorer API](https://stats.swiss/) (SSE).

These functions are only accessible when using the development version of this package on GitHub.

Expand All @@ -59,25 +59,37 @@ These functions are only accessible when using the development version of this p
devtools::install_github("lgnbhl/BFS")
```

Access a Swiss Stats Explorer dataset by BFS number using the new `bfs_get_sse_data()` function.
To get data from SSE you first create a query using the metadata codelist from SSE using the `bfs_get_sse_metadata()` function. First get the codelist of a BFS dataset:
```{r}
codelist <- bfs_get_sse_metadata("DF_LWZ_1", language = "en")

codelist
```

Then filter the codelist to build your query:
```{r}
querydat <- codelist %>%
dplyr::filter((code == "GR_KT_GDE" & valueText %in% c("Aarau", "Olten")) |
(code == "WOHN_ANZAHL" & valueText == "Total") |
(code == "LEERWOHN_TYP" & valueText == "Vacant dwelling for rent") |
(code == "MEASURE_DIMENSION" & valueText == "Value"))
query <- tapply(querydat$value, querydat$code, c)

query
```

This query can be used in the new `bfs_get_sse_data()` function to import the data.

```{r}
bfs_get_sse_data(
number_bfs = "DF_PASTA_552_MONTHLY",
number_bfs = "DF_LWZ_1",
language = "en",
query = list("FREQ" = "M", "ACCOMMODATION_TYPE" = c("552001"),
"COUNTRY_ORIGIN" = c("CH", "AUSL")),
query = query,
start_period = "2020",
end_period = "2023"
)
```

You can also access the Swiss Stats Explorer metadata with the following function:

```{r}
bfs_get_sse_metadata("DF_SSV_POL_LEG", language = "de")
```

### Get the data catalog

Before downloading a BFS dataset, you need to get its related BFS number (FSO number) in the [official data catalog](https://www.bfs.admin.ch/bfs/en/home/statistics/catalogue.html). You can search in the catalog directly from R using the `bfs_get_catalog_data()` function in any language ("de", "fr", "it" or "en"):
Expand Down
1 change: 1 addition & 0 deletions man/BFS-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.