This is an interesting question. Most of us arenāt familiar with R but it seems that the parameters are correct. I assume you entered your API key correctly?
I wonder if anyone else has any ideas? I think @Romain_Dalston might have some skills in this area? Any other data / R specialists who can assist with this syntax?
So the issue seems to be with the body of the request. Rather than passing the body as a JSON, you can generate the body (somewhat inelegantly) with paste0. This was a pain to get right with all the escaped and control characters, but this seems to work now:
I donāt know if the issue with passing the body as a JSON is an issue with the API or with httr, but whatever variations I tried, I couldnāt get it to work either.
@c.m.b.biggs There may be a more elegant way to do this, but the following code constructs a new data frame for the speed data (replacing your code starting at output ā content(request))
x <- content(request, as="text", encoding="UTF-8") |>
fromJSON(flatten=TRUE) %>%
as_tibble() %>%
readr::type_convert()
# create labels for the speed bins
# car_speed_hist_0to120plus: the estimated car speed distribution in 5 km/h bins from 0 to 120+ km/h (in percentage of the total 100%)
lower <- seq(0, 120, 5)
upper <- seq(5, 125, 5)
create_speed_bin <- function(x, y) {
paste0("speed_", x, "_to_", y)
}
speed_bins <- map2_chr(lower, upper, create_speed_bin)
#relabel the top category
speed_bins[length(speed_bins)] <- "speed_120+"
# construct new data frame for speed data
z <- x$report$car_speed_hist_0to120plus
names(z) <- x$report$date
speed_data <- cbind(speed_bins, as_tibble(z)) |>
pivot_longer(cols = where(is.numeric), names_to = "time", values_to = "pct") |>
mutate(time = ymd_hms(time),
speed_bins = fct_rev(speed_bins)) #reverse the order of speed bins
Hereās an example of plot for a single day: (edit: This isnāt Shiny code yet; youād have to wrap this in a renderPlot call and figure which pieces youād want to be interactive)
# example of a plot for a single day
speed_data |>
filter(date(time) == "2021-06-03") |>
ggplot(aes(time, pct, group = speed_bins, fill = speed_bins)) +
geom_col(position = "stack") +
scale_x_datetime()
Very nice chart. Has anybody some experience to display this kind of chart in a Wordpress based webpage? We plan to provide an interactive page on our Website, where users could select a time frame for a specific segment-id.