## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## -----------------------------------------------------------------------------
# library(pixieweb)
# 
# # Known aliases
# scb <- px_api("scb", lang = "en")
# ssb <- px_api("ssb", lang = "en")
# 
# # Or a custom URL
# custom <- px_api("https://my.statbank.example/api/v2/", lang = "en")
# 
# # See all known APIs
# px_api_catalogue()

## -----------------------------------------------------------------------------
# tables <- get_tables(scb, query = "income") |>
#   table_search("taxable")
# 
# tables |> table_describe(max_n = 3)

## -----------------------------------------------------------------------------
# vars <- get_variables(scb, "TAB638")
# vars |> variable_describe()

## -----------------------------------------------------------------------------
# # See what values a variable has
# vars |> variable_values("Kon")
# 
# # Look up variable codes by name
# variable_name_to_code(vars, "sex")

## -----------------------------------------------------------------------------
# pop <- get_data(scb, "TAB638",
#   Region = c("0180", "1480"),
#   Kon = c("1", "2"),
#   ContentsCode = "*",
#   Tid = px_top(5)
# )

## -----------------------------------------------------------------------------
# q <- prepare_query(scb, "TAB638")

## -----------------------------------------------------------------------------
# q <- prepare_query(scb, "TAB638",
#   Region = c("0180", "1480"),
#   maximize_selection = TRUE
# )

## -----------------------------------------------------------------------------
# pop <- get_data(scb, query = q)

## -----------------------------------------------------------------------------
# cls <- get_codelists(scb, "TAB638", "Region")
# cls |> codelist_describe(max_n = 5)
# 
# # Use a codelist in a query
# get_data(scb, "TAB638",
#   Region = "*",
#   Tid = px_top(5),
#   ContentsCode = "*",
#   .codelist = list(Region = "vs_RegionLän07")
# )

## -----------------------------------------------------------------------------
# demo <- get_data(scb, "TAB638",
#   Region = "0180",
#   Tid = px_top(5),
#   ContentsCode = "*",
#   .output = "wide"
# )
# demo

## -----------------------------------------------------------------------------
# q <- compose_data_query(scb, "TAB638",
#   Region = c("0180"),
#   ContentsCode = "*",
#   Tid = px_top(3)
# )
# 
# # Inspect the query
# q$url
# q$body
# 
# # Modify and execute
# raw <- execute_query(scb, q$url, q$body)

## -----------------------------------------------------------------------------
# # Save a query
# id <- save_query(scb, "TAB638", Region = "0180", Tid = px_top(5), ContentsCode = "*")
# 
# # Retrieve later
# get_saved_query(scb, id)

## -----------------------------------------------------------------------------
# px_cite(pop)

