| Title: | Access the Brazilian 'MDS' Social Information Matrix ('MI Social') API from 'R' |
|---|---|
| Description: | 'Tidyverse'-friendly interface to the 'MI Social' API of the Brazilian Ministry of Development and Social Assistance ('MDS', 'SAGI') <https://aplicacoes.mds.gov.br/sagi/servicos/misocial>. Retrieves monthly municipal series of the 'Cadastro Unico' (registered families and people by income bracket), the 'Bolsa Familia' programme (beneficiary families, benefits and transfers), the 'BPC' benefit, the social assistance network ('CRAS', 'CREAS' and other units), 'SUAS' funding, 'Bolsa Familia' conditionality monitoring and food security programmes as tidy long-format 'tibbles'. |
| 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>) |
| Maintainer: | André Leite <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-10-31 18:54:10 UTC |
| Source: | https://github.com/cran/misocialr |
The MI Social API identifies municipalities by the first six digits of the IBGE code (without the check digit). This helper restores the official 7-digit code, using the IBGE check-digit rule plus the table of municipalities that are exceptions to it.
ibge_code7(x)ibge_code7(x)
x |
Character or numeric vector of 6-digit codes. |
A character vector of 7-digit IBGE codes (NA for inputs that
are not 6 digits).
ibge_code7(c(261160, "260790"))ibge_code7(c(261160, "260790"))
Fetches the full documents of one municipality over all months (paging through the whole history) and returns the union of the field names found, with the first and last period in which each field appears. MI Social documents are sparse (a field only exists in the months it was published), which is why the whole history is scanned rather than a single document.
mds_api_fields(locality = 261160, refresh = FALSE)mds_api_fields(locality = 261160, refresh = FALSE)
locality |
6- or 7-digit IBGE code of the municipality whose
history is scanned. Defaults to Recife ( |
refresh |
Logical. Ignore the cached result and ask the API again?
Default |
This is the catalogue of one municipality, not of the whole API: fields that exist only elsewhere (a regional CREAS, for instance) do not appear. Scan other municipalities to widen the search.
Results are cached for the session.
A tibble with columns field, unit (inferred from the
suffix; "unknown" when the suffix does not say), storage_type,
in_dictionary, first_period and last_period. Empty, with a
warning, when the API has no document for the municipality.
mds_api_fields()mds_api_fields()
Shortcut for mds_data() with the curated Bolsa Familia fields
(mds_fields("bolsa_familia")).
mds_bolsa_familia(periods = NULL, localities = "BR", fields = NULL)mds_bolsa_familia(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_bolsa_familia(periods = -6, localities = 2611606)mds_bolsa_familia(periods = -6, localities = 2611606)
Shortcut for mds_data() with the curated BPC and CadSUAS fields
(mds_fields("bpc_suas")).
mds_bpc_suas(periods = NULL, localities = "BR", fields = NULL)mds_bpc_suas(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_bpc_suas(periods = 2025, localities = "PE")mds_bpc_suas(periods = 2025, localities = "PE")
Shortcut for mds_data() with the curated Cadastro Unico fields
(mds_fields("cadunico")).
mds_cadunico(periods = NULL, localities = "BR", fields = NULL)mds_cadunico(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_cadunico(periods = 2025, localities = "PE")mds_cadunico(periods = 2025, localities = "PE")
Removes all cached localities, field lists, periods and field checks,
forcing fresh API calls on subsequent requests. To refresh a single
result, use the refresh argument of mds_periods(),
mds_localities() or mds_api_fields().
mds_clear_cache()mds_clear_cache()
No return value, called for side effects.
mds_clear_cache()mds_clear_cache()
Main retrieval function. Returns monthly municipal values of the requested fields as a tidy long tibble: one row per field x municipality x month in which the API has a value.
mds_data(fields, periods = NULL, localities = "BR", validate = TRUE)mds_data(fields, periods = NULL, localities = "BR", validate = TRUE)
fields |
Character vector of field names. See |
periods |
Periods to fetch:
|
localities |
|
validate |
Logical. Check that fields not in the dictionary exist
in the API before requesting data? Default |
The query is validated before any data request (field names, period format, locality codes), then paged through Solr and, when many municipality codes are given, split into several requests whose results are bound together.
Values are returned as published. Note that the API answers 0 for
months that were not published yet; use mds_drop_unpublished() to
remove those trailing zeros explicitly.
Only numeric fields are supported: a field whose values are text
(such as regiao) is an error rather than an empty result.
Every response is checked before it is used: a partial or malformed
answer, a total that changes while paging, a document read twice, a
document without municipality or month, a field with several values in
one document, or two different values for the same municipality, month
and field are errors (classes misocialr_error_response and
misocialr_error_conflict). A requested municipality that returns no
document raises a warning.
Options: misocialr.timeout (seconds per request, default 180) and
misocialr.quiet (TRUE hides the progress messages).
A tibble with columns locality_id (6-digit code used by the
API), ibge_code (7-digit IBGE code), locality_name, state,
period ("YYYYMM"), field, label, unit and value (double).
The time of the request is kept in attr(x, "retrieved_at").
mds_data( c("bpc_ben_i", "cadsuas_qtd_cras_i"), periods = 2025, localities = c(2611606, 2607901) )mds_data( c("bpc_ben_i", "cadsuas_qtd_cras_i"), periods = 2025, localities = c(2611606, 2607901) )
The MI Social API answers 0, not "missing", for months whose data has
not been published: the BPC series, for example, carries zeros for every
municipality up to December of the current year. Treating those zeros as
real would make the latest months look like a collapse of the programme.
mds_drop_unpublished( data, today = Sys.Date(), reference = c("selection", "brazil"), action = c("drop", "flag") )mds_drop_unpublished( data, today = Sys.Date(), reference = c("selection", "brazil"), action = c("drop", "flag") )
data |
A tibble returned by |
today |
Reference date for rule 1. Default |
reference |
Where the last published month is read from:
|
action |
|
This helper applies an explicit rule, per field:
months after the current month are future;
months after the last month with a non-zero value are unpublished;
a field with no non-zero value at all is unpublished entirely.
The "last month with a non-zero value" comes from reference:
"selection" (default): the localities in data. Needs no request,
but a small selection can legitimately end in zeros (a municipality
without a CREAS unit, for example), and those zeros would be dropped.
"brazil": every municipality in the country, through
mds_periods() (two facet requests per field when not cached). A
month is unpublished only when no municipality in Brazil has a
non-zero value, which keeps legitimate zeros of small selections.
A non-zero value in data is never classified as unpublished. If the
national reference says a month with such a value is unpublished (a
cached reference older than the data, for instance), the reference is
requested again; if the contradiction remains, the month is kept and a
warning is raised.
This is an inference from the published values, not an official
publication date. A series can legitimately end in zeros, even
nationally; use action = "flag" to inspect the classification before
removing anything.
Rows with a missing value are never classified as zero: they are kept
and flagged as "missing".
data without the dropped rows, or with the classification
columns. A message summarizes what was removed.
x <- tibble::tibble( locality_id = "261160", period = c("202506", "202507", "202508"), field = "bpc_ben_i", value = c(98000, 98500, 0) ) mds_drop_unpublished(x, today = as.Date("2025-12-01")) mds_drop_unpublished(x, today = as.Date("2025-12-01"), action = "flag")x <- tibble::tibble( locality_id = "261160", period = c("202506", "202507", "202508"), field = "bpc_ben_i", value = c(98000, 98500, 0) ) mds_drop_unpublished(x, today = as.Date("2025-12-01")) mds_drop_unpublished(x, today = as.Date("2025-12-01"), action = "flag")
Returns the curated dictionary of MI Social fields: the technical field name used by the API, a readable Portuguese label, the unit and how the value is stored in the API.
mds_fields(theme = NULL)mds_fields(theme = NULL)
theme |
Optional theme id (see |
The API itself has no data dictionary. Labels are a reading of the
technical names, checked against the published values; units are what
is counted (Famílias, Pessoas, R$, %...). storage_type is the
Solr type given by the suffix (_i integer, _l long, _f float,
_d double, _s string) and says nothing about the unit. Any other
field published by the API (see mds_api_fields()) can still be
requested by name in mds_data().
nature says how a field may be aggregated:
nature |
Across municipalities | Across months |
estoque (count at the month) |
sum | do not sum |
fluxo (amount in the month) |
sum | sum |
fluxo anual (one value a year, in December) |
sum | one value a year |
acumulado (running total) |
sum | do not sum |
taxa, média, índice |
do not sum | do not sum |
nature_basis says where the classification comes from: medido (the
monthly series was inspected: a running total only grows, an amount of
the month varies and restarts), mesmo grupo de um campo medido, or
rótulo e unidade (inferred from the label and unit, not inspected).
A tibble with columns theme, field, label, unit,
nature, nature_basis and storage_type.
mds_fields("bpc_suas")mds_fields("bpc_suas")
Returns the municipalities as identified by the MI Social API, with the 6-digit code the API uses and the official 7-digit IBGE code. The list is read from one month (by default December of the last closed year), paged and checked for completeness; it is cached for the session.
mds_localities(state = NULL, period = NULL, refresh = FALSE)mds_localities(state = NULL, period = NULL, refresh = FALSE)
state |
Optional state abbreviations (e.g. |
period |
Month ( |
refresh |
Logical. Ignore the cached list and ask the API again?
Default |
A tibble with columns locality_id (6 digits, as in the API),
ibge_code (7 digits), locality_name, state and region. The
month and time of the request are kept in attr(x, "period") and
attr(x, "retrieved_at").
mds_localities("PE")mds_localities("PE")
Shortcut for mds_data() with the curated conditionality fields
(mds_fields("pbf_condicionalidades")): the SICON series from 2023 on
(education, health, warnings, blocks, suspensions and cancellations) and
the IGD-M follow-up rates, available since 2015.
mds_pbf_condicionalidades(periods = NULL, localities = "BR", fields = NULL)mds_pbf_condicionalidades(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_pbf_condicionalidades(periods = 2026, localities = "PE")mds_pbf_condicionalidades(periods = 2026, localities = "PE")
Uses Solr facets to count, for each month, how many municipalities have a value for the field and how many of those values are non-zero.
mds_periods(field, localities = "BR", refresh = FALSE)mds_periods(field, localities = "BR", refresh = FALSE)
field |
A single field name (see |
localities |
Localities to restrict the count to; same forms as in
|
refresh |
Logical. Ignore the cached result and ask the API again?
Default |
The second count matters: the API returns 0 for months that have not
been published yet (for instance, the BPC series carries zeros for every
municipality up to the end of the current year). A month where
n_nonzero is 0 for the whole selection is almost always unpublished,
not a true zero. See mds_drop_unpublished().
Results are cached for the session. Large selections are counted in
blocks of at most 200 municipality codes (the same split as
mds_data()); the blocks do not overlap, so their counts are summed.
A tibble with columns period ("YYYYMM"), n_localities and
n_nonzero, ordered by period. The time of the request is kept in
attr(x, "retrieved_at").
mds_periods("bpc_ben_i", localities = "PE")mds_periods("bpc_ben_i", localities = "PE")
Shortcut for mds_data() with the curated food security fields
(mds_fields("seguranca_alimentar")). Cistern counts are cumulative.
mds_seguranca_alimentar(periods = NULL, localities = "BR", fields = NULL)mds_seguranca_alimentar(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_seguranca_alimentar(periods = 2025, localities = "PE")mds_seguranca_alimentar(periods = 2025, localities = "PE")
Shortcut for mds_data() with the curated funding fields
(mds_fields("suas_financiamento")).
mds_suas_financiamento(periods = NULL, localities = "BR", fields = NULL)mds_suas_financiamento(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
The transfer components add up to suas_repasse_mun_vl_total_fundo_f
(checked for Pernambuco in 06/2026 and 09/2026). The account balances do
not add up to the general total, so they should be read one by one.
A tibble in the format of mds_data().
mds_suas_financiamento(periods = 2025, localities = 2611606)mds_suas_financiamento(periods = 2025, localities = 2611606)
Shortcut for mds_data() with the curated network fields
(mds_fields("suas_rede")): CRAS, CREAS, Centro POP, centros de
convivencia, centros-dia, unidades de acolhimento, regional CREAS and the
professionals registered in each kind of unit (professionals only from
09/2025).
mds_suas_rede(periods = NULL, localities = "BR", fields = NULL)mds_suas_rede(periods = NULL, localities = "BR", fields = NULL)
periods |
Periods to fetch:
|
localities |
|
fields |
Fields to fetch. |
A tibble in the format of mds_data().
mds_suas_rede(periods = 202609, localities = "PE")mds_suas_rede(periods = 202609, localities = "PE")
Each theme groups a curated set of MI Social fields (see mds_fields())
and has a shortcut retrieval function: mds_cadunico(),
mds_bolsa_familia(), mds_bpc_suas(), mds_suas_rede(),
mds_suas_financiamento(), mds_pbf_condicionalidades() and
mds_seguranca_alimentar().
mds_themes()mds_themes()
A tibble with columns theme, name, description and
n_fields.
mds_themes()mds_themes()