opendatasus_processar: Process large OpenDataSUS CSV resources in chunks

View source: R/efficient-reading.R

opendatasus_processarR Documentation

Process large OpenDataSUS CSV resources in chunks

Description

Downloads the selected resource using the regular atomic cache, expands multipart resources, and invokes a callback for bounded-size chunks. Returning small summaries or 'NULL' from the callback keeps memory use independent of the complete dataset size.

Usage

opendatasus_processar(
  conjunto,
  FUN,
  recurso = NULL,
  ano = "last",
  formato = "CSV",
  destino = NULL,
  cache = TRUE,
  atualizar = FALSE,
  colunas = NULL,
  tamanho_bloco = 100000L,
  sistema = NULL,
  ...
)

Arguments

conjunto

Dataset slug returned by [opendatasus_catalogo()].

FUN

Function called as 'FUN(dados, posicao, arquivo)', where 'posicao' is the first row position within a file and 'arquivo' is the one-based file-part index.

recurso

Optional exact resource name or stable resource identifier.

ano

Resource year, '"last"' for the latest dated resource, or 'NULL'. Ignored when 'recurso' is supplied.

formato

Resource format. When both 'recurso' and 'formato' are 'NULL', CSV is preferred.

destino

Optional destination file or existing directory. 'NULL' uses the package cache, or a temporary file when 'cache = FALSE'.

cache

Whether to cache portal metadata for the duration controlled by option 'datasus.cache_ttl'.

atualizar

Whether to ignore cached metadata and query the portal again.

colunas

Optional character vector selecting columns while CSV files are parsed. JSON columns are selected after parsing.

tamanho_bloco

Positive number of rows parsed per chunk.

sistema

Optional dictionary system accepted by [datasus_padronizar()]. When supplied, each chunk is standardized before 'FUN' is called.

...

Additional arguments passed to [readr::read_delim()] or [jsonlite::fromJSON()].

Value

An object of class '"datasus_processamento"' containing callback results, row, chunk and file counts, and provenance for every file.

Examples


old_options <- options(
  datasus.timeout = 5,
  datasus.download_timeout = 15,
  datasus.max_tries = 1
)
try({
summary <- opendatasus_processar(
  "doses-aplicadas-pelo-programa-de-nacional-de-imunizacoes-pni-2026",
  recurso = "Vacinação - Julho 2026",
  ano = NULL,
  formato = "CSV",
  FUN = function(dados, posicao, arquivo) nrow(dados),
  tamanho_bloco = 50000
)
})
options(old_options)


datasus documentation built on Oct. 7, 2026, 1:07 a.m.