## ----include = FALSE----------------------------------------------------------
# Examples run when a key is present (on the docs site), and are shown but not run
# otherwise (on CRAN, and locally without a key). The displayed client uses a
# placeholder; the real one is built here from an environment variable.
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = nzchar(Sys.getenv("CLOSECITY_KEY"))
)
library(closecity)
close <- closecity::close_client(api_key = Sys.getenv("CLOSECITY_KEY"))

## -----------------------------------------------------------------------------
# close$modes()

## -----------------------------------------------------------------------------
# amenity_types <- close$destination_types()
# supermarket_type <- amenity_types[amenity_types$label == "grocery_stores", ]$dest_type_id
# 
# providence_ri <- close$places(q = "Providence")[1, ]
# providence_ri[, c("name", "state", "geoid")]

## -----------------------------------------------------------------------------
# supermarkets <- close$place_pois(geoid = providence_ri$geoid, type = supermarket_type)
# city_boundary <- close$place_boundary(geoid = providence_ri$geoid)
# closecity::close_map(
#   x = supermarkets,
#   color = "#e8590c",
#   boundary = city_boundary,
#   label = "name"
# )

## -----------------------------------------------------------------------------
# walk_times <- close$block_summary(geoid = "440070008001068", mode = "walk")
# walk_times <- merge(
#   walk_times,
#   amenity_types[, c("dest_type_id", "name")],
#   by = "dest_type_id"
# )
# walk_times[order(walk_times$travel_time), c("name", "travel_time")]

## -----------------------------------------------------------------------------
# raw <- close$block_summary(geoid = "440070008001068", mode = "walk", output = "raw")
# str(raw$results, max.level = 2, list.len = 3)

## -----------------------------------------------------------------------------
# tryCatch(
#   close$block_summary(geoid = "000000000000000"),
#   close_api_error = function(e) message(sprintf("%s (%d)", e$slug, e$status))
# )

