Skip to content

Commit

Permalink
closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jul 16, 2023
1 parent 9d51036 commit 59f88e4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 15 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# flightsbr v0.3.0999 dev

* Major changes:
* The functions `read_flights()` and `read_airport_movements()` now have a new parameter `cache`, which indicates whether the function should read cached data downloaded previously. Defaults to `TRUE`. Closed #31.

* Minor changes:
* All functions now return numeric columns with `numeric` class. Closed #32.
* The function `read_flights()` has a new parameter `cache`, which indicates whether the function should read cached data downloaded previously. Defaults to `TRUE`. Closed #31.

* Bug fixes:
* Fixed bug when unzipping files for `read_flights()` function in Unix systems.
Expand Down
13 changes: 10 additions & 3 deletions R/read_airport_movements.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'
#' @template date
#' @template showProgress
#' @template cache
#'
#' @return A `"data.table" "data.frame"` object. All columns are returned with
#' `class` of type `"character"`.
Expand All @@ -21,7 +22,9 @@
#'
#' amov2020 <- read_airport_movements(date = 2020)
#'}}
read_airport_movements <- function(date = 202001, showProgress = TRUE){
read_airport_movements <- function(date = 202001,
showProgress = TRUE ,
cache = TRUE){

### check inputs
if( ! is.logical(showProgress) ){ stop(paste0("Argument 'showProgress' must be either 'TRUE' or 'FALSE.")) }
Expand All @@ -48,7 +51,9 @@ read_airport_movements <- function(date = 202001, showProgress = TRUE){
file_url <- get_airport_movements_url(year=y, month=m)

# download and read data
dt <- download_airport_movement_data(file_url, showProgress = showProgress)
dt <- download_airport_movement_data(file_url,
showProgress,
cache)

# check if download failed
if (is.null(dt)) { return(invisible(NULL)) } # nocov
Expand Down Expand Up @@ -85,7 +90,9 @@ read_airport_movements <- function(date = 202001, showProgress = TRUE){
file_url <- get_airport_movements_url(year=y, month=m)

# download and read data
temp_dt <- download_airport_movement_data(file_url, showProgress = FALSE)
temp_dt <- download_airport_movement_data(file_url,
showProgress = FALSE,
cache)

# check if download failed
if (is.null(temp_dt)) { return(invisible(NULL)) }
Expand Down
22 changes: 17 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ get_airfares_url <- function(dom, year, month) { # nocov start
#' dest_file = tempfile(fileext = ".zip")
#' )
#'}}
download_flightsbr_file <- function(file_url, showProgress=showProgress, dest_file = temp_local_file){
download_flightsbr_file <- function(file_url = parent.frame()$file_url,
showProgress = parent.frame()$showProgress,
dest_file = temp_local_file){

# download data
try(
Expand Down Expand Up @@ -423,7 +425,7 @@ download_flightsbr_file <- function(file_url, showProgress=showProgress, dest_fi
#' # download data
#' a <- download_flights_data(file_url=file_url, showProgress=TRUE, select=NULL)
#'}}
download_flights_data <- function(file_url,
download_flights_data <- function(file_url = parent.frame()$file_url,
showProgress = parent.frame()$showProgress,
select = parent.frame()$select,
cache = parent.frame()$cache){ # nocov start
Expand Down Expand Up @@ -492,7 +494,9 @@ download_flights_data <- function(file_url,
#' # download data
#' a <- download_airfares_data(file_url=file_url, showProgress=TRUE, select=NULL)
#'}}
download_airfares_data <- function(file_url, showProgress=showProgress, select=select){ # nocov start
download_airfares_data <- function(file_url = parent.frame()$file_url,
showProgress = parent.frame()$showProgress,
select = parent.frame()$select){ # nocov start

# create temp local file
file_name <- basename(file_url)
Expand Down Expand Up @@ -588,6 +592,7 @@ get_airport_movements_url <- function(year, month) { # nocov start
#'
#' @param file_url String. A url passed from \code{\link{get_flights_url}}.
#' @param showProgress Logical, passed from \code{\link{read_flights}}
#' @param cache Logical, passed from \code{\link{read_flights}}
#'
#' @return A `"data.table" "data.frame"` object
#'
Expand All @@ -599,14 +604,21 @@ get_airport_movements_url <- function(year, month) { # nocov start
#' # download data
#' a <- download_airport_movement_data(file_url=file_url, showProgress=TRUE)
#'}}
download_airport_movement_data <- function(file_url, showProgress=showProgress){ # nocov start
download_airport_movement_data <- function(file_url = parent.frame()$file_url,
showProgress = parent.frame()$showProgress,
cache = parent.frame()$cache){ # nocov start

# # create temp local file
file_name <- basename(file_url)
temp_local_file <- paste0(tempdir(),"/",file_name)

# use cached files or not
if (cache==FALSE & file.exists(temp_local_file)) {
unlink(temp_local_file, recursive = T)
}

# check if file has not been downloaded already. If not, download it
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {
if (cache==FALSE | !file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {

# download data
download_flightsbr_file(file_url=file_url, showProgress=showProgress, dest_file = temp_local_file)
Expand Down
6 changes: 5 additions & 1 deletion man/download_airfares_data.Rd

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

8 changes: 7 additions & 1 deletion man/download_airport_movement_data.Rd

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

2 changes: 1 addition & 1 deletion man/download_flights_data.Rd

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

4 changes: 2 additions & 2 deletions man/download_flightsbr_file.Rd

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

6 changes: 5 additions & 1 deletion man/read_airport_movements.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test_read_airport_movements.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ test_that("read_airport_movements", {
# all months in a year
test2 <- read_airport_movements(date=2022, showProgress = FALSE)
testthat::expect_true(is(test2, "data.table"))


# check whether cache argument is working
time_first <- system.time(
f201506 <- read_airport_movements(date = 202210))

time_cache_true <- system.time(
f201506 <- read_airport_movements(date = 202210, cache = TRUE))

time_cache_false <- system.time(
f201506 <- read_airport_movements(date = 202210, cache = FALSE))

testthat::expect_true( time_cache_true[['elapsed']] < time_cache_false[['elapsed']] )
})


Expand Down

0 comments on commit 59f88e4

Please sign in to comment.