
Get and aggregate data by year from SIDRA API
Source:R/sidra_download_by_year.R
      sidra_download_by_year.Rdsidra_download_by_year() retrieves data from the Brazilian Institute of
Geography and Statistics (IBGE) Automatic
Retrieval System (SIDRA) API for a specified
range of years.
This function addresses the SIDRA API's limitations on the volume of data that can be downloaded in a single request. It downloads data for each year individually and then combines the results into a single data frame.
Note: This function requires an active internet connection and the
sidrar package to be
installed.
Arguments
- years
- An - integerishvector with the years to download.
- api_start
- A string specifying the initial part of the SIDRA API URL, up to (but not including) the year segment. See the Details section for guidance. 
- api_end
- A string specifying the final part of the SIDRA API URL, immediately following the year segment. See the Details section for guidance. 
Value
A tibble containing the combined data for all requested years, as retrieved from the SIDRA API.
Details
To construct the API call, follow these steps:
- Visit the SIDRA website. 
- Locate the desired table containing your data. 
- Configure the parameters for data retrieval (e.g., variable, sex, years). 
- Click the share button (link symbol) at the end of the page. 
- If a checkbox labeled Usar períodos relativos, quando possível. appears, uncheck it, reload the page, and click the share button again. 
- Copy the portion of the Parâmetros para a API URL that starts with - /t(e.g.,- /t/6407...).
You need to provide the function with separate parts of the API URL. For example:
|-------- Start ------|--- Years ----|----- End -----|
/t/6407/n6/all/v/606/p/2021,2022,2023/c2/6794/c58/1140If you have difficulty identifying the correct segments, try adjusting the table settings, selecting different years, and examining how the URL changes.
Examples
library(curl)
library(dplyr)
# \dontrun{
  if (has_internet()) {
    sidra_download_by_year(
      years = 2010:2011,
      api_start = "/t/1612/n6/all/v/109/p/",
      api_end = "/c81/2692"
    ) |>
      glimpse()
  }
#> ⠙ Downloading data from 2010
#> All others arguments are desconsidered when 'api' is informed
#> ✔ Downloading data from 2010 [2.4s]
#> 
#> ⠙ Downloading data from 2011
#> All others arguments are desconsidered when 'api' is informed
#> ✔ Downloading data from 2011 [1.4s]
#> 
#> Rows: 11,126
#> Columns: 13
#> $ `Nível Territorial (Código)`                <chr> "6", "6", "6", "6", "6"…
#> $ `Nível Territorial`                         <chr> "Município", "Município…
#> $ `Unidade de Medida (Código)`                <chr> "1006", "1006", "1006",…
#> $ `Unidade de Medida`                         <chr> "Hectares", "Hectares",…
#> $ Valor                                       <dbl> 750, 5706, 700, 1230, 3…
#> $ `Município (Código)`                        <chr> "1100015", "1100023", "…
#> $ Município                                   <chr> "Alta Floresta D'Oeste …
#> $ `Variável (Código)`                         <chr> "109", "109", "109", "1…
#> $ Variável                                    <chr> "Área plantada", "Área …
#> $ `Ano (Código)`                              <chr> "2010", "2010", "2010",…
#> $ Ano                                         <chr> "2010", "2010", "2010",…
#> $ `Produto das lavouras temporárias (Código)` <chr> "2692", "2692", "2692",…
#> $ `Produto das lavouras temporárias`          <chr> "Arroz (em casca)", "Ar…
# }