Skip to contents

This function downloads Comexstat (Brazilian trade statistics) data from the MDIC website for specified years, directions (imports/exports), and types (NCM/HS4). It can also optionally download and manage auxiliary data tables. After download and checking the csv files, the data is stores as parquet files under a data directory, in order to increase speed.

Usage

comex_download(
  years = 2024,
  directions = c("imp", "exp"),
  types = c("hs4", "ncm"),
  cache = TRUE,
  .progress = TRUE,
  n_tries = 30,
  force_download_aux = FALSE,
  timeout = 600,
  ...
)

Arguments

years

A numeric vector or integer specifying the years for which data should be downloaded. Defaults to the current year.

directions

A character vector specifying the directions of trade: 'imp' (imports) and/or 'exp' (exports). Defaults to both.

types

A character vector specifying the types of data: 'ncm' (Nomenclatura Comum do Mercosul) and/or 'hs4' (Harmonized System 4-digit). Defaults to both.

cache

A logical value indicating whether to use cached files if they exist. Defaults to TRUE.

.progress

A logical value indicating whether to display a progress bar during downloads. Defaults to TRUE.

n_tries

The maximum number of download attempts before giving up. Defaults to 30.

force_download_aux

A logical value indicating whether to force the download of auxiliary data tables (e.g., URF, VIA, country codes), even if they already exist in the cache. Defaults to FALSE. Auxiliary data is typically downloaded when a new trade data file is downloaded.

timeout

The maximum time (in seconds) to wait for a download response. Defaults to 600 seconds (10 minutes).

...

Additional arguments to be passed to curl::multi_download, such as headers, handle, etc.

Value

invisible(NULL) if successful. The function primarily downloads data to the specified directories.

Details

This function performs the following steps:

  1. File Structure: Ensures the necessary directories exist to store downloaded files.

  2. URL Generation: Constructs URLs for Comexstat data files based on the specified years, directions, and types.

  3. Download (with Retry): Downloads files using curl::multi_download with retry logic in case of failures.

  4. Auxiliary Data: If there is new trade data or force_download_aux is TRUE, it downloads and manages auxiliary data tables.

  5. Error Handling: Checks if any downloads failed or if the downloaded files are valid.

  6. Write parquet files: Stores data as parquet files in order to speed up analyses.

Examples

if (FALSE) { # \dontrun{
# Download all data for 2023 and 2024
comex_download(years = 2023:2024)

# Download only import data (NCM) for 2024
comex_download(years = 2024, directions = 'imp', types = 'ncm')
} # }