This function is a wrapper for the Route Service API. It returns the full route data in a tibble format, or a list of 2 tibbles with results and status information if desired.
get_route(
token,
start,
end,
route,
date = Sys.Date(),
time = format(Sys.time(), format = "%T"),
mode = NULL,
max_dist = NULL,
n_itineraries = 3,
status_info = FALSE,
decode = FALSE
)
If no error occurs and status_info = TRUE
:
A list containing information about the query status. If route = "pt"
, the output contains lists request_params
, debug_output
and elevation
. Else, the list contains the variables status
and status_msg
A tibble or sf dataframe containing the data retrieved from the query. This is the only output if status_info = FALSE
. Each row is an itinerary. Output dimensions vary between route = "pt"
and other routes
If an error occurs, the output will be NULL
, along with a warning message.
User's API token. This can be retrieved using get_token
Vector of c(lat, lon) coordinates for the route start point
Vector of c(lat, lon) coordinates for the route end point
Type of route. Accepted values are walk
, drive
, pt
(public transport), or cycle
Default = current date. Date for which route is requested.
Default = current time. Time for which route is requested.
Required if route = "pt"
. Accepted values are transit
, bus
or rail
Optional if route = "pt"
. Maximum walking distance
Optional if route = "pt"
. Default = 3. The number of potential routes to provide.
Default = FALSE
. Whether to return output as a list including a list of status information and a tibble of output
Default = FALSE
. If TRUE
, output will be a sf dataframe displaying route geometry (`route_geom`) instead of a tibble. Requires the `sf` and `googlePolylines` packages. Do note that the decoding of `route_geom` is a lossy conversion.
# returns output tibble
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "drive")
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "pt",
mode = "bus", max_dist = 300, n_itineraries = 2)
# returns output sf dataframe
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581),
"drive", decode = TRUE)
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "pt",
mode = "bus", max_dist = 300, n_itineraries = 2, decode = TRUE)
# returns list of status list and output tibble
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581),
"drive", status_info = TRUE)
# error: output is NULL, warning message shows status code
if (FALSE) get_route("invalid_token", c(1.319728, 103.8421), c(1.319728905, 103.8421581), "drive")
# error: output is NULL, warning message shows error message from request
if (FALSE) get_route(token, c(300, 300), c(400, 500), "cycle")
if (FALSE) get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "fly")
Run the code above in your browser using DataLab