Fast Access to Brazilian Public Health Data from 'DATASUS'
Overview
Provides fast, in-memory reading of 'DATASUS' 'DBC' files using native 'C' code, along with a catalog of public health data sources, 'FTP' file discovery, caching downloads, and a high-level datasus_fetch() function that lists, downloads, and reads files in a single call. Bundles the 'blast' decompressor from 'zlib' contrib/blast to decode 'PKWare DCL' compressed 'DBC' files and parses 'DBF' records directly for efficient import into tibbles. See the 'DATASUS' file transfer site https://datasus.saude.gov.br and Adler (2003) https://github.com/madler/zlib/tree/master/contrib/blast for details on the underlying data and compression format.
Robustness release following a code audit of the compiled reader and of the
download layer. No new dependencies.
Behaviour changes
- Deleted DBF records (
*flag) are dropped instead of returned as all-NArows. - The cache layout now includes the release period (
<cache>/<source>/<file_type>/<period>/<file_name>); existing caches re-download once. timeoutindatasus_download()/datasus_fetch()defaults toInf.datasus_download()gainssuccessanderrorcolumns;datasus_fetch()gainsrefreshandoverwritearguments.encoding,year,monthandufare validated and invalid values error.
Show all changes in 0.1.1Hide the rest of the changes in 0.1.1
DBC / DBF reader (compiled code)
- **Malformed headers are rejected instead of read out of bounds.** The declared field widths are now validated against the record length before any data is touched (previously a corrupt file could make the parser read past the end of its buffer).
- **Deleted records (
*flag) are dropped.** They used to come back as all-NArows.n_maxnow counts live records only. - **Much lower memory use on character-heavy files**: per-cell scratch allocations were replaced by a single reusable buffer.
- **No native memory is leaked on error**: the file and decompression buffers are released through
R_UnwindProtect()whenever an R error or interrupt unwinds out of the reader. - Integer fields are parsed with overflow detection; values outside R's integer range (including
-2147483648, R'sNAsentinel) are inferred asdoubleor returned asNAinstead of a wrong number. - Numeric, logical and date fields are always trimmed before parsing;
trim_wsnow only affects character output (withtrim_ws = FALSE, padded numbers used to becomeNA). - Invalid dates such as
20240231are nowNAinstead of rolling over into the next month. encodingis validated:"latin1"(and thelatin-1/ISO-8859-1aliases),"UTF-8"and"unknown"are accepted; anything else errors instead of being silently treated as Latin-1.selectandcol_typesnames are matched case-insensitively against the DBF field names, andselectnames that match no column raise a warning instead of silently returning fewer columns.- A DBF whose data area is shorter than its header declares now reads the available records with a warning; a truncated plain DBF no longer fails with a misleading decompression error.
- A very small DBC whose compressed payload is larger than its uncompressed record area is no longer mistaken for a plain DBF (the record-flag bytes are checked in addition to the file size).
n_maxis floored to a whole number of rows.
Catalog, downloads and cache
- **
SIM/DOandSINASC/DNwithoutufnow list all 27 UFs** (previously zero files were returned). - **Cache layout now includes the period**:
<cache>/<source>/<file_type>/<period>/<file_name>. Final and preliminary releases of the same file (e.g.DOPE2022.dbc) used to share one path, which madedatasus_download()fail with "Duplicate destfiles" or served a stale preliminary file forever. Existing caches will re-download once. - **Failed or interrupted downloads no longer poison the cache**: files are downloaded to a
.partname and renamed only on success; partial files are removed.datasus_download()gainssuccessanderrorcolumns anddownloadedreflects the real outcome.datasus_fetch()skips failed files with a warning and errors if every download failed. - **
datasus_fetch()prefers final over preliminary data**: when the same file (matched case-insensitively, and treatingSINAN_P/ESUSNOTIFICA_Pas preliminary aliases ofSINAN/ESUSNOTIFICA) is available in both trees only the final copy is read, so rows are not duplicated. A final candidate that could not be confirmed on the FTP does not suppress a preliminary copy known to exist.datasus_fetch()also gainsrefreshandoverwritearguments (the vignette documentedrefreshbut it was not accepted). - **
timeoutno longer caps the whole transfer.** It used to be passed to libcurl as the total time allowed per file (240 s), which aborted large files on the slow DATASUS FTP. It now defaults toInf; stalled transfers are detected with a connect timeout and a low-speed limit instead. - FTP listing failures are reported with a warning and the affected files keep
exists = NA, instead of being silently reported as absent. datasus_build_path()no longer recycles a vectoryearagainst the SINASC path templates (which produced a warning and wrong paths).SINAN_Pwas advertised indatasus_sources()but had no file types and could never be fetched; its file types are now registered.year,monthandufare validated before any network access.- File names are matched against the FTP listing case-insensitively, keeping the server's spelling in the download URL.
datasus_download(use_cache = FALSE)withoutdest_dirwrites to a fresh subdirectory oftempdir()instead of the working directory, andoverwrite = FALSEis honoured in that mode.datasus_get_territory()copes with upper-case member names inside the territorial ZIP.- New offline test suite (
tests/testthat) covering the DBF parser with synthetic files and the catalog / cache / download logic withfile://URLs.
From NEWS.md in the release
Install
Health
- OK13 OK2026-08-05
- NOTE12 OK, 1 NOTE2026-08-01
- OK7 OK2026-05-05
Documentation
- Help pages
- 25 (7 internal)includes a package overview page
- Examples that run
- 63%10 run in a plain check · 6 only when donttest examples are enabled (R CMD check --as-cran)
- Exported functions with examples
- 89%
- Documented parameters
- 100%
- Return-value docs
- 100%
- Help pages citing references
- 0%
Downloads
Repository
Stars over time
Commits over time
7 development-tooling and community-health practices detected across 5 families in the upstream repository
Checks run against github.com/strategicprojects/datasusr on 2026-10-21.
The R CMD check workflow runs on Linux and macOS, and on R-devel
Show all practicesHide 1 practice
This repository shows signs of AI coding tools.
- Claude Codefirst seen on or before 2026-04-29Seen in the repository: CLAUDE.mdNamed in .Rbuildignore: .claude, CLAUDE.mdCommits: at least 7 crediting it in the message
- Claude Fable 5.13 commits · Sep 2026
- Claude Opus 5.52 commits · Sep 2026
- Claude Opus 4.71 commit · Apr 2026
- Claude Opus 4.81 commit · Jun 2026
Dependencies
Installing datasusr adds 21 packages (13 with compiled code, 18 MB of source tarballs). Heaviest: stringi 11 MB, vctrs 1.0 MB, dplyr 902 KB.
Code & Tests
People & History
Pick two releases to compare their code metrics.
- 0.1.1Latest
- 0.1.02026-05-04
- RR 4.6.0 released · 2026-04-24
Package metadata
- First published
- 2026-05-04
- Total releases
- 2 / 1 yrs
- License
- MIT + file LICENSE OSI
- Minimum R
- ≥ 4.1.0
- Download size
- 100 KB
Cite
Cite this package
Run in R for the authors' preferred citation:
citation("datasusr")This package has no citation file, so citation() returns this entry built from DESCRIPTION.
Cite the R Observatory
For a number measured here: a download total, a coverage figure, an archival date.
From data release v2026-10-23, which the citation names so these numbers can be found later. More on citing and the projects behind them.