% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/read_datasus_dbc.R
\name{read_datasus_dbc}
\alias{read_datasus_dbc}
\title{Read DATASUS DBC files}
\usage{
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
)
}
\arguments{
\item{file}{Path to a \code{.dbc} or \code{.dbf} file. Both compressed (DBC) and
uncompressed (DBF) formats are accepted.}

\item{select}{Optional character vector of column names to keep. When
\code{NULL} (the default), all columns are returned. Names are matched
case-insensitively, so both \code{"UF_ZI"} and \code{"uf_zi"} work.}

\item{n_max}{Maximum number of rows to read. Defaults to \code{Inf} (all rows).}

\item{trim_ws}{Logical. Trim leading/trailing whitespace from character
fields (default \code{TRUE}).}

\item{encoding}{Encoding of the DBF character fields. Typically \code{"latin1"}
(the default for DATASUS files) or \code{"UTF-8"}.}

\item{guess_types}{Logical. Inspect numeric fields to distinguish
integer-like columns from double columns (default \code{TRUE}). Disable for
faster reads when precise types are not needed.}

\item{col_types}{Optional named character vector of explicit column types.
Supported values: \code{"character"}, \code{"integer"}, \code{"double"}, \code{"logical"},
\code{"date"}. Names are matched case-insensitively against the original
DBF field names.}

\item{parse_dates}{Logical. If \code{TRUE}, DBF date fields (\code{D} type) are
returned as \code{Date}. Character fields are only parsed as dates when
explicitly listed in \code{col_types} (default \code{FALSE}).}

\item{clean_names}{Logical. If \code{TRUE} (the default), column names are
converted to \code{snake_case} (lowercase with underscores). The original
DATASUS names are uppercase (e.g. \code{UF_ZI} becomes \code{uf_zi}).}

\item{verbose}{Logical. Emit progress messages (default \code{TRUE}).}
}
\value{
A tibble.
}
\description{
Reads a DATASUS \code{.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.
}
\examples{
\donttest{
# 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)
}
}
}
