Skip to content

Commit

Permalink
1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-andersen committed Apr 19, 2022
1 parent eb95361 commit 74c2e85
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ComtradeDatabaseDownloader
Title: Comtrade Database Downloader for Gravity Models
Version: 1.1.2
Version: 1.1.3
Description: Package to download trade data from the Comtrade Database and wrangle it into a dataframe to be used in Gravity Model type estimations.
License: MIT + file LICENSE
Encoding: UTF-8
Expand Down
35 changes: 30 additions & 5 deletions R/comtrade_database_downloader.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#' A support function for the \code{\link{get_comtrade}} function.
#'
#' @export
get_comtrade_file <- function(freq, year, month = NULL, token)
{
get_comtrade_file <- function(freq, year, month = NULL, token) {
# Compose URL used to call Comtrade API
base_url <- "http://comtrade.un.org/api/get/bulk/C"
if (freq == "monthly") {
Expand Down Expand Up @@ -87,6 +87,7 @@ get_comtrade_file <- function(freq, year, month = NULL, token)
unlink(c(f, z), recursive = TRUE)

return(df)

}

#' Wrangle Comtrade Database Period
Expand Down Expand Up @@ -200,6 +201,10 @@ wrangle_comtrade_data <- function(df, freq)
#' \code{value} column as. If \code{FALSE} is specified, the column will be
#' converted to \code{numeric} (since some \code{R} operations do not support
#' the \code{Integer64} class).
#' @param timeout A single integer. Default: max of either 300 seconds or the
#' current download timeout parameter given by \code{getOption("timeout")}.
#' If specified, will set the timeout parameter for the current download
#' session.
#' @return Returns a \code{data.frame}. The \code{data.frame} contains the
#' complete set of bilateral trade flows reported to the Comtrade database
#' within the specified time period.
Expand Down Expand Up @@ -227,8 +232,9 @@ get_comtrade <- function(freq,
endmonth = NULL,
token,
savedir = NULL,
int64 = TRUE)
{
int64 = TRUE,
timeout = NULL) {

# Assigning default values
if (freq == "annual") {
if (missing(endyear)) {
Expand Down Expand Up @@ -363,6 +369,12 @@ get_comtrade <- function(freq,
if (!is.character(token)) {
stop("Comtrade token must be a string.")
}
if (!missing(int64) & !(is.logical(int64))) {
stop("'int64' must be a logical.")
}
if (!missing(timeout) & !(is.integer(timeout))) {
stop("'timeout' must be an integer.")
}

# If monthly frequency is specified
if (freq == "monthly") {
Expand All @@ -385,6 +397,15 @@ get_comtrade <- function(freq,
)
}

# Set download timeout option
current_timeout <- getOption("timeout")

if (missing(timeout)) {
options(timeout = max(300, current_timeout))
} else {
options(timeout = max(timeout, current_timeout))
}

# Start timer
starttime <- Sys.time()

Expand Down Expand Up @@ -580,7 +601,8 @@ get_comtrade <- function(freq,
years[i]
))

temp <- get_comtrade_file(freq, years[i], token = token)
temp <- get_comtrade_file(
freq, years[i], token = token)
temp <- wrangle_comtrade_data(temp, freq)

# Row-bind data.frames
Expand Down Expand Up @@ -619,6 +641,9 @@ get_comtrade <- function(freq,
" minutes"
))

# Reset timeout option
options(timeout = current_timeout)

return(df)
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object suited for Gravity Model estimation.

### Prerequisites:

- Access to a network with premium site license subscription to the Comtrade
- Access to a network with a premium site license subscription to the Comtrade
Database.
- Creation of an [API token](https://comtrade.un.org/api/swagger/ui/index\#/Auth)

Expand Down
8 changes: 7 additions & 1 deletion man/get_comtrade.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74c2e85

Please sign in to comment.