NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "false")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN
)

Set your api key as an environmental variable

If you don't have a key, you can get one here:
https://511.org/developers/list/tokens/create

library(r511)
#Sys.setenv(APIKEY511="yourkeyhere")

Get MTC 511 Operator List

This function pulls a list of operator names, modes, and private codes. The latter we use to make requests for GTFS data.

operator_df <- get_511_metadata()
head(operator_df)

Get URL for GTFS Data

You can use the get_511_url() function to build a URL from which you can directly download GTFS data for an operator.

bart_code <- operator_df[operator_df$name=='Bay Area Rapid Transit',]$privatecode
bart_gtfs_url <- get_511_url(bart_code)

Import Data

Using trread, load BART data into R as a list of dataframes.

library(trread)
bart_gtfs_data <- import_gtfs(bart_gtfs_url)

Example: Summarise Stops Per Route

Summarise the number of stops per route on BART.

library(dplyr)
attach(bart_gtfs_data)

routes_df %>% inner_join(trips_df, by="route_id") %>%
  inner_join(stop_times_df, by="trip_id") %>% 
    inner_join(stops_df, by="stop_id") %>% 
      group_by(route_long_name) %>%
        summarise(stop_count=n_distinct(stop_id)) %>%
  arrange(desc(stop_count))


r-gtfs/r511 documentation built on May 17, 2019, 1:58 p.m.