## ----include= FALSE----------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup, message= FALSE---------------------------------------------------- library(gtfsrealtime) library(dplyr) library(stringr) ## ----read-alerts-------------------------------------------------------------- alerts <- read_gtfsrt_alerts( system.file("nyc-service-alerts.pb.bz2", package = "gtfsrealtime"), "America/New_York" ) ## ----glimpse-alerts----------------------------------------------------------- glimpse(alerts) ## ----inspect-alert------------------------------------------------------------ alerts |> filter(id == first(id)) |> select(id, route_id, stop_id, header_text, description_text) ## ----summarize-alert---------------------------------------------------------- # make sure trip_route_id and route_id always agree if both are specified stopifnot(with(alerts, all(is.na(route_id) | is.na(trip_route_id) | route_id == trip_route_id))) # make sure every update has a route id stopifnot(with(alerts, all(!is.na(route_id) | !is.na(trip_route_id)))) alerts |> mutate(route_id = coalesce(route_id, trip_route_id)) |> group_by(id) |> slice_head(n=1) |> ungroup() |> count(route_id, sort = TRUE) |> head(10) ## ----alert-text--------------------------------------------------------------- alerts |> distinct(id, header_text, description_text) |> mutate( header_text = str_trunc(header_text, 80), description_text = str_trunc(description_text, 120) ) |> head()