Skip to content

Commit

Permalink
closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jul 7, 2023
1 parent e9a57a6 commit b5d634c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export(read_flights)
importFrom(data.table,"%like%")
importFrom(data.table,":=")
importFrom(utils,globalVariables)
importFrom(utils,unzip)
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# flightsbr v0.3.0999 dev

* Bug fixes:
* Fixed bug when unzipping files for `read_flights()` function in Unix systems.
Closed #31.

# flightsbr v0.3.0


* Major changes:
* Function read_airfares() is temporarily unavailable. See issue [#30](https://github.com/ipeaGIT/flightsbr/issues/30)

Expand Down
5 changes: 3 additions & 2 deletions R/flightsbr.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
#' @aliases flightsbr-package
#'
#' @importFrom data.table := %like%
#' @importFrom utils globalVariables
#' @importFrom utils globalVariables unzip
"_PACKAGE"


## quiets concerns of R CMD check re: the .'s that appear in pipelines
utils::globalVariables( c('month',
'year',
'latitude',
'longitude') )
'longitude',
'temp_local_file') )

2 changes: 1 addition & 1 deletion R/read_aircrafts.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ read_aircrafts <- function( showProgress = TRUE ){
data.table::setDTthreads(percent = 100)

# download data
rab_dt <- try(silent=T,
rab_dt <- try(silent=TRUE,
data.table::fread(rab_url,
skip = 1,
encoding = 'UTF-8',
Expand Down
47 changes: 30 additions & 17 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ get_airfares_url <- function(dom, year, month) { # nocov start
#'
#' @param file_url String. A url passed from get_flights_url.
#' @param showProgress Logical, passed from \code{\link{read_flights}}
#' @param dest_file String, passed from \code{\link{read_flights}}
#'
#' @return Silently saves downloaded file to temp dir.
#'
Expand All @@ -370,36 +371,35 @@ get_airfares_url <- function(dom, year, month) { # nocov start
#' file_url <- get_flights_url(type='basica', year=2000, month=11)
#'
#' # download data
#' download_flightsbr_file(file_url=file_url, showProgress=TRUE)
#' download_flightsbr_file(file_url=file_url,
#' showProgress=TRUE,
#' dest_file = tempfile(fileext = ".zip")
#' )
#'}}
download_flightsbr_file <- function(file_url, showProgress=showProgress){

# create temp local file
file_name <- basename(file_url)
temp_local_file <- paste0(tempdir(),"/",file_name)
download_flightsbr_file <- function(file_url, showProgress=showProgress, dest_file = temp_local_file){

# download data
try(
httr::GET(url=file_url,
if(showProgress==T){ httr::progress()},
httr::write_disk(temp_local_file, overwrite = T),
httr::write_disk(dest_file, overwrite = T),
config = httr::config(ssl_verifypeer = FALSE)
), silent = TRUE)

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

# download data: try a 2nd time
try(
httr::GET(url=file_url,
if(showProgress==T){ httr::progress()},
httr::write_disk(temp_local_file, overwrite = T),
httr::write_disk(dest_file, overwrite = T),
config = httr::config(ssl_verifypeer = FALSE)
), silent = TRUE)
}

# Halt function if download failed
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {
if (!file.exists(dest_file) | file.info(dest_file)$size == 0) {
message('Internet connection not working.')
return(invisible(NULL)) }
}
Expand Down Expand Up @@ -432,18 +432,31 @@ download_flights_data <- function(file_url, showProgress=showProgress, select=se
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {

# download data
download_flightsbr_file(file_url=file_url, showProgress=showProgress)
download_flightsbr_file(file_url=file_url, showProgress=showProgress, dest_file = temp_local_file)
}

### 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)
# # 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', sep = ';')

# unzip file to tempdir
temp_local_dir <- tempdir()
utils::unzip(zipfile = temp_local_file, exdir = temp_local_dir)

# read zipped file stored locally
dt <- data.table::fread( cmd = temp_local_file_zip, select=select, colClasses = 'character', sep = ';')
# get file name
file_name <- utils::unzip(temp_local_file, list = TRUE)$Name

# read file stored locally
dt <- data.table::fread( paste0(temp_local_dir,'/', file_name),
select = select,
colClasses = 'character',
sep = ';')

# return to original threads
data.table::setDTthreads(orig_threads)
Expand Down Expand Up @@ -480,7 +493,7 @@ download_airfares_data <- function(file_url, showProgress=showProgress, select=s
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {

# download data
download_flightsbr_file(file_url=file_url, showProgress=showProgress)
download_flightsbr_file(file_url=file_url, showProgress=showProgress, dest_file = temp_local_file)
}

### set threads for fread
Expand Down Expand Up @@ -587,7 +600,7 @@ download_airport_movement_data <- function(file_url, showProgress=showProgress){
if (!file.exists(temp_local_file) | file.info(temp_local_file)$size == 0) {

# download data
download_flightsbr_file(file_url=file_url, showProgress=showProgress)
download_flightsbr_file(file_url=file_url, showProgress=showProgress, dest_file = temp_local_file)
}

### set threads for fread
Expand Down
13 changes: 11 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.

0 comments on commit b5d634c

Please sign in to comment.