Skip to content

Commit

Permalink
clean colnames with janitor
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Oct 9, 2024
1 parent f1c525d commit 4c378a7
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 225 deletions.
10 changes: 6 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# flightsbr dev v0.5.099999 dev


* Breaking changes:
* The names of all columns in the data outputs are now cleanned with {janitor}
* Function `read_airports()` now downloads v2 version of public airports data. Closes [#41](https://github.com/ipeaGIT/flightsbr/issues/41)

* Major changes:
* Function `read_airfares()` is working again. Closes [#30](https://github.com/ipeaGIT/flightsbr/issues/30)
* Function `read_airports()` now downloads v2 version of public airports data. Closes [#41](https://github.com/ipeaGIT/flightsbr/issues/41)

* Minor changes:
* Internally check of the consistency of date inputs. The date input must be consistent in either a 6-digit format `yyyymm` OR a 4-digit format `yyyy`.
* New support function `latest_airfares_date()`


Fix error that stopped reading aircraft data `read_aircrafts()` for multiple months when the number of collums differed across months. Fixed using `data.table::rbindlist(fill = TRUE)`
Fix error that stopped reading aircraft data `read_aircrafts()` for multiple months when the number of collums differed across months. Fixed using `data.table::rbindlist(fill = TRUE)`



Expand Down
9 changes: 8 additions & 1 deletion R/read_aircrafts.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ read_aircrafts <- function(date = 202001,
# convert columns to numeric
convert_to_numeric(dt)

return(dt)
# clean names
nnn <- names(dt)
data.table::setnames(
x = dt,
old = nnn,
new = janitor::make_clean_names(nnn)
)

return(dt)
}
8 changes: 8 additions & 0 deletions R/read_airfares.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ read_airfares <- function(date = 202001,
# convert columns to numeric
convert_to_numeric(dt)

# clean names
nnn <- names(dt)
data.table::setnames(
x = dt,
old = nnn,
new = janitor::make_clean_names(nnn)
)

return(dt)
}

8 changes: 8 additions & 0 deletions R/read_airport_movements.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ read_airport_movements <- function(date = 202001,
# convert columns to numeric
convert_to_numeric(dt)

# clean names
nnn <- names(dt)
data.table::setnames(
x = dt,
old = nnn,
new = janitor::make_clean_names(nnn)
)

return(dt)
}
10 changes: 9 additions & 1 deletion R/read_flights.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ read_flights <- function(date = 202001,
#### prep data

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

# convert columns to numeric
convert_to_numeric(dt)

# clean names
nnn <- names(dt)
data.table::setnames(
x = dt,
old = nnn,
new = janitor::make_clean_names(nnn)
)

return(dt)
}

2 changes: 1 addition & 1 deletion R/utils_aircrafts.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ download_aircrafts_data <- function(file_url = parent.frame()$file_url,
xdate <- gsub('-', '', xdate)
temp_x[, reference_date := xdate]
}) |>
data.table::rbindlist()
data.table::rbindlist(fill = TRUE)


# return to original threads
Expand Down
2 changes: 1 addition & 1 deletion R/utils_airport_movement.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ download_airport_movement_data <- function(file_url = parent.frame()$file_url,
colClasses = 'character',
sep = ';'
) |>
data.table::rbindlist()
data.table::rbindlist(fill = TRUE)



Expand Down
2 changes: 1 addition & 1 deletion man/read_airports.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test_read_aircrafts.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_that("read_aircrafts", {
testthat::expect_true(is(read_aircrafts(showProgress = TRUE), "data.table"))

# test columns are correct
testthat::expect_equal(names(read_aircrafts())[1], 'MARCA')
testthat::expect_equal(names(read_aircrafts())[1], 'marca')

# test date all months in a year
test2 <- read_aircrafts(date = 2020, showProgress = FALSE)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_read_airfares.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ test_that("read_airfares", {
testthat::expect_true(nrow(test4) >0 )

# check content
testthat::expect_equal( as.character(min(test3$ANO)), as.character("2004") )
testthat::expect_equal( as.character(min(test4$ANO)), as.character("2012") )
testthat::expect_equal( as.character(min(test3$ano)), as.character("2004") )
testthat::expect_equal( as.character(min(test4$ano)), as.character("2012") )

# all months in a year
test5 <- read_airfares(date=2022, select='Tarifa-N', showProgress = FALSE)
test5 <- read_airfares(date=2022, select='tarifa-n', showProgress = FALSE)
testthat::expect_true(is(test5, "data.table"))

test6 <- read_airfares(date=2022, domestic = FALSE, showProgress = FALSE, select='VALOR_TARIFA')
test6 <- read_airfares(date=2022, domestic = FALSE, showProgress = FALSE, select='valor_tarifa')
testthat::expect_true(is(test6, "data.table"))

# test vector of dates
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_read_airport_movements.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("read_airport_movements", {
testthat::expect_true(nrow(test1) >0 )

# check conteudo
testthat::expect_equal( as.character(min(test1$DT_PREVISTO)), as.character("2021-12-31") )
testthat::expect_equal( as.character(min(test1$dt_previsto)), as.character("2021-12-31") )

# all months in a year
test2 <- read_airport_movements(date=2022, showProgress = FALSE)
Expand Down
209 changes: 0 additions & 209 deletions tests_rafa/precodapassagem.R

This file was deleted.

3 changes: 2 additions & 1 deletion tests_rafa/test_rafa.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Sys.setenv(NOT_CRAN = "true")

# each function separately
t1 <- covr::function_coverage(fun=read_aircrafts, test_file("tests/testthat/test_read_aircrafts.R"))
t1 <- covr::function_coverage(fun=read_airfares, test_file("tests/testthat/test_read_airfares.R"))
t2 <- covr::function_coverage(fun=read_airports, test_file("tests/testthat/test_read_airports.R"))
t3 <- covr::function_coverage(fun=read_flights, test_file("tests/testthat/test_read_flights.R"))
t4 <- covr::function_coverage(fun=read_airport_movements, test_file("tests/testthat/test_read_airport_movements.R"))
Expand All @@ -122,7 +123,7 @@ t8

# the whole package
Sys.setenv(NOT_CRAN = "true")
cov <- covr::package_coverage(path = ".", type = "tests", clean = FALSE)
cov <- covr::package_coverage(path = ".", type = "tests", pre_clean = FALSE)
cov

rep <- covr::report()
Expand Down

0 comments on commit 4c378a7

Please sign in to comment.