| Title: | Access the 'IBGE' Aggregate Data API from 'R' |
|---|---|
| Description: | 'Tidyverse'-friendly interface to the Brazilian Institute of Geography and Statistics ('IBGE') aggregate data 'API' <https://servicodados.ibge.gov.br/api/docs/agregados?versao=3>. Query aggregates, variables, localities, periods, and metadata from surveys and censuses conducted by 'IBGE'. |
| Authors: | André Leite [aut, cre] (ORCID: <https://orcid.org/0000-0002-4718-9766>), Marcos Wasiliew [aut] (ORCID: <https://orcid.org/0009-0004-4694-3159>), Hugo Vasconcelos [aut] (ORCID: <https://orcid.org/0000-0001-6249-0920>), Carlos Amorim [aut] (ORCID: <https://orcid.org/0000-0001-6315-8305>), Diogo Bezerra [aut] (ORCID: <https://orcid.org/0000-0002-1216-8674>), Allan Quadros [rev] (ORCID: <https://orcid.org/0000-0003-3250-5380>, Reviewed the package for rOpenSci, see <https://github.com/ropensci/software-review/issues/787>), Diana García-Cortés [rev] (ORCID: <https://orcid.org/0000-0003-1562-7319>, Reviewed the package for rOpenSci, see <https://github.com/ropensci/software-review/issues/787>) |
| Maintainer: | André Leite <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.1 |
| Built: | 2026-10-14 13:59:04 UTC |
| Source: | https://github.com/ropensci/ibger |
Parses a SIDRA API URL and fetches the data using ibge_variables(),
returning the same tidy tibble format.
fetch_sidra_url(url, validate = TRUE)fetch_sidra_url(url, validate = TRUE)
url |
Character string. A SIDRA API URL, typically starting with
|
validate |
Logical. If |
A tibble in tidy (long) format, same as
ibge_variables().
url <- paste0( "https://apisidra.ibge.gov.br/values", "/t/7060/n1/all/v/63/p/last%2012/c315/7169" ) fetch_sidra_url(url) # Pipe-friendly: inspect then fetch url |> parse_sidra_url() url |> fetch_sidra_url()url <- paste0( "https://apisidra.ibge.gov.br/values", "/t/7060/n1/all/v/63/p/last%2012/c315/7169" ) fetch_sidra_url(url) # Pipe-friendly: inspect then fetch url |> parse_sidra_url() url |> fetch_sidra_url()
Retrieves the set of available aggregates (tables), grouped by survey. Each aggregate corresponds to a SIDRA table. Results are cached in memory per unique combination of parameters, so repeated calls with the same filters are instant.
ibge_aggregates( period = NULL, subject = NULL, classification = NULL, periodicity = NULL, level = NULL )ibge_aggregates( period = NULL, subject = NULL, classification = NULL, periodicity = NULL, level = NULL )
period |
Period of interest, as a periodicity code followed by one
or more period ids in brackets: |
subject |
Numeric subject code (e.g. |
classification |
Numeric classification code (e.g. |
periodicity |
Periodicity code (see Details): |
level |
Geographic level: |
All filters are optional. Their format is checked before the request: the IBGE API silently ignores filters it cannot parse (returning the whole catalog) or answers HTTP 500, so malformed values are rejected here with an informative error. A well-formed filter that matches no aggregate returns an empty tibble with a warning.
Periodicity codes used by the API (as observed in the catalog):
| Code | Periodicity |
P1 |
Annual |
P5 |
Monthly |
P7 |
Every three years |
P8 |
Semi-annual |
P9 |
Quarterly |
P11 |
Every two years |
P13 |
Rolling quarter (PNAD Contínua) |
P16 |
Every six years |
A tibble with columns:
survey_id, survey_name, aggregate_id, aggregate_name
ibge_aggregates() ibge_aggregates(periodicity = "P5") ibge_aggregates(level = "N6") ibge_aggregates(subject = 70) ibge_aggregates(period = "P5[202001]")ibge_aggregates() ibge_aggregates(periodicity = "P5") ibge_aggregates(level = "N6") ibge_aggregates(subject = 70) ibge_aggregates(period = "P5[202001]")
Removes all cached metadata (aggregates and surveys), forcing fresh API calls on subsequent requests.
ibge_clear_cache()ibge_clear_cache()
No return value, called for side effects.
ibge_clear_cache()ibge_clear_cache()
Launches a Shiny app for browsing and filtering the full catalog of IBGE aggregates. The app displays value boxes with summary counts, a searchable table with column-level filters, and lets you filter by survey or search aggregate names.
ibge_explorer(launch.browser = TRUE)ibge_explorer(launch.browser = TRUE)
launch.browser |
Logical or function. If |
Clicking a row shows a notification with the aggregate ID and the
corresponding ibge_metadata() call. A CSV download button is also
available.
This function is called for its side effect (launching the app).
Returns the value of shiny::runApp() invisibly.
# Open in RStudio Viewer ibge_explorer(launch.browser = FALSE) # Open in browser ibge_explorer()# Open in RStudio Viewer ibge_explorer(launch.browser = FALSE) # Open in browser ibge_explorer()
Retrieves available localities for an aggregate at one or more geographic levels. Validates the requested level(s) against the aggregate metadata before querying.
ibge_localities(aggregate, level = "N6", validate = TRUE)ibge_localities(aggregate, level = "N6", validate = TRUE)
aggregate |
Numeric aggregate identifier. |
level |
Geographic level. Use |
validate |
Logical. If |
When several levels are requested, one request is made per level and the results are combined. (The API's own multi-level endpoint returns an empty list when any of the requested levels has no localities for the aggregate, which would silently hide the other levels.)
A tibble with columns:
id, name, level_id, level_name
ibge_localities(1437, level = "N1") ibge_localities(1437, level = c("N2", "N3"))ibge_localities(1437, level = "N1") ibge_localities(1437, level = c("N2", "N3"))
Retrieves complete metadata for an aggregate: name, survey, subject, periodicity, territorial levels, variables, and classifications with all their categories.
ibge_metadata(aggregate)ibge_metadata(aggregate)
aggregate |
Numeric aggregate identifier. |
A list of class ibge_metadata with:
id, name, url, survey, subject
periodicity: list with frequency, start and end
territorial_level: list with administrative, special and ibge
variables: tibble with id, name, unit
classifications: tibble with id, name and list-column categories,
where each element is a tibble with category_id, category_name,
category_unit and category_level
meta <- ibge_metadata(7060) meta$variables meta$classifications tidyr::unnest(meta$classifications, categories)meta <- ibge_metadata(7060) meta$variables meta$classifications tidyr::unnest(meta$classifications, categories)
Retrieves available periods for an aggregate.
ibge_periods(aggregate)ibge_periods(aggregate)
aggregate |
Numeric aggregate identifier. |
A tibble with columns:
id, literal, modification
ibge_periods(1705)ibge_periods(1705)
Returns a tibble with IBGE subject (theme) codes and descriptions. These codes can be used to understand the thematic organization of IBGE surveys and aggregates.
ibge_subjects(pattern = NULL, ignore_case = TRUE)ibge_subjects(pattern = NULL, ignore_case = TRUE)
pattern |
Optional character string. A regular expression used to
filter subject descriptions. If |
ignore_case |
Logical. If |
The table is built-in (no API call is made) and contains all subject codes currently defined by IBGE.
A tibble with columns:
Integer subject code.
Character subject description.
# All subjects ibge_subjects() # Find sanitation-related themes ibge_subjects("saneamento|esgoto|lixo") # Find themes mentioning "internet" ibge_subjects("internet")# All subjects ibge_subjects() # Find sanitation-related themes ibge_subjects("saneamento|esgoto|lixo") # Find themes mentioning "internet" ibge_subjects("internet")
Retrieves detailed institutional and methodological metadata for a survey in a given reference period. Before querying, both the survey code and the year/month combination are validated against the IBGE catalog, so invalid inputs produce clear errors without wasting an API call.
ibge_survey_metadata(survey, year, month = NULL, order = 0)ibge_survey_metadata(survey, year, month = NULL, order = 0)
survey |
Character string. The survey code (e.g. |
year |
Integer. Reference year. Must be a year with available
metadata for the survey (see |
month |
Integer or |
order |
Integer. Publication order within the period (default |
The structure of occurrences varies by survey and may include fields
such as objective, data collection method, sample design, geographic
scope, reference period, and more.
A list of class ibge_survey_metadata with:
Survey status (corrente, concluída, desativada, etc.)
conjuntural or estrutural
Type of statistical operation
Responsible area
Survey acronym
Survey start date
Deactivation date, if applicable
URL to SIDRA data
URL to CONCLA (CNAE classifications)
Tibble of thematic classifications
List of metadata records for the period (structure varies by survey)
ibge_surveys(), ibge_survey_periods()
# Structural survey (no month needed) ibge_survey_metadata("CD", year = 2022) # Conjunctural survey (specify month) ibge_survey_metadata("SC", year = 2023, month = 6) # Inspect methodology fields meta <- ibge_survey_metadata("CD", year = 2022) names(meta$occurrences[[1]]) # Invalid code: clear error with suggestions try(ibge_survey_metadata("PMS", year = 2024)) #> Error: Survey code "PMS" not found in the IBGE catalog. # Invalid year: error with available range try(ibge_survey_metadata("CD", year = 1800)) #> Error: Year 1800 not available for survey "CD". #> i Available years: 1940 to 2022 (9 total).# Structural survey (no month needed) ibge_survey_metadata("CD", year = 2022) # Conjunctural survey (specify month) ibge_survey_metadata("SC", year = 2023, month = 6) # Inspect methodology fields meta <- ibge_survey_metadata("CD", year = 2022) names(meta$occurrences[[1]]) # Invalid code: clear error with suggestions try(ibge_survey_metadata("PMS", year = 2024)) #> Error: Survey code "PMS" not found in the IBGE catalog. # Invalid year: error with available range try(ibge_survey_metadata("CD", year = 1800)) #> Error: Year 1800 not available for survey "CD". #> i Available years: 1940 to 2022 (9 total).
Retrieves the periods (year/month combinations) for which a given survey has associated metadata records. Before querying, the survey code is validated against the IBGE catalog; invalid codes produce a helpful error with suggestions.
ibge_survey_periods(survey)ibge_survey_periods(survey)
survey |
Character string. The survey code as returned by
|
A tibble with columns:
Integer year with metadata available.
Integer month (1–12) or NA for structural
(annual or longer) surveys.
Publication order within the period (0 = most recent).
ibge_surveys(), ibge_survey_metadata()
# Conjunctural survey (monthly periods) ibge_survey_periods("SC") # Pesquisa Mensal de Serviços # Structural survey (annual periods) ibge_survey_periods("CD") # Censo Demográfico # Invalid code: helpful error with suggestions try(ibge_survey_periods("PMS")) #> Error: Survey code "PMS" not found in the IBGE catalog. #> i Did you mean one of these? #> * SC - Pesquisa Mensal de Serviços #> ...# Conjunctural survey (monthly periods) ibge_survey_periods("SC") # Pesquisa Mensal de Serviços # Structural survey (annual periods) ibge_survey_periods("CD") # Censo Demográfico # Invalid code: helpful error with suggestions try(ibge_survey_periods("PMS")) #> Error: Survey code "PMS" not found in the IBGE catalog. #> i Did you mean one of these? #> * SC - Pesquisa Mensal de Serviços #> ...
Retrieves the full catalog of IBGE surveys (statistical operations) that have associated metadata in the IBGE Metadata API, including status, category, collection and publication frequency, and thematic classifications.
ibge_surveys(thematic_classifications = TRUE)ibge_surveys(thematic_classifications = TRUE)
thematic_classifications |
Logical. If |
This function queries a different API from the other ibge_* functions.
While ibge_aggregates() and ibge_metadata() use the Aggregates API (v3),
this function uses the Metadata API (v2), which provides institutional and
methodological documentation about surveys.
A tibble with columns:
Survey code (e.g. "SC", "CD")
Survey name in Portuguese
Survey name in English (may be NA)
Survey status: Ativa, Encerrada, etc.
Conjuntural, Estrutural, or Especial
Data collection frequency (Mensal, Anual, etc.)
Publication frequency
List-column of tibbles with thematic
classification details (name, domain, description). Only if
thematic_classifications = TRUE.
ibge_survey_periods(), ibge_survey_metadata()
# Full catalog ibge_surveys() # Flat table without classifications ibge_surveys(thematic_classifications = FALSE) # Filter active conjunctural surveys library(dplyr) ibge_surveys(thematic_classifications = FALSE) |> filter(status == "Ativa", category == "Conjuntural")# Full catalog ibge_surveys() # Flat table without classifications ibge_surveys(thematic_classifications = FALSE) # Filter active conjunctural surveys library(dplyr) ibge_surveys(thematic_classifications = FALSE) |> filter(status == "Ativa", category == "Conjuntural")
Main function of the package. Retrieves variable results from an IBGE aggregate for the specified localities, periods and classifications.
ibge_variables( aggregate, variable = NULL, periods = -6, localities = "BR", classification = NULL, view = NULL, validate = TRUE, chunk = TRUE )ibge_variables( aggregate, variable = NULL, periods = -6, localities = "BR", classification = NULL, view = NULL, validate = TRUE, chunk = TRUE )
aggregate |
Numeric aggregate identifier (SIDRA table). |
variable |
Variable(s) to retrieve. Can be:
|
periods |
Period(s) to query. Can be:
|
localities |
Locality(ies) to query. Can be:
|
classification |
Classification(s) to filter results. Named list
where names are classification IDs and values are category ID vectors.
Use |
view |
Display mode: |
validate |
Logical. If |
chunk |
Controls automatic splitting of large queries. The IBGE API rejects requests whose result is too large with an HTTP 500 error (the documented limit is 100,000 values, but in practice requests fail above about 50,000). Can be:
|
Before querying the API, validates all parameters against the aggregate metadata. If any parameter is invalid, stops with a clear error message showing the allowed values.
Note that large requests can be slow on the server side: queries covering
all municipalities (localities = "N6", some 5,500 localities) may take
one to several minutes per request to be answered by the IBGE API, even
before any chunking. This is normal — the function is not hanging.
A tibble in tidy (long) format with columns:
variable_id, variable_name, variable_unit,
classification columns (when present),
locality_id, locality_name, locality_level,
period, value
# IPCA in Brazil ibge_variables(7060, localities = "BR") # Specific variables for states (quarterly animal slaughter) ibge_variables(1092, variable = c(284, 285), localities = "N3") # Large query (all municipalities): split automatically into # multiple requests to respect the API's 100,000 value limit ibge_variables(1612, localities = "N6") # Specific municipalities with classification ibge_variables( aggregate = 1712, variable = 214, periods = -3, localities = list(N6 = c(3550308, 3304557)), classification = list("226" = c(4844, 96608)) )# IPCA in Brazil ibge_variables(7060, localities = "BR") # Specific variables for states (quarterly animal slaughter) ibge_variables(1092, variable = c(284, 285), localities = "N3") # Large query (all municipalities): split automatically into # multiple requests to respect the API's 100,000 value limit ibge_variables(1612, localities = "N6") # Specific municipalities with classification ibge_variables( aggregate = 1712, variable = 214, periods = -3, localities = list(N6 = c(3550308, 3304557)), classification = list("226" = c(4844, 96608)) )
Converts the character value column returned by ibge_variables() to
numeric, handling IBGE special value conventions.
parse_ibge_value(x)parse_ibge_value(x)
x |
A character or numeric vector of IBGE values. |
According to IBGE's tabular presentation standards, the value column may contain special character codes instead of numbers:
| Code | Meaning |
- |
Numeric value equal to zero (not from rounding) |
.. |
Not applicable |
... |
Data not available |
X |
Suppressed to avoid identifying individual respondents |
A numeric vector where:
"-" becomes 0
"..", "..." and "X" become NA_real_
All other values are converted with as.numeric()
parse_ibge_value(c("1.5", "10", "-", "..", "...", "X", NA)) #> [1] 1.5 10.0 0.0 NA NA NA NA # Typical usage after ibge_variables() library(dplyr) ibge_variables(7060, localities = "BR") |> mutate(value = parse_ibge_value(value))parse_ibge_value(c("1.5", "10", "-", "..", "...", "X", NA)) #> [1] 1.5 10.0 0.0 NA NA NA NA # Typical usage after ibge_variables() library(dplyr) ibge_variables(7060, localities = "BR") |> mutate(value = parse_ibge_value(value))
Converts a SIDRA API URL (from the SIDRA Query Builder or sidrar package) into a human-readable breakdown of its parameters, enriched with names from the aggregate metadata.
parse_sidra_url(url)parse_sidra_url(url)
url |
Character string. A SIDRA API URL, typically starting with
|
A list of class sidra_query with:
aggregate: list with id and name
variables: tibble with id and name
periods: character vector of period codes
localities: list of level/locality pairs
classifications: list of classification/category details
ibger_call: string with the equivalent ibge_variables() call
url <- paste0( "https://apisidra.ibge.gov.br/values", "/t/5434/n1/all/v/4090/p/last%201/c888/47946,56623" ) parse_sidra_url(url)url <- paste0( "https://apisidra.ibge.gov.br/values", "/t/5434/n1/all/v/4090/p/last%201/c888/47946,56623" ) parse_sidra_url(url)