Retrieving Data from a Custom Web API
The cumtd
R package provides an example of implementing and using a Web API. In particular, the package is a case study in retrieving data from the Champaign-Urbana Mass Transit District (‘CUMTD’) Web API, which provides realtime information for buses. Writing a custom wrapper to the API was necessary since there is not a realtime General Transit Feed Specification (GTFS) available via GTFS Realtime. Thus, for those wanting to know if their bus will arrive on time, one must look elsewhere.
As a follow-up, the package is used in r-shinydashboard
example to illustrate how a real-time API can be used with shinydashboard
.
Installation
To install the cumtd
package, use:
if(!requireNamespace("remotes")) install.packages("remotes")
remotes::install_github("coatless-r-n-d/r-web-api")
To access its contents, load it into R with:
Endpoints
We have support presently for:
Implemented Functions |
---|
classify_departure_status |
convert_response |
cumtd |
cumtd_check_key |
cumtd_set_key |
cumtd_show_key |
get_all_routes |
get_all_stops |
get_all_vehicles |
get_api_usage |
get_departures_by_stop |
get_route_shape |
get_route_shape_between_stops |
End points to be added (PRs appreciated!):
Implementation Details
When trying to import data into R from a Web API, the preferred approach is to use httr
or curl
. Using either of these packages, one can form HTTP requests to send to the Web API and receive HTTP responses from it that contain the data. Inside the response, there is a body portion that contains the desired data and needs to be converted into an R data structure.