caged_load: Pipeline completo: download -> parse -> DuckDB

View source: R/database.R

caged_loadR Documentation

Pipeline completo: download -> parse -> DuckDB

Description

Combina caged_download(), caged_parse() e caged_to_duckdb() em um único comando. É o jeito mais simples de popular o banco local.

Usage

caged_load(
  years,
  months = seq_len(12L),
  states = NULL,
  db_path = file.path(tempdir(), "caged.duckdb"),
  destdir = NULL,
  force_download = FALSE,
  overwrite_competencies = FALSE,
  timeout = 300,
  workers = getOption("datacaged.workers", min(3L, future::availableCores()))
)

Arguments

years

integer vector. Desired years.

months

integer vector. Desired months (1–12). Default: seq_len(12L) (todos os meses).

states

character vector or NULL. Ignored — the parameter is validated but does not filter downloads for any series (Novo CAGED, antigo or Ajustes), as all files are national in scope. Kept for backwards compatibility.

db_path

character. Path to the file .duckdb. Default: file.path(tempdir(), "caged.duckdb").

destdir

character or NULL. Cache directory for ⁠.7z⁠ files.

force_download

logical. Re-downloads files already in cache. Default: FALSE.

overwrite_competencies

logical. Overwrites competencies already in the database. Default: FALSE.

timeout

integer. Timeout per file in seconds. Default: 300.

workers

integer. Number of parallel downloads. Default: 3. Use 1 for sequential mode. Controlled via options(datacaged.workers = N).

Value

invisible: tibble with final database statistics (via caged_info()).

See Also

caged_adjustments_load() para o CAGED Ajustes (correções retroativas até 2019).

caged_download() para baixar sem gravar no banco.

caged_info() para inspecionar o banco após a carga.

Examples

## Not run: 
# Download real --- exemplos nao executados automaticamente (requerem rede e tempo)

# Novo CAGED: 1 mes recente
caged_load(
  years   = 2024,
  months  = 1,
  db_path = file.path(tempdir(), "caged.duckdb")
)

# CAGED antigo: 1 ano
caged_load(
  years   = 2019,
  months  = seq_len(12L),
  db_path = file.path(tempdir(), "caged_historico.duckdb")
)

# Conecta e consulta
con <- caged_connect(file.path(tempdir(), "caged.duckdb"))
if ("caged_mov" %in% DBI::dbListTables(con)) {
  dplyr::tbl(con, "caged_mov") |>
    dplyr::group_by(competencia, uf) |>
    dplyr::summarise(saldo = sum(saldomovimentacao, na.rm = TRUE)) |>
    dplyr::collect()
}
DBI::dbDisconnect(con, shutdown = TRUE)

## End(Not run)


datacaged documentation built on Oct. 13, 2026, 1:06 a.m.