-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathREADME.Rmd
More file actions
251 lines (175 loc) · 9 KB
/
Copy pathREADME.Rmd
File metadata and controls
251 lines (175 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
---
output: github_document
---
# nycOpenData <img src="man/figures/nycOpenData_hex.png" alt="nycOpenData logo" width="72" align="right" />
[](https://CRAN.R-project.org/package=nycOpenData)
[](https://r-pkg.org/pkg/nycOpenData)
[](https://lifecycle.r-lib.org/articles/stages.html)
[](https://www.repostatus.org/#active)
[](https://www.r-bloggers.com/2026/01/nycopendata-a-unified-r-interface-to-nyc-open-data-apis/)
[](https://rweekly.org/#RintheRealWorld)
[](https://github.com/ropensci/nycOpenData/actions/workflows/R-CMD-check.yaml)
`nycOpenData` provides a lightweight R interface to the
[NYC Open Data](https://opendata.cityofnewyork.us/) Socrata API.
The package allows users to search, filter, and download datasets from the
NYC Open Data Portal directly into R without manually constructing API queries,
handling JSON responses, or performing type conversion.
Designed primarily for students, educators, and researchers, `nycOpenData`
reduces the technical overhead required to begin working with civic datasets
while still exposing the underlying structure of the NYC Open Data ecosystem.
Version **0.2.3** introduces a streamlined, catalog-driven interface for NYC Open Data.
While users may still explore datasets through the NYC Open Data Portal itself,
`nycOpenData` streamlines the transition from discovery to reproducible analysis
within R workflows.
---
## How nycOpenData Works
The package wraps the NYC Open Data Portal's Socrata API.
Internally, `nycOpenData`:
- retrieves metadata from live NYC Open Data catalog tables
- constructs parameterized HTTP GET requests
- downloads JSON responses from Socrata endpoints
- converts results into tidy tibble outputs
- optionally performs automatic column type coercion
Automatic type coercion uses heuristic-based parsing to infer common column types from Socrata API responses.
Most workflows begin with `nyc_list_datasets()`, which retrieves a live catalog
of available datasets from NYC Open Data (`5tqd-u88y`).
Datasets can then be downloaded using either:
- a human-readable catalog `key` (recommended)
- the raw Socrata dataset UID (e.g. `"erm2-nwe9"`)
The catalog `key` is designed to be easier to remember and use in classroom settings,
while the Socrata UID is the stable identifier used internally by the NYC Open Data Portal.
The package provides three core functions:
- `nyc_list_datasets()` — Retrieve a live catalog of available NYC Open Data datasets, including dataset titles, human-readable keys, Socrata UIDs, endpoint URLs, and metadata used throughout the package.
- `nyc_pull_dataset()` — Download cataloged NYC Open Data datasets using either a human-readable key or dataset UID, with support for filtering, ordering, date ranges, automatic type coercion, and optional column name cleaning.
- `nyc_any_dataset()` — Pull data directly from arbitrary NYC Open Data Socrata JSON endpoints without requiring inclusion in the internal package catalog.
Datasets pulled via `nyc_pull_dataset()` automatically apply sensible defaults from the catalog (such as default ordering and date fields), while still allowing user control over:
- `limit`
- `filters`
- `date` / `from` / `to`
- `where`
- `order`
- `clean_names`
- `coerce_types`
Datasets can be referenced using either:
- a human-readable catalog `key` (recommended), or
- the underlying Socrata dataset UID (e.g. `"erm2-nwe9"`)
The catalog `key` system was designed to improve readability and usability in
classroom and reproducible research settings, where memorizing opaque Socrata
UIDs can create unnecessary friction for new users.
All functions return clean **tibble** outputs and support filtering via
`filters = list(field = "value")`.
Advanced users may optionally provide raw SoQL queries through the `where`
argument.
SoQL (Socrata Query Language) is the filtering and query syntax used by
Socrata-powered open data portals:
https://dev.socrata.com/docs/queries/
---
## Installation
### From **CRAN**
```r
install.packages("nycOpenData")
install.packages(
"nycOpenData",
repos = c(
"https://ropensci.r-universe.dev",
"https://cloud.r-project.org"
)
)
```
### Development version (GitHub)
```r
devtools::install_github("martinezc1/nycOpenData")
```
---
## Example
```{r message=FALSE, warning=FALSE}
library(nycOpenData)
# Browse available datasets
catalog <- nyc_list_datasets()
# Search for 311-related datasets
catalog |>
filter(grepl("311", name, ignore.case = TRUE)) |>
select(key, name)
# Pull recent 311 requests
requests <- nyc_pull_dataset(
dataset = "x311_service_requests_from_2020_to_present",
limit = 100
)
# Pull filtered data
brooklyn_nypd <- nyc_pull_dataset(
dataset = "x311_service_requests_from_2020_to_present",
limit = 100,
filters = list(
agency = "NYPD",
city = "BROOKLYN"
)
)
```
The `filters` argument accepts named lists and automatically generates
appropriate SoQL filtering statements.
For example:
```{r}
filters = list(
borough = c("BROOKLYN", "QUEENS")
)
```
This internally generates a SQL-style `IN` clause within the resulting SoQL query.
---
## Learn by example
- `vignette("nyc-311", package = "nycOpenData")` – Working with NYC 311 data end-to-end
---
## About
`nycOpenData` makes New York City's civic datasets accessible to students,
educators, analysts, and researchers through a unified and user-friendly R interface.
Developed to support reproducible research, open-data literacy, and real-world analysis.
---
## Development
`nycOpenData` uses cassette-based testing through the
`vcr` and `webmockr` packages to mock API responses during testing.
To run tests locally:
```r
devtools::test()
```
Recorded fixtures are stored in:
```r
tests/testthat/fixtures/
```
---
## Comparison to Other Software
While the [`RSocrata`](https://CRAN.R-project.org/package=RSocrata) package provides a general interface for any Socrata-backed portal, `nycOpenData` is specifically tailored for the New York City ecosystem.
- **Low-friction onboarding**: Designed so students can begin working with NYC civic datasets in minutes without needing to manually construct API requests.
- **Readable dataset identifiers**: Uses stable catalog-driven keys instead of requiring users to memorize Socrata dataset UIDs.
- **Pedagogical design**: Emphasizes transparency, reproducibility, and classroom-friendly workflows for teaching open data literacy.
- **Lightweight workflow**: Returns clean tibble outputs with minimal configuration and dependencies.
---
## Contributing
We welcome contributions! If you find a bug or would like to request a wrapper for a specific NYC dataset, please open an issue or submit a pull request on [GitHub](https://github.com/martinezc1/nycOpenData).
---
## Authors & Contributors
### Maintainer
**Christian A. Martinez** 📧 [c.martinez0@outlook.com](mailto:c.martinez0@outlook.com)
GitHub: [@martinezc1](https://github.com/martinezc1)
### ✨ Contributors
Special thanks to the students of **PSYC 7750G – Reproducible Psychological Research** at Brooklyn College (CUNY) who have contributed functions and documentation:
* **Crystal Adote** ([@crystalna20](https://github.com/crystalna20))
* **Jonah Dratfield** ([@jdratfield38](https://github.com/jdratfield38))
* **Joyce Escatel-Flores** ([@JoyceEscatel](https://github.com/JoyceEscatel))
* **Rob Hutto** ([@robhutto](https://github.com/robhutto))
* **Isley Jean-Pierre** ([@ijpier](https://github.com/ijpier))
* **Shannon Joyce** ([@shannonjoyce](https://github.com/shannonjoyce))
* **Laura Rose-Werner** ([@laurarosewerner](https://github.com/laurarosewerner))
* **Emma Tupone** ([@emmatup0205](https://github.com/emmatup0205))
* **Xinru Wang** ([@xrwangxr](https://github.com/xrwangxr))
### Acknowledgements
This package was accepted into the rOpenSci software ecosystem following open peer review. Many thanks to editor [@ronnyhdez](https://github.com/ronnyhdez) and reviewers [@donghl17](https://github.com/donghl17) and [@MichaelPascale](https://github.com/michaelpascale) for their thoughtful feedback, which substantially improved the package.
---
## Academic Context
This package is developed as a primary pedagogical tool for teaching data acquisition and open science practices at **Brooklyn College, City University of New York (CUNY)**.
---
## Maintenance
Because the package retrieves metadata dynamically from the live NYC Open Data
catalog, many newly published datasets can be accessed without requiring package updates.
---
## Disclaimer
`nycOpenData` is an independent project and is not affiliated with,
endorsed by, or maintained by the City of New York.