source("R/setup.R")$value
if (!sc_key_exists())
  knitr::opts_chunk$set(eval = FALSE)

In the following example, a table will be exported from STATcube into an R session. This process involves four steps

It is assumed that you already provided your API key as described in the r ticle("sc_key").

Create a table with the STATcube GUI

Use the graphical user interface of STATcube to create a table. Visit STATcube and select a database. This will open the table view where you can create a table. See the STATcube documentation for details.

Download an API request

Choose "Open Data API Query (.json)" in the download options. This will save a json file on your local file system.

knitr::include_graphics("img/download_json.png")

It might be the case that this download option is not listed as a download format. This means that the current user is not permitted to use the API.

Send the json to the API

Provide the path to the downloaded json file as a string in sc_table().

library(STATcubeR)
my_table <- sc_table(json = "path/to/api_request.json")

This will send the json-request to the [/table endpoint] of the API and return an object of class sc_table. We will demonstrate this with an example json via sc_example().

(json_path <- sc_example("population_timeseries.json"))
## [1] "~/R/3.6/STATcubeR/json_examples/population_timeseries.json"
my_table <- sc_table(json_path)

Printing the object my_table will summarize the data contained in the response.

my_table

Convert the response into a data frame

The return value of sc_table() can be converted into a data.frame with as.data.frame().

as.data.frame(my_table)

This will produce a data.frame, which contains a column for each classification field of the table. Furthermore, one column will be present for each measure. In other words, the data uses a long format. If you prefer to use codes rather than labels, use my_table$data instead.

my_table$data

Example datasets {.tabset .tabset-pills}

This article used a dataset about the Austrian population via sc_example(). r STATcubeR contains more example jsons to get started. The datasets can be listed with sc_examples_list().

Accommodation

sc_example("accomodation.json") %>% sc_table()

STATatlas

sc_example("economic_atlas.json") %>% sc_table()

Trade

sc_example("foreign_trade.json") %>% sc_table()

GDP

sc_example("gross_regional_product.json") %>% sc_table()

Working Hours

sc_example("labor_force_survey.json") %>% sc_table()

Agriculture

```{r, eval = FALSE sc_example("agriculture_prices.json") %>% sc_table()

### monitor.statistik.at

```r
sc_example("economic_trend_monitor.json") %>% sc_table()

Choosing the Language {.tabset .tabset-pills}

The language which is used for labeling can be changed via the language parameter of sc_table().

Accommodation

sc_example("accomodation.json") %>% sc_table("de")

STATatlas

sc_example("economic_atlas.json") %>% sc_table("de")

Trade

sc_example("foreign_trade.json") %>% sc_table("de")

GDP

sc_example("gross_regional_product.json") %>% sc_table("de")

Working Hours

sc_example("labor_force_survey.json") %>% sc_table("de")

Agriculture

sc_example("agriculture_prices.json") %>% sc_table("de")

monitor.statistik.at

sc_example("economic_trend_monitor.json") %>% sc_table("de")

Further reading



statistikat/STATcubeR documentation built on Dec. 3, 2024, 8:04 p.m.