Skip to contents

The following code reproduces the data set that was submitted for TidyTuesday.

Install the package from GitHub:

pak::pak::pkg_install("nrennie/national-highways")

Load the package:

Choose sensors sites from webtris.nationalhighways.co.uk and store site name in vector. These sensors are on the A64 Eastbound from York to Scarborough.

sites_chosen <- c("30361466", "30361338", "30361451", "30361486")

Pull sensor site data, and find ID for chosen sites:

sites <- sites()
sites_id <- sites |> 
  tibble::as_tibble() |> 
  dplyr::filter(Description %in% sites_chosen) |> 
  dplyr::pull(Id)

Map over sites and extract daily report for each site for May 2021. Note that page_size is chosen to exceed 31 days, 4 intervals per hour over 24 hours a day.

sites_data <- purrr::map(
  .x = sites_id,
  .f = ~daily_report(
    site = .x,
    start_date = "01052021",
    end_date = "31052021",
    page_size = 3000
  )
)

Reformat the data into a single tibble:

A64_traffic <- dplyr::bind_rows(
  sites_data
) |> 
  dplyr::left_join(
    sites, by = c("SiteId" = "Id")
  ) |> 
  # Description duplicate of Site Name
  dplyr::select(
    -c(Description)
  ) |> 
  # Replace characters which cause problems saving to CSV
  dplyr::mutate(
    Name = stringr::str_replace_all(Name, ";", "-")
  )