The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Package {sidrar}


Type: Package
Title: An Interface to IBGE's SIDRA API
Version: 0.5.0
Description: Provides a flexible interface to discover, inspect, plan, and retrieve aggregate data from the Brazilian Institute of Geography and Statistics (IBGE) through its SIDRA application programming interfaces. SIDRA is IBGE's system for retrieving aggregate statistical data.
Depends: R (≥ 3.6.0)
License: GPL-3
Encoding: UTF-8
Language: en-US
URL: https://github.com/rpradosiqueira/sidrar/
BugReports: https://github.com/rpradosiqueira/sidrar/issues/
Imports: httr, jsonlite, utils
Suggests: knitr, rmarkdown, testthat (≥ 3.2.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-08-25 20:40:17 UTC; renat
Author: Renato Prado Siqueira [aut, cre]
Maintainer: Renato Prado Siqueira <rpradosiqueira@gmail.com>
Repository: CRAN
Date/Publication: 2026-08-26 02:20:03 UTC

Get a SIDRA table

Description

Retrieves aggregate data from the Brazilian Institute of Geography and Statistics (IBGE) SIDRA API.

Usage

get_sidra(
  x,
  variable = "allxp",
  period = "last",
  geo = "Brazil",
  geo.filter = NULL,
  classific = "all",
  category = "all",
  header = TRUE,
  format = 4,
  digits = "default",
  api = NULL,
  value_type = c("numeric", "character", "both"),
  geo_view = NULL,
  include_extinct = FALSE
)

Arguments

x

A numeric SIDRA table code. It may be omitted when api is used.

variable

A vector of variable codes. Defaults to "allxp", which selects all variables except automatically generated percentages.

period

A character vector of period codes, "all", or a single named value such as c(last = 12) or c(first = 5). Defaults to "last", the latest available period.

geo

A character vector containing supported geographic levels. Aliases and their nNN codes are case-insensitive. Defaults to "Brazil".

geo.filter

A list of geographic filters. Each element corresponds positionally to an element of geo; names may identify a higher geographic level, such as list(State = 50) for cities in a state.

classific

A vector of classification codes. Defaults to "all".

category

"all" or a list containing categories for each classification.

header

Logical. Should the first API record be used as the returned column names?

format

An integer from 1 to 4 controlling the returned descriptor fields. See Details.

digits

"default", "max", or an integer from 0 to 9.

api

A relative SIDRA API path or a complete URL under ⁠https://apisidra.ibge.gov.br/values⁠. When supplied, the other query arguments are ignored.

value_type

How the value column is returned: "numeric" preserves the historical numeric interface, "character" preserves SIDRA symbols, and "both" keeps the numeric column and appends a ⁠_raw⁠ column.

geo_view

Optional numeric SIDRA territorial-view code, using the API's G parameter instead of an N level. It cannot be combined with explicitly supplied geo or geo.filter values.

include_extinct

Logical. Include extinct territorial units in geo queries through the API's ⁠/u/y⁠ parameter. It cannot be combined with geo_view.

Details

Supported values of geo are "Brazil", "Region", "State", "IntermediaryRegion", "ImmediateRegion", "MesoRegion", "MicroRegion", "MetroRegion", "MetroRegionDiv", "IRD", "UrbAglo", "PopArrang", "City", "District", "subdistrict", and "Neighborhood". Their corresponding nNN codes and all aliases are accepted without regard to letter case.

format = 1 returns codes, format = 2 returns names, format = 3 returns codes and names for geographic units plus names for other descriptors, and format = 4 returns codes and names for all descriptors.

Requests use HTTPS, UTF-8 decoding, a timeout, and limited retries for transient failures. Set options(sidrar.timeout = 120) or options(sidrar.retries = 4) to override their defaults. Responses are requested live and are not cached by the package.

HTTP conditions inherit from sidrar_http_error and carry status_code, response_body, and url. Transport failures may additionally inherit from sidrar_timeout_error, sidrar_tls_error, sidrar_dns_error, sidrar_connection_error, or sidrar_transient_error when the underlying failure can be identified conservatively.

When SIDRA rejects a query for exceeding its per-request value limit, get_sidra() raises a sidrar_limit_error, which also inherits from sidrar_http_error. The condition records requested_values, limit_values, and minimum_batches. Split an explicit dimension such as period, geo.filter, variable, or category across disjoint calls and combine the returned rows.

The SIDRA API uses special value symbols. With the default value_type = "numeric", non-numeric symbols such as "-", "X", "..", and "..." become NA, as in earlier versions. Use value_type = "character" or "both" when those distinctions matter.

Geographic identifiers returned by SIDRA should be kept as character strings. In particular, "Neighborhood" (n102) identifiers belong to SIDRA's territorial level and are not census tract identifiers; do not join them directly without an official correspondence.

Value

A base data.frame.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

info_sidra(), search_sidra(), sidra_query(), and sidra_collect()

Examples

## Not run: 
get_sidra(
  x = 7060,
  variable = 63,
  period = c(last = 12),
  geo = "City",
  geo.filter = list(State = 50),
  classific = "c315",
  category = list(7169)
)

get_sidra(
  api = "/t/7060/n1/all/v/63/p/last/c315/7169/h/n"
)

## End(Not run)

List the parameters of a SIDRA table

Description

Uses the official JSON table descriptor to return variables, periods, classifications, categories, and geographic levels available in a table.

Usage

info_sidra(x, wb = FALSE)

Arguments

x

A numeric SIDRA table code.

wb

Logical. When TRUE, open the official HTML descriptor in the default browser. When FALSE, return structured metadata.

Details

Metadata is requested live from the official descriptor endpoint and is not cached by the package. The timeout and retry options described in get_sidra() also apply.

Value

When wb = FALSE, a list with components table, period, variable, classific_category, and geo. When wb = TRUE, the descriptor URL is returned invisibly after the browser is opened.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

get_sidra() and sidra_metadata()

Examples

## Not run: 
info_sidra(7060)
info_sidra(7060, wb = TRUE)

## End(Not run)

Search SIDRA tables

Description

Searches table titles in the official IBGE aggregate catalog.

Usage

search_sidra(x)

Arguments

x

A non-empty character vector containing the search terms.

Details

Matching is case- and accent-insensitive. When x contains multiple terms, every term must occur in the title, but the terms need not be adjacent. The catalog is requested live and is not cached by the package. The timeout and retry options described in get_sidra() also apply.

Value

A named character vector with matching SIDRA table titles. Names are the table codes. Returns character(0) when there are no matches.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

get_sidra(), info_sidra(), and sidra_catalog()

Examples

## Not run: 
search_sidra("contas nacionais")
search_sidra("IPCA")

## End(Not run)

Clear the sidrar metadata cache

Description

Removes only cache entries created by sidrar; the cache directory itself and unrelated files are preserved.

Usage

sidra_cache_clear(cache_dir = NULL)

Arguments

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Value

The number of removed entries, invisibly.

See Also

sidra_cache_info()


Inspect the sidrar metadata cache

Description

Lists entries created explicitly by the discovery functions when their cache argument is enabled. Value responses downloaded by get_sidra() are never cached by these helpers.

Usage

sidra_cache_info(cache_dir = NULL)

Arguments

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Value

A data frame with one row per sidrar cache file: normalized key, absolute path, byte size, fetched_at, expires_at, and valid. valid is TRUE only for a structurally readable, unexpired entry whose stored key matches its file name.

See Also

sidra_cache_clear()


Discover SIDRA tables

Description

Returns the official IBGE aggregate catalog as a rectangular table while retaining the research group to which each SIDRA table belongs.

Usage

sidra_catalog(refresh = FALSE, cache = FALSE, ttl = 1800, cache_dir = NULL)

Arguments

refresh

A single logical value. When caching is enabled, bypass and replace an existing entry.

cache

A single logical value. Cache normalized catalog or metadata responses on disk. The default is FALSE, preserving live behavior.

ttl

A positive cache lifetime in seconds. The default is 30 minutes.

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Details

The catalog is obtained from the official version 3 aggregate API. Unknown fields added by the API are ignored. Missing fields are returned as NA without changing the documented column types.

Value

A base data.frame() with columns research_id, research_name, table_id, and table_name. Identifier columns are character vectors. Research groups without tables are retained with missing table fields.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

search_sidra(), sidra_metadata()

Other SIDRA discovery functions: sidra_locations(), sidra_metadata(), sidra_periods()

Examples

## Not run: 
catalog <- sidra_catalog()
subset(catalog, grepl("prices", table_name, ignore.case = TRUE))

## End(Not run)

Collect one or more planned SIDRA queries

Description

Executes queries sequentially, checks that every batch has the same column names and types, and combines the rows without sorting or deduplicating them. Parallel requests are intentionally not used.

Usage

sidra_collect(x, value_type = NULL, provenance = FALSE)

Arguments

x

A sidra_query(), sidra_plan(), sidra_split() result, or a non-empty list of sidra_query objects.

value_type

Optional value representation overriding the preference stored in each query: "numeric", "character", or "both".

provenance

Logical. Attach URLs, access time, package version, and batch count as a sidrar_provenance attribute.

Value

A base data.frame(). When provenance = TRUE, its sidrar_provenance attribute can be read with sidra_provenance().

See Also

sidra_query(), sidra_split(), sidra_provenance()

Examples

## Not run: 
query <- sidra_query(
  7060,
  variable = 63,
  period = as.character(202401:202406),
  geo = "Brazil",
  classific = "c315",
  category = list(7169)
)
sidra_collect(sidra_split(query, "period", size = 3))

## End(Not run)

List the locations available for a SIDRA table

Description

List the locations available for a SIDRA table

Usage

sidra_locations(
  table,
  level,
  refresh = FALSE,
  cache = FALSE,
  ttl = 1800,
  cache_dir = NULL
)

Arguments

table

A single SIDRA table code.

level

A single geographic level, with or without the "N" prefix; for example, "N6" or 6 for municipalities.

refresh

A single logical value. When caching is enabled, bypass and replace an existing entry.

cache

A single logical value. Cache normalized catalog or metadata responses on disk. The default is FALSE, preserving live behavior.

ttl

A positive cache lifetime in seconds. The default is 30 minutes.

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Details

Locations come from the official table- and level-specific version 3 endpoint. Location identifiers are SIDRA territorial identifiers; they must not be coerced to numeric values or assumed to be census tract codes.

Value

A base data.frame() with columns table_id, level_id, level_name, location_id, and location_name. All identifiers are character vectors.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

sidra_metadata()

Other SIDRA discovery functions: sidra_catalog(), sidra_metadata(), sidra_periods()

Examples

## Not run: 
sidra_locations(7060, "N6")

## End(Not run)

Retrieve structured SIDRA table metadata

Description

Normalizes the official version 3 metadata and period responses into a stable collection of base data frames.

Usage

sidra_metadata(
  table,
  refresh = FALSE,
  cache = FALSE,
  ttl = 1800,
  cache_dir = NULL
)

Arguments

table

A single SIDRA table code.

refresh

A single logical value. When caching is enabled, bypass and replace an existing entry.

cache

A single logical value. Cache normalized catalog or metadata responses on disk. The default is FALSE, preserving live behavior.

ttl

A positive cache lifetime in seconds. The default is 30 minutes.

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Details

sidra_metadata() requests both the table metadata endpoint and the table-specific period endpoint. This makes periods equivalent to sidra_periods() and keeps the table's complete period inventory separate from its frequency and first/last-period summary. Within each classification, category_order is the one-based order in the official response. Together with level, it preserves the API's hierarchical preorder; categories are deliberately not sorted by identifier.

Value

A named list with six data frames. table contains table_id, table_name, research_name, subject, url, frequency, period_start, and period_end. periods has the schema documented by sidra_periods(). variables contains table_id, variable_id, variable_name, unit, and the character list-column summarized_by. classifications contains table_id, classification_id, classification_name, summarizable, and the character list-column summarization_exceptions. categories contains table_id, classification_id, category_id, category_name, unit, level, and category_order. geographies contains table_id, group, and level_id. All API identifiers are character vectors; no raw JSON object is exposed.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

sidra_catalog(), sidra_periods(), sidra_locations()

Other SIDRA discovery functions: sidra_catalog(), sidra_locations(), sidra_periods()

Examples

## Not run: 
metadata <- sidra_metadata(7060)
metadata$variables
metadata$geographies

## End(Not run)

List the periods available for a SIDRA table

Description

List the periods available for a SIDRA table

Usage

sidra_periods(
  table,
  refresh = FALSE,
  cache = FALSE,
  ttl = 1800,
  cache_dir = NULL
)

Arguments

table

A single SIDRA table code.

refresh

A single logical value. When caching is enabled, bypass and replace an existing entry.

cache

A single logical value. Cache normalized catalog or metadata responses on disk. The default is FALSE, preserving live behavior.

ttl

A positive cache lifetime in seconds. The default is 30 minutes.

cache_dir

Optional cache directory. By default, the platform-specific user cache directory for sidrar is used.

Details

Data come from the official table-specific version 3 period endpoint. Period identifiers are kept as character strings so leading zeros and non-calendar codes cannot be lost.

Value

A base data.frame() with columns table_id, period_id, period_name, alternative_names, and modified. The alternative_names column is a base list-column of character vectors.

Author(s)

Renato Prado Siqueira rpradosiqueira@gmail.com

See Also

sidra_metadata()

Other SIDRA discovery functions: sidra_catalog(), sidra_locations(), sidra_metadata()

Examples

## Not run: 
sidra_periods(7060)

## End(Not run)

Estimate the size of a SIDRA query

Description

Creates an offline plan from a sidra_query() object or official SIDRA values URL. Exact cardinalities are calculated only when they follow from explicit selections in the URL. Special selections such as all, allxp, first, and last, territorial views and containment filters remain unknown until metadata is available.

Usage

sidra_plan(query, limit = NULL)

Arguments

query

A sidra_query object or an official SIDRA values URL.

limit

Optional positive numeric limit used only to classify risk. No SIDRA limit is assumed by the package.

Value

A list of class sidra_plan containing table, url, a stable dimensions data frame, total_estimated, limit, exceeds_limit, and risk. The originating query is retained internally for use by sidra_collect(). total_estimated is NA whenever any cardinality is unknown.

See Also

sidra_query()

Examples

query <- sidra_query(
  1612,
  variable = c(214, 215),
  period = c("2020", "2021"),
  geo = "n1",
  classific = "c81",
  category = list(c(2702, 2703))
)
sidra_plan(query, limit = 20)

Extract SIDRA result provenance

Description

Extract SIDRA result provenance

Usage

sidra_provenance(x)

Arguments

x

An object returned by sidra_collect() with provenance = TRUE.

Value

The provenance list, with accessed_at, package_version, batch_count, urls, and value_type; or NULL when none is attached.

See Also

sidra_collect()


Build a SIDRA query without downloading values

Description

Constructs and validates a SIDRA values URL. This is useful for inspecting a request or passing it to sidra_plan() before any values are downloaded.

Usage

sidra_query(
  x,
  variable = "allxp",
  period = "last",
  geo = "Brazil",
  geo.filter = NULL,
  classific = "all",
  category = "all",
  header = TRUE,
  format = 4,
  digits = "default",
  api = NULL,
  value_type = c("numeric", "character", "both"),
  geo_view = NULL,
  include_extinct = FALSE
)

Arguments

x

A numeric SIDRA table code. It may be omitted when api is used.

variable

A vector of variable codes. The special selections "all" and "allxp" are also accepted.

period

A character vector of period codes, "all", or a single named value such as c(last = 12) or c(first = 5).

geo

A character vector with geographic aliases or nNN level codes. Aliases and codes are case-insensitive.

geo.filter

A list of geographic filters corresponding to geo. Names may be aliases or nNN codes and are case-insensitive.

classific

A vector of classification codes.

category

"all" or a list of categories for each classification.

header

Logical. Should the API include its header record?

format

An integer from 1 to 4 controlling descriptor fields.

digits

"default", "max", or an integer from 0 to 9.

api

A relative SIDRA API path or complete official values URL. When supplied, the other URL-building arguments are ignored.

value_type

Preferred value representation for a later collection: "numeric", "character", or "both". It does not change the URL.

geo_view

Optional numeric SIDRA territorial-view code. Territorial views (G) cannot be combined with geo or geo.filter (N).

include_extinct

Logical. Add ⁠/u/y⁠ to an N query so extinct territorial units may be returned. It cannot be used with geo_view.

Value

A list of class sidra_query with stable url, header, and parameters components. No values are downloaded.

See Also

get_sidra() and sidra_plan()

Examples

query <- sidra_query(
  1612,
  variable = 214,
  period = "2021",
  geo = "n1",
  classific = "c81",
  category = list(2702)
)
query

Split an explicit SIDRA query into disjoint batches

Description

Divides one explicit query dimension without downloading values. The resulting batches can be executed with sidra_collect(). Special selectors such as all, allxt, first, and last must first be resolved to explicit codes. Each vector element must represent one member; embedded comma lists and period ranges are rejected because their cardinality cannot be enforced by size.

Usage

sidra_split(
  query,
  by = c("period", "variable", "geo.filter", "category"),
  size,
  index = 1L
)

Arguments

query

A structured sidra_query() object. Queries created from ⁠api =⁠ URLs cannot be split because their original argument structure is unavailable.

by

One of "period", "variable", "geo.filter", or "category".

size

Maximum number of selected members in each batch.

index

For list-valued geo.filter and category, the element to split. The default is the first element.

Details

Geographic filters can be split only when the query requests one non-Brazil geographic level. Queries with multiple territorial levels are rejected because unchanged levels would overlap across batches. Classifications already resolved in the original URL are reused without another metadata request.

Value

An object of class sidra_batch containing disjoint queries in their original order.

See Also

sidra_query(), sidra_plan(), sidra_collect()

Examples

query <- sidra_query(
  1612,
  variable = 214,
  period = as.character(2018:2022),
  geo = "Brazil",
  classific = "c81",
  category = list(2702)
)
batches <- sidra_split(query, by = "period", size = 2)
length(batches$queries)

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.