datasusr 0.1.1
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.
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.
datasusr 0.1.0
CRAN release: 2026-05-04
CRAN review fixes
-
DESCRIPTIONnow consistently single-quotes external software/API names (‘DATASUS’, ‘DBC’, ‘DBF’, ‘FTP’, ‘C’, ‘PKWare DCL’, ‘blast’, ‘zlib’) and unquotes the in-package function referencedatasus_fetch(). The description field also now references the upstream sources (DATASUS file transfer site and Adler 2003 for the bundled blast decompressor). - All examples switched from
\dontrun{}to\donttest{}. Network- dependent examples are wrapped intryCatch()and write totempdir()so that running them never touches the user’s home filespace. - The default cache directory is now a session-scoped subdirectory of
tempdir()instead oftools::R_user_dir("datasusr", "cache"). To opt in to a persistent cache across sessions, set theDATASUSR_CACHE_DIRenvironment variable, thedatasusr.cache_dirR option, or passcache_direxplicitly.
Breaking changes
- Package version bumped to 0.1.0 to reflect the expanded scope beyond just reading DBC files.
New features
-
datasus_fetch(): high-level convenience function that lists, downloads, and reads DATASUS files in a single call. Supports column selection, type specification, and row-binding of multiple files. -
DATASUSR_CACHE_DIRenvironment variable: the cache directory now respects theDATASUSR_CACHE_DIRenv var in addition to the existingdatasusr.cache_diroption. -
format_bytes()is now exported for general use.
Bug fixes
-
SIM path templates:
DOEXT,DOINF, andDOMATare served from the sameDOFETFTP directory asDOFET(verified against the live FTP); the templates register that directory for each of them. -
Fixed column/argument name shadowing in
datasus_build_path(),datasus_file_types(), anddatasus_list_files(). Column names likesourceandfile_typeno longer clash with function arguments thanks to proper use of.env$pronouns. - Download failures no longer abort the loop: individual FTP download errors are caught and reported without stopping the remaining downloads.
- Empty file lists now return immediately from
datasus_download()instead of proceeding with zero iterations.
Improvements
-
Removed all
.data$pronoun usage insidedplyrverbs. Columns are now referenced with bare (tidy evaluation) names, and external variables use.env$where needed. This follows current tidyverse best practice. -
Replaced deprecated purrr functions:
pmap_dfr()replaced withmap2() |> list_rbind(). -
Replaced
purrr::map_lgl()in cache functions with basevapply()for fewer dependencies in hot paths. -
Improved CLI messages throughout: all user-facing messages now use
cli::cli_abort()with structured hints (ibullets) instead of barestop(). Progress messages are more concise and informative. - Wrapped FTP connection errors with a user-friendly message suggesting a connectivity check.
-
rlangis now an explicit dependency (was already an indirect dep viadplyr), making the%||%import explicit. - Added
rlang (>= 1.0.0)anddplyr (>= 1.1.0)minimum version constraints. - Vignettes expanded with richer explanations and a new comparison article.
- pkgdown site structure updated with a comparison article.
- Compiled
.oand.sofiles excluded from the source bundle via.Rbuildignore.
datasusr 0.0.7.3
- Fixed dplyr data-masking bugs in catalog and file listing helpers.
- Removed
.data$usage inside mutate pipelines and simplified tidyverse expressions. - Improved verbose output in
read_datasus_dbc()so row and column counts print on separate lines.
datasusr 0.0.7.1
- Fixed
datasus_file_types(source = ...)filtering consistency. - Improved
clispacing and summary formatting in key functions. - Filtered invalid source/file type combinations earlier in
datasus_list_files().
datasusr 0.0.7
- Standardized user-facing progress and status messages with
cli. - Added progress bars and cache-hit messages to
datasus_download(). - Added optional
verbosecontrol to FTP, listing, and cache helpers. - Improved cache summaries with human-readable sizes.
datasusr 0.0.6
- Added a configurable cache layer for DATASUS downloads.
- Added
datasus_cache_dir(),datasus_cache_list(),datasus_cache_info(),datasus_cache_clear(), anddatasus_cache_prune(). - Integrated cache-aware downloads into
datasus_download().
datasusr 0.0.5
- Added DATASUS catalog helpers:
datasus_sources(),datasus_modalities(),datasus_file_types(), anddatasus_ufs(). - Added FTP helpers:
datasus_ftp_ls(),datasus_build_path(),datasus_list_files(), anddatasus_download().
datasusr 0.0.4
- Added
guess_types,col_types, andparse_datestoread_datasus_dbc(). - Improved DBC handling for files with DBF headers plus compressed record areas.