View source: R/datasus_catalog.R
| datasus_fetch | R Documentation |
A convenience wrapper that lists, downloads, and reads DATASUS files in a single call. Particularly useful for interactive / exploratory work.
datasus_fetch(
source,
file_type,
year = NULL,
month = NULL,
uf = NULL,
...,
bind = TRUE,
include_prelim = TRUE,
timeout = Inf,
use_cache = TRUE,
cache_dir = NULL,
refresh = FALSE,
overwrite = FALSE,
verbose = TRUE
)
source |
Character vector of source codes. |
file_type |
Character vector of file type codes. |
year |
Integer vector of years. |
month |
Integer vector of months (required for monthly sources). |
uf |
Character vector of UF codes (required for UF-scoped sources). |
... |
Additional arguments forwarded to |
bind |
Logical. When |
include_prelim |
Logical. Include preliminary data trees (default |
timeout |
Optional hard cap, in seconds, on each file transfer
(default |
use_cache |
Logical. Reuse cached downloads (default |
cache_dir |
Optional cache directory. |
refresh |
Logical. Force re-download even when a cached file exists
(default |
overwrite |
Logical. Re-download files that already exist at the
destination (default |
verbose |
Logical. Emit progress messages (default |
Files that fail to download are skipped with a warning; if every download fails, an error is raised.
When the same file is available both as a final release and as a
preliminary one (a SIM DO year that has just been consolidated, or a
SINAN file requested together with its SINAN_P alias), only the final
version is read, so rows are not duplicated. A final file that could not be
confirmed on the FTP does not suppress a preliminary copy that is known to
exist. Use datasus_list_files() and datasus_download() directly to work
with both copies.
A tibble (when bind = TRUE) or a named list of tibbles. List
names are the file names; when the same file name comes from more than
one FTP tree they are prefixed with the period (e.g.
"prelim/DOPE2022.dbc").
tryCatch({
# Fetch a small SIHSUS slice into tempdir() (network required).
df <- datasus_fetch(
source = "SIHSUS",
file_type = "RD",
year = 2024,
month = 1,
uf = "AC",
select = c("uf_zi", "ano_cmpt", "munic_res", "val_tot"),
cache_dir = tempdir(),
verbose = FALSE
)
}, error = function(e) message("FTP unavailable: ", conditionMessage(e)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.