Skip to contents

Send requests against the /table endpoint of the STATcube REST API. The requests can use three formats with corresponding functions

  • sc_table() uses a json file downloaded via the STATcube GUI

  • sc_table_custom() uses the ids of a database, measures and fields

  • sc_table_saved() uses a table uri of a saved table.

Those three functions all return an object of class "sc_table".

Usage

sc_table(json, language = NULL, add_totals = TRUE, key = NULL, json_file = NA)

sc_examples_list()

sc_example(filename)

sc_table_saved_list(key = NULL, server = "ext")

sc_table_saved(table_uri, language = NULL, key = NULL, server = "ext")

Arguments

json

Path to a json file, which was downloaded via the STATcube GUI ("Open Data API Request"). Alternatively, a json string which passes jsonlite::validate().

language

The language to be used for labeling. "en" (the default) will use english. "de" uses german. The third option "both" will import both languages by sending two requests to the /table endpoint.

add_totals

Should totals be added for each classification field in the json request?

key

(string) An API key. To display your key, call sc_browse_preferences().

json_file

Depricated. Use json instead

filename

The name of an example json file.

server

A STATcube API server. Defaults to the external Server via "ext". Other options are "red" for the editing server and "prod" for the production server. External users should always use the default option "ext".

table_uri

Identifier of a saved table as returned by sc_table_saved_list()

Value

An object of class sc_table which contains the return value of the httr::POST() request in obj$response. The object also provides member functions to parse this response object. See sc_table_class for the class documentation.

Examples

my_table <- sc_table(json = sc_example("population_timeseries.json"))

# print
my_table
#> Population at the beginning of the quarter since 2002
#> 
#> Database: debevstand (STATcube)
#> Measures: Number of persons
#> Fields: Quarter <82>, Age in single years <96> <7>, Sex <2> <3>,
#>   Commune <2383> (Province-District) <10>
#> 
#> Request: [2022-08-29 09:52:04]
#> STATcubeR: 0.5.0.1

# get matadata for the table
my_table$meta
#> $source
#> # STATcubeR metadata: 1 x 6
#>   code       label                                               lang 
#>   <chr>      <chr>                                               <chr>
#> 1 debevstand Population at the beginning of the quarter since 2… en   
#> # … with 3 more columns: 'label_de', 'label_en', 'scr_version'
#> 
#> $measures
#> # STATcubeR metadata: 1 x 8
#>   code     label               NAs
#>   <chr>    <chr>             <int>
#> 1 F-ISIS-1 Number of persons     0
#> # … with 5 more columns: 'fun', 'precision', 'annotations', 'label_de', 'label_en'
#> 
#> $fields
#> # STATcubeR metadata: 4 x 7
#>   code       label                              total_c…¹ nitems type 
#>   <chr>      <chr>                              <chr>      <int> <chr>
#> 1 C-A10-0    Quarter                            SC_TOTAL      82 Time…
#> 2 C-BESC51-0 Age in single years <96>           SC_TOTAL       7 Cate…
#> 3 C-BESC11-0 Sex <2>                            SC_TOTAL       3 Cate…
#> 4 C-C41-2    Commune <2383> (Province-District) SC_TOTAL      10 Cate…
#> # … with abbreviated variable name ¹​total_code
#> # … with 2 more columns: 'label_de', 'label_en'
#> 

# get a data.frame
as.data.frame(my_table)
#> # A STATcubeR tibble: 17,220 x 5
#>    Quarter    `Age in single years <96>` `Sex <2>` Commune <…¹ Numbe…²
#>    <date>     <fct>                      <fct>     <fct>         <dbl>
#>  1 2002-01-01 Up to 14 years old         male      Burgenland…   21287
#>  2 2002-01-01 Up to 14 years old         male      Carinthia …   47230
#>  3 2002-01-01 Up to 14 years old         male      Vienna <AT…  117920
#>  4 2002-01-01 Up to 14 years old         male      Vorarlberg…   34798
#>  5 2002-01-01 Up to 14 years old         male      Tyrol <AT3…   62794
#>  6 2002-01-01 Up to 14 years old         male      Styria <AT…   97538
#>  7 2002-01-01 Up to 14 years old         male      Salzburg <…   46955
#>  8 2002-01-01 Up to 14 years old         male      Upper Aust…  127316
#>  9 2002-01-01 Up to 14 years old         male      Lower Aust…  133928
#> 10 2002-01-01 Up to 14 years old         male      Total        689766
#> # … with 17,210 more rows, and abbreviated variable names
#> #   ¹​`Commune <2383> (Province-District)`, ²​`Number of persons`

# get metadata for field 2
my_table$field(2)
#> # STATcubeR metadata: 7 x 7
#>   code     label              parsed            
#>   <chr>    <chr>              <chr>             
#> 1 BESN07-1 Up to 14 years old Up to 14 years old
#> 2 BESN07-2 15 to 29 years old 15 to 29 years old
#> 3 BESN07-3 30 to 44 years old 30 to 44 years old
#> 4 BESN07-4 45 to 59 years old 45 to 59 years old
#> 5 BESN07-5 60 to 74 years old 60 to 74 years old
#> 6 BESN07-6 75 plus years old  75 plus years old 
#> 7 SC_TOTAL Total              Total             
#> # … with 4 more columns: 'label_de', 'label_en', 'visible', 'order'


# get the ids and labels of all saved tables
(saved_tables <- sc_table_saved_list())
#> # A data frame: 5 × 2
#>   label           id                                            
#>   <chr>           <chr>                                         
#> 1 Bier auf Wein   str:table:4520fba3-dffd-4f2a-9cd1-d46d710b6da4
#> 2 hierarchical    str:table:1529fb5c-8f71-425c-bc95-a017f6534c8f
#> 3 neu             str:table:4d55ca7c-e999-49e4-90f4-396163195da5
#> 4 lebenserwartung str:table:b0f4779b-546e-4a6c-88a1-43038cdeaf7b
#> 5 test123         str:table:9ba81454-32f5-4ab7-b904-c446940946f5
table_uri <- saved_tables$id[1]

# get a table based on one of these ids
my_response <- sc_table_saved(table_uri)
as.data.frame(my_response)
#> # A STATcubeR tibble: 156 x 4
#>    Year       `Wine colour`     `Production in hectolitres` Area in …¹
#>    <date>     <fct>                                   <dbl>      <dbl>
#>  1 2021-01-01 Wine (in total)                      2460275.     42835.
#>  2 2021-01-01 White wine                           1730468.     29428.
#>  3 2021-01-01 Red wine and rosé                     729806.     13407.
#>  4 1970-01-01 Wine (in total)                      3096130      41821 
#>  5 1970-01-01 White wine                           2723071      36694 
#>  6 1970-01-01 Red wine and rosé                     373059       5127 
#>  7 1971-01-01 Wine (in total)                      1812790      42714 
#>  8 1971-01-01 White wine                           1563710      37157 
#>  9 1971-01-01 Red wine and rosé                     249080       5557 
#> 10 1972-01-01 Wine (in total)                      2595615      42714 
#> # … with 146 more rows, and abbreviated variable name
#> #   ¹​`Area in hectares`