## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## -----------------------------------------------------------------------------
# library(pixieweb)
# 
# px_api_catalogue()

## -----------------------------------------------------------------------------
# scb <- px_api("scb", lang = "en")      # Sweden (v2)
# ssb <- px_api("ssb", lang = "en")      # Norway (v2)
# statfi <- px_api("statfi", lang = "en") # Finland (v1)

## -----------------------------------------------------------------------------
# scb
# ssb

## -----------------------------------------------------------------------------
# library(dplyr)
# library(purrr)
# 
# # Find population tables in each country
# scb_tables <- get_tables(scb, query = "population")
# ssb_tables <- get_tables(ssb, query = "population")
# 
# # Explore a table from each
# scb_tables |> table_describe(max_n = 3)
# ssb_tables |> table_describe(max_n = 3)

## -----------------------------------------------------------------------------
# # Fetch data using prepare_query() for quick exploration
# scb_q <- prepare_query(scb, "TAB638",
#   Region = "00",       # "Riket" (whole country)
#   Tid = px_top(5),
#   ContentsCode = "BE0101N1" # Population
# )
# 
# # Norwegian table IDs are different — explore to find the right one
# ssb_vars <- get_variables(ssb, "05803")
# ssb_vars |> variable_describe()

## -----------------------------------------------------------------------------
# results <- list(
#   sweden = get_data(scb, query = scb_q),
#   norway = get_data(ssb, "05803",
#     ContentsCode = "Personer",
#     Tid = px_top(5)
#   )
# )
# 
# # .id = "country" adds a column tracking which list element each row
# # came from — essential for traceability after binding
# bind_rows(results, .id = "country")
# # NOTE: column names may differ between countries. If so, you may need
# # to rename() before bind_rows() to align them.

