Skip to content

Commit

Permalink
closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jul 7, 2023
1 parent 01f997c commit 11fdfdc
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# flightsbr v0.3.0999 dev

* Minor changes:
* All functions now return numeric columns with `numeric` class. Closed #32.

* Bug fixes:
* Fixed bug when unzipping files for `read_flights()` function in Unix systems.
Closed #31.
Expand Down
6 changes: 4 additions & 2 deletions R/flightsbr.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @name flightsbr
#' @aliases flightsbr-package
#'
#' @importFrom data.table := %like%
#' @importFrom data.table := %like% .SD
#' @importFrom utils globalVariables unzip
"_PACKAGE"

Expand All @@ -24,5 +24,7 @@ utils::globalVariables( c('month',
'year',
'latitude',
'longitude',
'temp_local_file') )
'temp_local_file',
'NR_',
'nr_') )

2 changes: 2 additions & 0 deletions R/read_aircrafts.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ read_aircrafts <- function( showProgress = TRUE ){
message('Internet connection not working.') # nocov
return(invisible(NULL)) }

# convert columns to numeric
convert_to_numeric(rab_dt)

# return output
return(rab_dt)
Expand Down
7 changes: 7 additions & 0 deletions R/read_airfares.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ if (nchar(date)==6) {
# check if download failed
if (is.null(dt)) { return(invisible(NULL)) }

# convert columns to numeric
convert_to_numeric(dt)

return(dt)


Expand Down Expand Up @@ -108,6 +111,10 @@ pbapply::pboptions(original_options)

# row bind data tables
dt <- data.table::rbindlist(dt_list, fill=TRUE)

# convert columns to numeric
convert_to_numeric(dt)

return(dt)

}}
Expand Down
7 changes: 7 additions & 0 deletions R/read_airport_movements.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ read_airport_movements <- function(date = 202001, showProgress = TRUE){
# check if download failed
if (is.null(dt)) { return(invisible(NULL)) } # nocov

# convert columns to numeric
convert_to_numeric(dt)

return(dt)


Expand Down Expand Up @@ -97,6 +100,10 @@ read_airport_movements <- function(date = 202001, showProgress = TRUE){

# row bind data tables
dt <- data.table::rbindlist(dt_list)

# convert columns to numeric
convert_to_numeric(dt)

return(dt)
}
}
11 changes: 9 additions & 2 deletions R/read_airports.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ if (any(type %in% c('public', 'all'))){

# add type info
data.table::setDT(dt_public)[, type := 'public']
# dt_public$type <- 'public'

# convert columns to numeric
convert_to_numeric(dt_public)

}

Expand Down Expand Up @@ -132,7 +134,9 @@ if (any(type %in% c('private', 'all'))){

# add type info
data.table::setDT(dt_private)[, type := 'private']
# dt_private$type <- 'private'

# convert columns to numeric
convert_to_numeric(dt_private)

}

Expand All @@ -153,6 +157,9 @@ if (type == 'all') {

dt <- data.table::rbindlist(list(dt_public, dt_private), use.names=FALSE)

# convert columns to numeric
convert_to_numeric(dt)

return(dt)
}
}
Expand Down
7 changes: 7 additions & 0 deletions R/read_flights.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ if (length(date) == 1 & nchar(date[1])==6) {
# check if download failed
if (is.null(dt)) { return(invisible(NULL)) }

# convert columns to numeric
convert_to_numeric(dt)

return(dt)
}

Expand Down Expand Up @@ -128,6 +131,10 @@ pbapply::pboptions(original_options)

# row bind data tables
dt <- data.table::rbindlist(dt_list)

# convert columns to numeric
convert_to_numeric(dt)

return(dt)
}

18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,22 @@ download_airport_movement_data <- function(file_url, showProgress=showProgress){
data.table::setDTthreads(orig_threads)

return(dt)
}


#' @keywords internal
convert_to_numeric <- function(dt) {

# detect if there are any columns that should be numeric
numeric_cols <- names(dt)[names(dt) %like% 'NR_' | names(dt) %like% 'nr_']

if (length(numeric_cols)==0) { return(invisible(TRUE)) }

# convert columns to numeric
data.table::setDT(dt)
suppressWarnings(
dt[,(numeric_cols):= lapply(.SD, as.numeric), .SDcols = numeric_cols]
)

return(invisible(TRUE))
} # nocov end
3 changes: 2 additions & 1 deletion tests/testthat/test_read_flights.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test_that("read_flights", {
test1 <- read_flights()
testthat::expect_true(is(test1, "data.table"))
testthat::expect_true(nrow(test1) >0 )
testthat::expect_equal( class(test1$nr_voo), 'numeric')

# one month, combinada, no progress bar
test2 <- read_flights(date=202201, type='combinada' ,showProgress = FALSE)
Expand All @@ -33,7 +34,7 @@ test_that("read_flights", {
testthat::expect_true(is(test4, "data.table"))
testthat::expect_true(nrow(test4) >0 )
months <- unique(test4$nr_mes_referencia)
testthat::expect_true( all.equal(months , c('1','2')) )
testthat::expect_true( all.equal(months , c(1,2)) )

})

Expand Down

0 comments on commit 11fdfdc

Please sign in to comment.