I am trying to extract data from Philadelphia’s public transit API using the httr2 package.
I load the packages and call the API in the code below
library(tidyverse)
library(httr2)
septaURL <- 'https://www3.septa.org/api/TransitViewAll'
response <- request(septaURL) %>%
req_perform()
It returns a bunch of nested lists. I use the pluck function to drill down to a list of 128 items (the number of bus routes), which are lists of varying length (depending on how many buses are driving that route), which themselves are lists of 19 items (the data I want to retrieve).
response %>%
resp_body_json() %>%
pluck('routes', 1) %>%
glimpse()
However when I pipe in the map function to extract the data, I get a tibble of 0 columns, 0 rows.
response %>%
resp_body_json() %>%
pluck('routes', 1) %>%
#map_dfr(
(x) {
tibble(
Route = x %>% pluck('route_id'),
Trip = x %>% pluck('trip'),
Late = x %>% pluck('late'),
seatsAvailable = x %>% pluck('estimated_seat_availability')
)
}
) %>%
glimpse()
If I add an additional argument to the pluck function, it gives me the data I want for a single route in a tibble. But I want the data from all 128 routes.
response %>%
resp_body_json() %>%
pluck('routes', 1, 1) %>% #Added an argument here
#map_dfr(
(x) {
tibble(
Route = x %>% pluck('route_id'),
Trip = x %>% pluck('trip'),
Late = x %>% pluck('late'),
seatsAvailable = x %>% pluck('estimated_seat_availability')
)
}
) %>%
glimpse()
How do I get a tibble like the one produced above but for all routes?
featherz_mcgraw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.