Skip to content

Commit

Permalink
Closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Mar 26, 2022
1 parent 294078b commit daeb1e7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: flightsbr
Title: Download Flight and Airport Data from Brazil
Version: 0.1.29
Date: 2022-03-18
Date: 2022-03-25
Authors@R:
c(person(given="Rafael H. M.", family="Pereira",
email="rafa.pereira.br@gmail.com",
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flightsbr v0.1.3
# flightsbr v0.1.29

* Minor changes:
* The data downloaded in `read_flights()` is now cached in temp dir. Closed [#20](https://github.com/ipeaGIT/flightsbr/issues/21).
* All columns are now returned with class `character`. This fixes a bug in the `read_airport_movements()` function. Closed [#20](https://github.com/ipeaGIT/flightsbr/issues/20).


Expand Down
23 changes: 13 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ download_flights_data <- function(file_url, showProgress=showProgress, select=se
# create temp local file
# file_name <- substr(file_url, (nchar(file_url) + 1) -17, nchar(file_url) )
file_name <- basename(file_url)
temp_local_file <- tempfile( file_name )
temp_local_file <- paste0(tempdir(),"/",file_name)


# 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) {

# download data
try(
Expand All @@ -233,9 +237,6 @@ download_flights_data <- function(file_url, showProgress=showProgress, select=se
config = httr::config(ssl_verifypeer = FALSE)
), silent = TRUE)

# address of zipped file stored locally
temp_local_file_zip <- paste0('unzip -p ', temp_local_file)

# check if file has been downloaded, try a 2nd time
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {

Expand All @@ -248,15 +249,19 @@ download_flights_data <- function(file_url, showProgress=showProgress, select=se
), silent = TRUE)
}

# check if file has been downloaded
# Halt function if download failed
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {
message('Internet connection not working.')
return(invisible(NULL)) }
}

### set threads for fread
orig_threads <- data.table::getDTthreads()
data.table::setDTthreads(percent = 100)

# address of zipped file stored locally
temp_local_file_zip <- paste0('unzip -p ', temp_local_file)

# read zipped file stored locally
dt <- data.table::fread( cmd = temp_local_file_zip, select=select, colClasses = 'character')

Expand Down Expand Up @@ -348,11 +353,9 @@ get_airport_movements_url <- function(year, month) { # nocov start
#'}}
download_airport_movement_data <- function(file_url, showProgress=showProgress){ # nocov start

# create temp local file
# file_name <- substr(file_url, (nchar(file_url) + 1) -17, nchar(file_url) )
file_name <- basename(file_url)

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

### set threads for fread
orig_threads <- data.table::getDTthreads()
Expand Down
6 changes: 3 additions & 3 deletions tests_rafa/test_rafa.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# devtools::install_github("ipeaGIT/r5r", subdir = "r-package", force=T)
library(flightsbr)

m2020 <- read_airport_movements(date = 2020)
m2020 <- read_flights(date = 2020)
m2019 <- read_airport_movements(date = 2019)

m201901 <- read_airport_movements(date = 201901)
m201911 <- read_airport_movements(date = 201911)
m201901 <- read_flights(date = 201901)
m201911 <- read_flights(date = 2019)

m201901$DT_PREVISTO |> class()
m201911$DT_PREVISTO |> class()
Expand Down

0 comments on commit daeb1e7

Please sign in to comment.