onemapsgapi (version 1.1.0)

get_route: Get Route Information

Description

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.

Usage

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
)

Value

If no error occurs and status_info = TRUE:

status_info

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

result

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.

Arguments

token

User's API token. This can be retrieved using get_token

start

Vector of c(lat, lon) coordinates for the route start point

end

Vector of c(lat, lon) coordinates for the route end point

route

Type of route. Accepted values are walk, drive, pt (public transport), or cycle

date

Default = current date. Date for which route is requested.

time

Default = current time. Time for which route is requested.

mode

Required if route = "pt". Accepted values are transit, bus or rail

max_dist

Optional if route = "pt". Maximum walking distance

n_itineraries

Optional if route = "pt". Default = 3. The number of potential routes to provide.

status_info

Default = FALSE. Whether to return output as a list including a list of status information and a tibble of output

decode

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.

Examples

Run this code
# 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