View source: R/load_chicago_data.R
| chiload | R Documentation |
Returns the bundled sample by default, or fetches the full dataset from the
City of Chicago SODA API when full = TRUE. A complete fetch is
cached as Parquet under tempdir() for the session; to keep it
across sessions name a directory first, for example
options(rmoriedata.cache_dir = tools::R_user_dir("rmoriedata", "cache")),
and drop it with chiclr(). Nothing is written
outside tempdir() unless you set that option. The result can be
returned as a base data frame, a
tibble, or written to a Parquet file whose path is returned – the last being
the recommended bridge for Python (pandas.read_parquet).
chiload(
type = c("arrests", "complaints"),
as = c("data.frame", "tibble", "parquet_path"),
full = FALSE,
mirror = getOption("rmoriedata.mirror", NULL),
limit = NULL,
fraction = NULL,
refresh = FALSE
)
type |
One of |
as |
Return format: |
full |
If |
mirror |
Optional base URL of an r-universe/drat mirror to try before
Socrata (offline-friendly fallback). Defaults to
|
limit |
Optional row cap. For a |
fraction |
Optional share of the dataset, in |
refresh |
If |
Parquet I/O uses this package's own native codec (R/aaa_parquet.R), so no arrow install is required.
A data.frame/tibble, or a length-1 character Parquet
path when as = "parquet_path".
# `type` selects the dataset; the bundled sample is returned by default.
comp <- chiload("complaints") # reported incidents
arr <- chiload("arrests") # arrests
nrow(comp)
nrow(arr)
head(sort(table(comp$primary_type), decreasing = TRUE), 5)
# `as = "tibble"` returns a tibble when the package is installed.
if (requireNamespace("tibble", quietly = TRUE)) {
tb <- chiload("complaints", as = "tibble")
class(tb)
}
# `as = "parquet_path"` writes a Parquet file and returns its path --
# the recommended bridge to Python (pandas.read_parquet). Offline: the
# bundled sample is written, no network.
pq <- chiload("arrests", as = "parquet_path")
file.exists(pq)
# `full = TRUE` fetches from the live Chicago SODA API; `limit` bounds
# the request (seconds, not minutes) and try() keeps the example
# graceful when the service is unreachable. Omit `limit` for the
# complete multi-million-row dataset (cached, see Description); `mirror`
# tries an offline-friendly Parquet mirror first when set.
# `fraction = 0.001` takes a share of the dataset (0.1% of all rows)
# instead of a row count; the live total is looked up first.
big <- try(chiload("complaints", full = TRUE, limit = 200))
if (!inherits(big, "try-error")) nrow(big)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.