knitr::opts_chunk$set( collapse = TRUE, cache = FALSE, comment = "#>", message = FALSE, error = FALSE, warning = FALSE, fig.path = "README/README-", fig.width=7.3, fig.height=5, out.width = '100%' ) knitr::opts_chunk$set(echo = TRUE) library(r511)
If you don't have a key, you can get one here:
https://511.org/developers/list/tokens/create
#Sys.setenv(APIKEY511="yourkeyhere")
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)
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)
Using trread, load BART data into R as a list of dataframes.
library(trread) bart_gtfs_data <- import_gtfs(bart_gtfs_url)
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.