View source: R/read_datasus_dbc.R
| read_datasus_dbc | R Documentation |
Reads a DATASUS .dbc file directly into R using native C code for
PKWare DCL decompression and DBF parsing. The function always returns a
tibble and is designed to work well with the tidyverse.
read_datasus_dbc(
file,
select = NULL,
n_max = Inf,
trim_ws = TRUE,
encoding = "latin1",
guess_types = TRUE,
col_types = NULL,
parse_dates = FALSE,
clean_names = TRUE,
verbose = TRUE
)
file |
Path to a |
select |
Optional character vector of column names to keep. When
|
n_max |
Maximum number of rows to read. Defaults to |
trim_ws |
Logical. Trim leading/trailing whitespace from character
fields (default |
encoding |
Encoding of the DBF character fields. One of |
guess_types |
Logical. Inspect numeric fields to distinguish
integer-like columns from double columns (default |
col_types |
Optional named character vector of explicit column types.
Supported values: |
parse_dates |
Logical. If |
clean_names |
Logical. If |
verbose |
Logical. Emit progress messages (default |
A tibble.
# The example downloads a small DATASUS file into tempdir() and reads it.
# Skipped automatically if the FTP is unreachable.
tmp <- file.path(tempdir(), "RDAC2401.dbc")
url <- paste0(
"ftp://ftp.datasus.gov.br/dissemin/publicos/SIHSUS/200801_/Dados/",
"RDAC2401.dbc"
)
ok <- tryCatch(
{
utils::download.file(url, tmp, mode = "wb", quiet = TRUE)
TRUE
},
error = function(e) FALSE,
warning = function(w) FALSE
)
if (ok) {
# Basic read (column names in snake_case by default)
x <- read_datasus_dbc(tmp, verbose = FALSE)
# Select works with either case
x <- read_datasus_dbc(
tmp,
select = c("uf_zi", "ano_cmpt", "val_tot"),
verbose = FALSE
)
# Keep original uppercase names
x <- read_datasus_dbc(tmp, clean_names = FALSE, verbose = FALSE)
unlink(tmp)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.