Monday, November 23, 2026 From rOpenSci (https://ropensci.org/blog/2026/11/23/ibger/). Except where otherwise noted, content on this site is licensed under the CC-BY license.
The Brazilian Institute of Geography and Statistics (IBGE) produces Brazil’s official statistics: the demographic census, the IPCA inflation index1, municipal GDP, and dozens of recurring economic, social and agricultural surveys. Their results are published as thousands of aggregate tables in SIDRA, IBGE’s data retrieval system, and exposed through the IBGE Aggregates API.
ibger is an R client for that API. It lets you discover tables, inspect their metadata, and retrieve data as tidy tibbles. The package recently passed rOpenSci software peer review, and version 0.2.1 is on CRAN.
ibger grew out of everyday work. We are part of the Strategic Monitoring Data Science Team of the Pernambuco state government, in northeastern Brazil, which builds R-based tools to monitor state public policies2. Our dashboards and reports draw on IBGE indicators, and they are updated by scripts that pull the same tables again and again, often for every municipality and for many years at once. We wanted that access to be scripted, validated and reproducible, rather than a series of manual downloads from the SIDRA website. Less than a week after the first CRAN release, Daniel Vartanian opened an issue explaining that he split his calls into smaller chunks by hand to get around the API’s limits, and suggested submitting the package to rOpenSci. Both ideas stuck.
This tech note is about one problem that every heavy user of the API runs into sooner or later: the limit on how many values a single request can return, and how ibger works around it.
Each SIDRA table crosses variables, periods, geographic units and classification categories, so results grow multiplicatively. A modest question such as “how much soybean did each of Brazil’s 5,570 municipalities produce in each year since 2000?” already means more than 140,000 values.
The API documentation states a limit of 100,000 values per request. In practice, requests start to fail well before that: while developing ibger we saw a request for 44,560 values succeed and one for 50,130 values fail. And when a request is too large, the API does not say so. It answers with a generic HTTP 500 error.
Here is what happens if we send the soybean query in a single request, with
automatic splitting turned off (chunk = FALSE). We use table 1612
(municipal crop production), variable 214 (quantity produced), category 2713
of classification 81 (soybeans) and the municipal level "N6":
library(ibger)
soy <- ibge_variables(
1612,
variable = 214,
periods = 2000:2025,
localities = "N6",
classification = list("81" = 2713),
chunk = FALSE
)
Error:
! Failed to access the IBGE API.
✖ HTTP 500 Internal Server Error. ℹ The API returned error 500. This usually
means the query exceeds the API's result-size limit (documented as 100,000
values, in practice around 50,000). Try reducing localities, periods or
categories, or let `chunk = TRUE` split the request automatically.
ℹ URL:
<https://servicodados.ibge.gov.br/api/v3/agregados/1612/periodos/2000|2001|2002|2003|2004|2005|2006|2007|2008|2009|2010|2011|2012|2013|2014|2015|2016|2017|2018|2019|2020|2021|2022|2023|2024|2025/variaveis/214>
The usual workaround is to split the query by hand into several smaller requests, guess sizes that will work, and bind the results back together. ibger does this for you.
Before sending a data request, ibger estimates the size of the result as the
product of the number of variables, periods, localities and categories
involved. Most of the numbers come straight from the arguments. The ones
that do not, such as “all municipalities” or “the last six periods”, are
resolved from the table metadata: ibge_metadata() lists a table’s
variables and classifications, and the period and locality endpoints list
what each table covers. These lookups are cached, so each one is made only
once per table in a session.
The same metadata powers ibger’s validation: invalid variables, periods, geographic levels or categories are caught before any data request is sent, with an error that lists the allowed values.
If the estimate exceeds the limit, ibger splits the query into chunks: first by periods and then, if a single period is still too large, by localities. Locality chunks are also capped at 500 codes per request to keep URLs at a safe length. Each chunk is requested in turn and the results are combined into a single tibble.
With the default chunk = TRUE, the same soybean query just works:
soy <- ibge_variables(
1612,
variable = 214,
periods = 2000:2025,
localities = "N6",
classification = list("81" = 2713)
)
✔ Aggregate 1612: 8 variables,
1 classification, 45 categories (cached).
ℹ Fetching N6 localities for aggregate 1612 from IBGE API...
✔ Fetching N6 localities for aggregate 1612 from IBGE API... [149ms]
ℹ Fetching period list for aggregate 1612 from IBGE API...
✔ Fetching period list for aggregate 1612 from IBGE API... [101ms]
ℹ Estimated result exceeds the API limit (50000 values);
splitting into 4 requests.
ℹ Fetching chunk 1/4 for aggregate 1612 from IBGE API...
✔ Fetching chunk 1/4 for aggregate 1612 from IBGE API... [237ms]
ℹ Fetching chunk 2/4 for aggregate 1612 from IBGE API...
✔ Fetching chunk 2/4 for aggregate 1612 from IBGE API... [230ms]
ℹ Fetching chunk 3/4 for aggregate 1612 from IBGE API...
✔ Fetching chunk 3/4 for aggregate 1612 from IBGE API... [285ms]
ℹ Fetching chunk 4/4 for aggregate 1612 from IBGE API...
✔ Fetching chunk 4/4 for aggregate 1612 from IBGE API... [280ms]
✔ 144664 records retrieved.
soy
# A tibble: 144,664 × 9
variable_id variable_name variable_unit classification_81 locality_id
<chr> <chr> <chr> <chr> <chr>
1 214 Quantidade produzida Toneladas Soja (em grão) 1100015
2 214 Quantidade produzida Toneladas Soja (em grão) 1100015
3 214 Quantidade produzida Toneladas Soja (em grão) 1100015
4 214 Quantidade produzida Toneladas Soja (em grão) 1100015
5 214 Quantidade produzida Toneladas Soja (em grão) 1100015
6 214 Quantidade produzida Toneladas Soja (em grão) 1100015
7 214 Quantidade produzida Toneladas Soja (em grão) 1100015
8 214 Quantidade produzida Toneladas Soja (em grão) 1100015
9 214 Quantidade produzida Toneladas Soja (em grão) 1100023
10 214 Quantidade produzida Toneladas Soja (em grão) 1100023
# ℹ 144,654 more rows
# ℹ 4 more variables: locality_name <chr>, locality_level <chr>, period <chr>,
# value <chr>
The estimate was 26 periods × 5,564 municipalities, about 145,000 values, so ibger fetched up to eight years per request, four requests in all.
If you need smaller requests, for example on an unstable connection, pass
your own limit, as in chunk = 20000. With chunk = FALSE the query is
sent as is.
The value column is character, because IBGE uses special codes for
values that are zero after rounding (-), not applicable (..), not
available (...) or suppressed for confidentiality (X).
parse_ibge_value() converts them to numbers or NA. With that, the
145,000 values summarise into a picture of two decades of soybean expansion
in Brazil:
library(dplyr)
library(ggplot2)
by_year <- soy |>
mutate(
year = as.integer(period),
tonnes = parse_ibge_value(value)
) |>
group_by(year) |>
summarise(
`Number of producing municipalities` = sum(tonnes > 0, na.rm = TRUE),
`Production (million metric tons)` = sum(tonnes, na.rm = TRUE) / 1e6
) |>
tidyr::pivot_longer(-year)
# first and last year of each series, to label the endpoints
ends <- by_year |>
filter(year %in% range(year)) |>
mutate(first = year == min(year))
ggplot(by_year, aes(year, value)) +
geom_line(linewidth = 1, colour = "#2a7f62") +
geom_point(data = ends, size = 2.5, colour = "#2a7f62") +
geom_text(
aes(
label = scales::comma(value, accuracy = 1),
hjust = ifelse(first, -0.3, 1),
vjust = ifelse(first, 1, -1)
),
data = ends
) +
facet_wrap(~name, scales = "free_y") +
scale_y_continuous(expand = expansion(mult = c(0.1, 0.2))) +
labs(
title = "Growth in soybean production and geographic expansion (2000–2025)",
x = NULL, y = NULL
) +
theme_minimal(base_size = 13) +
theme(plot.title.position = "plot")

Soybean production and producing municipalities in Brazil, 2000 to 2025 (IBGE, Produção Agrícola Municipal, table 1612).
Production grew about fivefold over the period, while the number of municipalities producing it nearly doubled, from about 1,450 to over 2,600.
ibger also translates URLs from the legacy SIDRA API, such as those
produced by the SIDRA Query Builder or used in existing scripts, into
equivalent calls with parse_sidra_url() and fetch_sidra_url(), and ships
an interactive Shiny explorer for browsing the table catalog
(ibge_explorer()). The
getting started vignette
and the vignette on
the concepts behind the IBGE API
are good places to start. If you work in Python, the sibling package
ibgepy offers the same
functions.
We are grateful to the editor Beatriz Milz and to the reviewers Allan Quadros and Diana García-Cortés, whose review, run against the live API, made ibger much better. Bug reports, questions and ideas are welcome in the issue tracker.
IPCA stands for Índice Nacional de Preços ao Consumidor Amplo (Extended National Consumer Price Index), Brazil’s official inflation measure and the reference for the central bank’s inflation target. ↩︎
Medeiros, H. A. V., Wanderley, A. L., Bezerra, D. de C., Amorim Filho, C. A. G. de, Nóbrega, R. Z. da, & Ferreira, F. G. de M. (2025). Government Data Science Teams: A Framework for Implementing Strategic Monitoring Solutions. dg.o 2025: 26th Annual International Conference on Digital Government Research. https://doi.org/10.59490/dgo.2025.925 ↩︎