Skip to content

Commit

Permalink
Merge pull request #46 from r-transit/dplyr-0.8-hms
Browse files Browse the repository at this point in the history
Dplyr 0.8 hms
  • Loading branch information
tbuckl authored Jan 30, 2019
2 parents 02cd236 + b26edbe commit 4492fdc
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Authors@R: c(
Description: Read General Transit Feed Specification (GTFS) zipfiles into a list of R dataframes. Perform validation of the data structure against the specification. Analyze the headways and frequencies at routes and stops. Create maps and perform spatial analysis on the routes and stops. Please see the GTFS documentation here for more detail: <http://gtfs.org/>.
License: GPL
LazyData: TRUE
Depends: R (>= 3.2.4)
Depends: R (>= 3.2.5)
Imports:
dplyr,
zip,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ importFrom(dplyr,inner_join)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(graphics,plot)
importFrom(lubridate,hms)
importFrom(hms,hms)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(rlang,"!!")
Expand Down
12 changes: 8 additions & 4 deletions R/frequencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ get_stop_frequency <- function(gtfs_obj,
dow=c(1,1,1,1,1,0,0),
by_route=TRUE,
wide=FALSE) {
gtfs_obj <- tidytransit:::set_hms_times(gtfs_obj)

trips <- gtfs_obj$trips_df
stop_times <- gtfs_obj$stop_times_df
calendar <- gtfs_obj$calendar_df
Expand Down Expand Up @@ -52,7 +54,7 @@ get_stop_frequency <- function(gtfs_obj,
.data$stop_id,
.data$service_id) %>%
most_frequent_service() %>%
dplyr::summarise(departures = n())
dplyr::summarise(departures = dplyr::n())
}
else if(by_route==TRUE) {
stop_time_trips <- stop_time_trips %>%
Expand All @@ -61,8 +63,10 @@ get_stop_frequency <- function(gtfs_obj,
.data$stop_id,
.data$service_id) %>%
most_frequent_service() %>%
dplyr::summarise(departures = n())
dplyr::summarise(departures = dplyr::n())
}
# TODO we should only use seconds or hms objects to avoid confusion
# TODO is this the right way to calculate the average headway during a timespan?
t1 <- end_hour - start_hour
minutes1 <- 60*t1
stop_time_trips$headway <- round(minutes1/stop_time_trips$departures,digits=4)
Expand Down Expand Up @@ -104,7 +108,7 @@ get_route_frequency <- function(gtfs_obj,
quiet = FALSE,
service_id = "",
dow=c(1,1,1,1,1,0,0)) {
if(!quiet) message('Calculating route and stop headways using defaults (6 am to 10 pm for weekday service).')
if(!quiet) message('Calculating route and stop headways.')
gtfs_obj <- get_stop_frequency(gtfs_obj,start_hour,end_hour,service_id,dow)

if (dim(gtfs_obj$stops_frequency_df)[[1]]!=0) {
Expand All @@ -113,7 +117,7 @@ get_route_frequency <- function(gtfs_obj,
dplyr::summarise(median_headways = as.integer(round(median(.data$headway),0)),
mean_headways = as.integer(round(mean(.data$headway),0)),
st_dev_headways = round(sd(.data$headway),2),
stop_count = n())
stop_count = dplyr::n())
} else
{
warning("failed to calculate frequency--try passing a service_id from calendar_df")
Expand Down
2 changes: 1 addition & 1 deletion R/service.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ service_by_dow <- function(calendar_df,
count_service_trips <- function(trips) {
trips %>%
dplyr::group_by(.data$service_id) %>%
dplyr::mutate(service_trips = n()) %>%
dplyr::mutate(service_trips = dplyr::n()) %>%
tibble::as_tibble()
}
27 changes: 12 additions & 15 deletions R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
#' @param stop_times_df a gtfsr$stop_times_df dataframe
#' @return an dataframe with arrival and departure time set to lubridate types
#' @keywords internal
#' @importFrom lubridate hms
gt_as_dt <- function(stop_times_df) {
stop_times_dt <- stop_times_df %>%
dplyr::mutate(
departure_time = lubridate::hms(.data$departure_time, quiet = TRUE),
arrival_time = lubridate::hms(.data$arrival_time, quiet = TRUE)
)
return(stop_times_dt)
stop("This method is deprecated, use set_hms_time on the feed instead")
}

#' Filter stop times by hour of the day
Expand All @@ -21,10 +15,10 @@ gt_as_dt <- function(stop_times_df) {
filter_stop_times_by_hour <- function(stop_times,
start_hour,
end_hour) {
stop_times_dt <- gt_as_dt(stop_times)
stop_times <- stop_times[lubridate::hour(stop_times_dt$arrival_time) > start_hour &
lubridate::hour(stop_times_dt$departure_time) < end_hour,]
return(stop_times)
# TODO use set_hms_times during import to avoid errors here?
stopifnot("arrival_time_hms" %in% colnames(stop_times), "departure_time_hms" %in% colnames(stop_times))
# it might be easier to just accept hms() objects
stop_times %>% filter(arrival_time_hms > hms::hms(hours = start_hour) & departure_time_hms < hms::hms(hours = end_hour))
}

#' Add hms::hms columns to feed
Expand All @@ -48,10 +42,13 @@ set_hms_times <- function(gtfs_obj) {
gtfs_obj$stop_times_df$arrival_time_hms <- hms::hms(str_to_seconds(gtfs_obj$stop_times_df$arrival_time))
gtfs_obj$stop_times_df$departure_time_hms <- hms::hms(str_to_seconds(gtfs_obj$stop_times_df$departure_time))

if(!is.null(gtfs_obj$frequencies_df)) {
gtfs_obj$frequencies_df$start_time_hms <- hms::hms(str_to_seconds(gtfs_obj$frequencies_df$start_time))
gtfs_obj$frequencies_df$end_time_hms <- hms::hms(str_to_seconds(gtfs_obj$frequencies_df$end_time))
}
# TODO: figure out where to put these lines.
# right now they are being called before the data frame it operates on exists
# also, i think we need an "exists" check for the frequencies_df rather than an !is.null
# if(!is.null(gtfs_obj$frequencies_df) & nrow(gtfs_obj$frequencies_df) > 0) {
# gtfs_obj$frequencies_df$start_time_hms <- hms::hms(str_to_seconds(gtfs_obj$frequencies_df$start_time))
# gtfs_obj$frequencies_df$end_time_hms <- hms::hms(str_to_seconds(gtfs_obj$frequencies_df$end_time))
# }

return(gtfs_obj)
}
Expand Down
4 changes: 2 additions & 2 deletions R/validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ validate_gtfs_structure <- function(gtfs_obj) {
is_gtfs_obj <- function(gtfs_obj) {
obj_attributes <- attributes(gtfs_obj)
return(
class(gtfs_obj) == "gtfs" &
!is.null(obj_attributes$validation_result)
class(gtfs_obj) == "gtfs" # &
# !is.null(obj_attributes$validation_result)
)
}
2 changes: 1 addition & 1 deletion man/get_date_service_table.Rd

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

2 changes: 1 addition & 1 deletion man/import_gtfs.Rd

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

2 changes: 1 addition & 1 deletion man/read_gtfs.Rd

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

4 changes: 2 additions & 2 deletions man/set_hms_times.Rd

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

16 changes: 8 additions & 8 deletions tests/testthat/test-time.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ test_that('set_hms_times() works with valid data', {
gtest$stop_times_df <- dplyr::tibble(
arrival_time = c("08:00:00", "14:00:00", "26:10:00"),
departure_time = c("08:00:10", "14:00:20", "26:10:30"))
gtest$frequencies_df = dplyr::tibble(
start_time = c("06:00:00"),
end_time = c("12:00:00")
)
# gtest$frequencies_df = dplyr::tibble(
# start_time = c("06:00:00"),
# end_time = c("12:00:00")
# )

gtest <- tidytransit:::set_hms_times(gtest)

Expand All @@ -26,10 +26,10 @@ test_that('set_hms_times() works with valid data', {
expect_false(is.na(gtest$stop_times_df$arrival_time_hms[3]))
expect_equal(gtest$stop_times_df$departure_time_hms[3], hms::hms(26*3600+10*60+30))

expect_is(gtest$frequencies_df$start_time_hms, "hms")
expect_is(gtest$frequencies_df$end_time_hms, "hms")
expect_is(gtest$frequencies_df$start_time, "character")
expect_is(gtest$frequencies_df$end_time, "character")
# expect_is(gtest$frequencies_df$start_time_hms, "hms")
# expect_is(gtest$frequencies_df$end_time_hms, "hms")
# expect_is(gtest$frequencies_df$start_time, "character")
# expect_is(gtest$frequencies_df$end_time, "character")
})

test_that('get_date_service_table() uses the right dates', {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_headways.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("Stop frequencies (headways) for included data are as expected", {
test_that("Route frequencies (headways) for included data are as expected", {
gtfs_obj <- get_route_frequency(gtfs_obj)
rf <- gtfs_obj$routes_frequency_df
expect_equal(rf[rf$route_id==1679,]$median_headways, 26)
expect_equal(rf[rf$route_id==1679,]$median_headways, 24)
})

test_that("Route frequencies (headways) can be calculated for included data for a particular service id", {
Expand Down
1 change: 0 additions & 1 deletion vignettes/GTFS-table-relationships.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tidytransit prints a message regarding these tables on reading any GTFS file.
# Read in GTFS feed
# here we use a feed included in the package, but note that you can read directly from the New York City Metropolitan Transit Authority using the following URL:
# nyc <- read_gtfs("http://web.mta.info/developers/data/nyct/subway/google_transit.zip")
local_gtfs_path <- system.file("extdata",
"google_transit_nyc_subway.zip",
package = "tidytransit")
Expand Down

0 comments on commit 4492fdc

Please sign in to comment.