## ----basecode, message=FALSE, eval=FALSE, echo=FALSE--------------------------
# # Below are code to run to setup the data
# 
# # Get PXWEB levels
# px_levels <- pxweb_get("https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/")
# px_levels
# save(px_levels, file = "vignettes/px_levels_example.rda")
# 
# # Get PXWEB metadata about a table
# scb_table_url <- paste0(
#   "https", "://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy"
# )
# px_meta <- pxweb_get(scb_table_url)
# px_meta
# save(px_meta, file = "vignettes/px_meta_example.rda")
# 
# 
# # Example Download
# pxweb_query_list <-
#   list(
#     "Civilstand" = c("*"), # Use "*" to select all
#     "Kon" = c("1", "2"),
#     "ContentsCode" = c("BE0101N1"),
#     "Tid" = c("2015", "2016", "2017")
#   )
# pxq <- pxweb_query(pxweb_query_list)
# 
# pxd <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# save(pxd, file = "vignettes/pxd_example.rda")
# 
# pxq$response$format <- "json-stat"
# pxjstat <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# save(pxjstat, file = "vignettes/pxjstat_example.rda")
# 
# pxq$response$format <- "px"
# pxfp <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# save(pxfp, file = "vignettes/pxfp_example.rda")
# 
# pxweb_cite_example <- capture.output(pxweb_cite(pxd))
# 

## ----install1, eval=FALSE-----------------------------------------------------
# install.packages("pxweb")

## ----install2, eval=FALSE-----------------------------------------------------
# library("remotes")
# remotes::install_github("ropengov/pxweb")

## ----test, message=FALSE, warning=FALSE, eval=TRUE----------------------------
library(pxweb)

## ----locale, eval=FALSE-------------------------------------------------------
# Sys.setlocale(locale = "UTF-8")

## ----standardquery, message=FALSE, eval=FALSE---------------------------------
# # Navigate through all pxweb api:s in the R package API catalogue
# d <- pxweb_interactive()
# 
# # Get data from SCB (Statistics Sweden)
# d <- pxweb_interactive("api.scb.se")
# 
# # Fetching data from statfi (Statistics Finland)
# d <- pxweb_interactive("statfin.stat.fi")
# 
# # Fetching data from StatBank (Statistics Norway)
# d <- pxweb_interactive("data.ssb.no")
# 
# # To see all available PXWEB APIs use
# pxweb_apis <- pxweb_api_catalogue()

## ----inapi, message=FALSE, eval=FALSE-----------------------------------------
# # Start with a specific path.
# d <- pxweb_interactive("https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A")

## ----message=FALSE, eval=FALSE------------------------------------------------
# file.edit(pxweb_api_catalogue_path())

## ----levels, message=FALSE, eval=FALSE----------------------------------------
# # Get PXWEB levels
# px_levels <- pxweb_get("https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/")
# px_levels

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("px_levels_example.rda")
px_levels

## ----meta, message=FALSE, eval=FALSE------------------------------------------
# # Get PXWEB metadata about a table
# scb_table_url <- paste0(
#   "https", "://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy"
# )
# px_meta <- pxweb_get(scb_table_url)
# px_meta

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("px_meta_example.rda")
px_meta

## ----message=FALSE, eval=FALSE------------------------------------------------
# d <- pxweb_interactive(scb_table_url)

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
# save(d, file = "d_example.rda")
load("d_example.rda")

## ----message=FALSE, eval=TRUE-------------------------------------------------
names(d)
d$url
d$query

## ----interactive_query, message=FALSE, eval=TRUE------------------------------
pxweb_query_as_json(d$query, pretty = TRUE)

## ----pxq, message=FALSE, eval=FALSE-------------------------------------------
# pxq <- pxweb_query("path/to/the/json/query.json")

## ----rquery, message=FALSE, eval=TRUE-----------------------------------------
pxweb_query_list <-
  list(
    "Civilstand" = c("*"), # Use "*" to select all
    "Kon" = c("1", "2"),
    "ContentsCode" = c("BE0101N1"),
    "Tid" = c("2015", "2016", "2017")
  )
pxq <- pxweb_query(pxweb_query_list)
pxq

## ----validate_query, message=FALSE, eval=TRUE---------------------------------
pxweb_validate_query_with_metadata(pxq, px_meta)

## ----message=FALSE, eval=FALSE------------------------------------------------
# pxd <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# pxd

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("pxd_example.rda")
pxd

## ----message=FALSE, eval=FALSE------------------------------------------------
# pxq$response$format <- "json-stat"
# pxjstat <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# pxjstat

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("pxjstat_example.rda")
pxjstat

## ----message=FALSE, eval=FALSE------------------------------------------------
# pxq$response$format <- "px"
# pxfp <- pxweb_get(
#   scb_table_url,
#   pxq
# )
# pxfp

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("pxfp_example.rda")
pxfp

## ----message=FALSE, eval=FALSE------------------------------------------------
# search_results <- pxweb_search(
#   "population",
#   api_url = paste0("https:", "//", "statistikdatabasen.scb.se", "/api/v2"),
#   lang = "en",
#   page_size = 5
# )
# 
# search_results[, c("id", "label", "metadata_url")]

## ----message=FALSE, eval=FALSE------------------------------------------------
# v2_url <- paste0(
#   "https:", "//", "statistikdatabasen.scb.se",
#   "/api/v2/tables/TAB638/metadata?lang=sv"
# )
# 
# v2_query <- list(
#   Region = "00",
#   Civilstand = "OG",
#   Alder = "0",
#   Kon = "1",
#   ContentsCode = "BE0101N1",
#   Tid = "2024"
# )
# 
# v2_data <- pxweb_get(v2_url, query = v2_query)
# as.data.frame(v2_data, column.name.type = "code", variable.value.type = "code")

## ----message=FALSE, eval=FALSE------------------------------------------------
# v2_query <- list(
#   Region = pxweb_all(),
#   Alder = pxweb_aggregation("agg_Ålder5år_1"),
#   Kon = c("1", "2"),
#   ContentsCode = "BE0101N1",
#   Tid = pxweb_latest()
# )
# 
# v2_df <- pxweb_get_data(
#   v2_url,
#   query = v2_query,
#   column.name.type = "code",
#   variable.value.type = "code"
# )

## ----message=FALSE, eval=FALSE------------------------------------------------
# pxweb_all()                         # all values for a variable
# pxweb_latest()                      # latest time period, resolved from metadata
# pxweb_top(10)                       # top 10 values
# pxweb_bottom(10)                    # bottom 10 values
# pxweb_aggregation("agg_Ålder5år_1") # use an aggregation codelist
# pxweb_valueset("vs_Ålder1årG")      # use a value set codelist

## ----message=FALSE, eval=FALSE------------------------------------------------
# v2_query <- list(
#   Alder = pxweb_aggregation(
#     "agg_Ålder5år_1",
#     value_codes = c("0-4", "5-9", "10-14")
#   ),
#   Kon = "1",
#   ContentsCode = "BE0101N1",
#   Tid = pxweb_latest()
# )

## ----message=FALSE, eval=FALSE------------------------------------------------
# v2_meta <- pxweb_get(v2_url)
# pxweb_codelists(v2_meta, variable = "Alder")

## ----message=FALSE, eval=FALSE------------------------------------------------
# v2_df <- pxweb_get_data(
#   v2_url,
#   query = v2_query,
#   column.name.type = "code",
#   variable.value.type = "code"
# )

## ----message=FALSE, eval=TRUE-------------------------------------------------
pxdf <- as.data.frame(pxd, column.name.type = "text", variable.value.type = "text")
head(pxdf)

## ----message=FALSE, eval=TRUE-------------------------------------------------
pxdf <- as.data.frame(pxd, column.name.type = "code", variable.value.type = "code")
head(pxdf)

## ----message=FALSE, eval=TRUE-------------------------------------------------
pxmat <- as.matrix(pxd, column.name.type = "code", variable.value.type = "code")
head(pxmat)

## ----message=FALSE, eval=TRUE-------------------------------------------------
pxdc <- pxweb_data_comments(pxd)
pxdc

## ----message=FALSE, eval=FALSE------------------------------------------------
# as.data.frame(pxdc)

## ----message=FALSE, eval=FALSE------------------------------------------------
# pxweb_cite(pxd)

## ----message=FALSE, eval=TRUE, echo=FALSE-------------------------------------
load("pxweb_cite_example.rda")
cat(pxweb_cite_example, sep = "\n")

## ----sessioninfo, message=FALSE, warning=FALSE--------------------------------
sessionInfo()

