statistikat / STATcubeR

R interface for the STATcube REST API and data.statistik.gv.at
https://statistikat.github.io/STATcubeR/
GNU General Public License v2.0
14 stars 1 forks source link

Add sc_table_custom() #6

Closed GregorDeCillia closed 3 years ago

GregorDeCillia commented 3 years ago

Implement a function that takes ids for a database, measures and fields and sends a json request. Here is a snippet on how to do that manually at the moment

# pick a dataset
db_id <- "detouextregsai"
db_schema <- sc_schema_db(db_id)
db_uid <- paste0("str:database:", db_id)

# browse the schema to obtain resource ids
id_arrivals <- db_schema$Facts$Arrivals$Arrivals$id
id_time <- db_schema$`Mandatory fields`$`Season/Tourism Month`$`Season/Tourism Month`$id

# get the response
json_list <- list(database = db_uid, measures = list(id_arrivals), dimensions = list(list(id_time)))
response <- httr::POST(
  url = paste0(STATcubeR:::base_url, "/table"),
  body = jsonlite::toJSON(json_list, auto_unbox = TRUE),
  encode = "raw",
  config = httr::add_headers(APIKey = sc_key())
)

# convert to class sc_table
my_table <- STATcubeR:::sc_table_class$new(response)

Transofm this snippet into a function

sc_table_custom(database_id, measures, fields)