Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ Type: Package
Package: flightsbr
Title: Download Flight and Airport Data from Brazil
Version: 1.1.10999
Authors@R:
c(person(given="Rafael H. M.", family="Pereira",
email="rafa.pereira.br@gmail.com",
role=c("aut", "cre"),
Authors@R:
c(person(given="Rafael H. M.", family="Pereira",
email="rafa.pereira.br@gmail.com",
role=c("aut", "cre"),
comment = c(ORCID = "0000-0003-2125-7465")),
person(given = "Ipea - Institute for Applied Economic Research",
role = c("cph", "fnd")))
Description: Download flight and airport data from Brazil’s Civil Aviation Agency
(ANAC) <https://www.gov.br/anac/pt-br>. The data covers detailed
information on aircraft, airports, and airport operations registered
with ANAC. It also includes data on airfares, all international
(ANAC) <https://www.gov.br/anac/pt-br>. The data covers detailed
information on aircraft, airports, and airport operations registered
with ANAC. It also includes data on airfares, all international
flights to and from Brazil, and domestic flights within the country.
License: MIT + file LICENSE
URL: https://github.com/ipeaGIT/flightsbr
BugReports: https://github.com/ipeaGIT/flightsbr/issues
Depends:
Depends:
R (>= 2.10)
Imports:
archive,
curl (>= 5.0.0),
data.table (>= 1.14.0),
fs,
httr2,
lifecycle,
parzer,
pbapply,
janitor,
rvest
Suggests:
Suggests:
dplyr,
ggplot2 (>= 3.3.1),
rmarkdown (>= 2.6),
knitr,
testthat
VignetteBuilder:
VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
78 changes: 45 additions & 33 deletions R/read_flights.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#' description of all variables included in the data is available at \url{https://www.gov.br/anac/pt-br/assuntos/regulados/empresas-aereas/Instrucoes-para-a-elaboracao-e-apresentacao-das-demonstracoes-contabeis/descricao-de-variaveis}.
#'
#' @param date Numeric. Date of the data in the format `yyyymm`. Defaults to
#' `202001`. To download the data for all months in a year, the user
#' can pass a 4-digit year input `yyyy`. The parameter also accepts
#' a vector of dates such as `c(202001, 202006, 202012)`.
#' the latest available month. To download the data for all months
#' in a year, the user can pass a 4-digit year input `yyyy`. The
#' parameter also accepts a vector of dates such as
#' `c(202001, 202006, 202012)`.
#' @param type String. Whether the data set should be of the type `basica`
#' (flight stage, the default) or `combinada` (On flight origin and
#' destination - OFOD).
Expand All @@ -29,46 +30,58 @@
#'
#' f2015 <- read_flights(date = 2015)
#'}}
read_flights <- function(date = NULL,
type = 'basica',
showProgress = TRUE,
select = NULL,
cache = TRUE
){

### check inputs
if( ! type %in% c('basica', 'combinada') ){ stop(paste0("Argument 'type' must be either 'basica' or 'combinada'")) }
if( ! is.logical(showProgress) ){ stop(paste0("Argument 'showProgress' must be either 'TRUE' or 'FALSE.")) }
if( ! is.logical(cache) ){ stop(paste0("Argument 'cache' must be either 'TRUE' or 'FALSE.")) }
read_flights <- function(
date = NULL,
type = c("basica", "combinada"),
showProgress = TRUE,
select = NULL,
cache = TRUE
) {
### check inputs
requested_type <- match.arg(type)
if (!is.logical(showProgress)) {
stop(paste0("Argument 'showProgress' must be either 'TRUE' or 'FALSE."))
}
if (!is.logical(cache)) {
stop(paste0("Argument 'cache' must be either 'TRUE' or 'FALSE."))
}
check_input_date_format(date)

### check date input
# get all dates available
all_dates <- get_flight_dates_available()
### get files available
files <- get_flights_files_available()

# check if download failed
if (is.null(all_dates)) { return(invisible(NULL)) }
if (is.null(files)) {
return(invisible(NULL))
}

# check dates
if (is.null(date)) { date <- max(all_dates) }
check_date(date=date, all_dates)
# subset files: only required type
files <- files[type == requested_type]
all_dates <- files$date

### check date input
if (is.null(date)) {
date <- max(all_dates)
}
check_date(date = date, all_dates)

#### Download and read data
# expand years to months
if (all(nchar(date) == 4L)) {
date <- generate_all_months(date)
}

# prepare url of online files
file_url <- get_flights_url(type=type, date=date)
# subset files: only required dates
requested_dates <- date
files <- files[date %in% requested_dates]

# download and read data
dt_list <- download_flights_data(file_url,
showProgress,
select,
cache)
#### Download and read data
dt_list <- download_flights_data(files$url, showProgress, select, cache)

# check if download failed
if (is.null(dt)) { return(invisible(NULL)) }
if (is.null(dt_list)) {
return(invisible(NULL))
}

#### prep data
#### prep data

# row bind data tables
dt <- data.table::rbindlist(dt_list, fill = TRUE)
Expand All @@ -86,4 +99,3 @@ read_flights <- function(date = NULL,

return(dt)
}

Loading
Loading