R/automation.R

library(httr)
library(jsonlite)
library(dplyr)
library(zoo)
library(remotes)
library(tidyverse)
library(DatawRappr)

#API query for hospitalisations

#' Extracts paginated data by requesting all of the pages
#' and combining the results.
#'
#' @param filters    API filters. See the API documentations for 
#'                   additional information.
#'                   
#' @param structure  Structure parameter. See the API documentations 
#'                   for additional information.
#'                   
#' @return list      Comprehensive list of dictionaries containing all 
#'                   the data for the given ``filter`` and ``structure`.`
get_paginated_data <- function (filters, structure) {
  
  endpoint     <- "https://api.coronavirus.data.gov.uk/v1/data"
  results      <- list()
  current_page <- 1
  
  repeat {
    
    httr::GET(
      url   = endpoint,
      query = list(
        filters   = paste(filters, collapse = ";"),
        structure = jsonlite::toJSON(structure, auto_unbox = TRUE),
        page      = current_page
      ),
      timeout(10)
    ) -> response
    
    # Handle errors:
    if ( response$status_code >= 400 ) {
      err_msg = httr::http_status(response)
      stop(err_msg)
    } else if ( response$status_code == 204 ) {
      break
    }
    
    # Convert response from binary to JSON:
    json_text <- content(response, "text")
    dt        <- jsonlite::fromJSON(json_text)
    results   <- rbind(results, dt$data)
    
    if ( is.null( dt$pagination$`next` ) ){
      break
    }
    
    current_page <- current_page + 1;
    
  }
  
  return(results)
  
}


# Create filters:
query_filters <- c(
  "areaType=nation",
  "areaName=england"
)

# Create the structure as a list or a list of lists:
query_structure <- list(
  date       = "date", 
  name       = "areaName",
  hospital_cases = "hospitalCases",
  hospital_admissions = "newAdmissions"
)

nhs_england <- get_paginated_data(query_filters, query_structure)

list(
  "Shape"                = dim(nhs_england),
  "Data (first 3 items)" = nhs_england[0:3, 0:-1]
) -> report

print(report)


england_average <- nhs_england %>%
  dplyr::mutate(seven_day_average = zoo::rollmean(hospital_admissions, k = 7, align="left", fill = NA)) %>%
  select(1,4,5)

write.csv(england_average, file= "data/nhs_england.csv")

datawrapper_auth("8yh0eD4SdYhm1szJKrSjrg6hWypl3Y1qQO4WxJEKrasu9nBn4xGvNwVso2dwSM3u")

id = "DlM2l"
httr::handle_reset(paste("https://api.datawrapper.de/v3/charts/", id))
dw_data_to_chart(admissions_last_month, chart_id = id)
dw_edit_chart(chart_id = id, 
              annotate = paste("Last updated on ",gsub(" 0"," ",format(Sys.Date(),"%b %d")), sep = ""))
dw_publish_chart(id)

hospital_cases <- nhs_england %>%
  select(1,3)

hospital_cases <- hospital_cases %>%
  dplyr::mutate(seven_day_average = zoo::rollmean(hospital_cases, k = 7, align="left", fill = NA))

id = "k9v33"
httr::handle_reset(paste("https://api.datawrapper.de/v3/charts/", id))
dw_data_to_chart(hospital_cases, chart_id = id)
dw_edit_chart(chart_id = id, 
              annotate = paste("Last updated on ",gsub(" 0"," ",format(Sys.Date(),"%b %d")), sep = ""))
dw_publish_chart(id)
GWilloughby99/covidauto documentation built on Jan. 28, 2022, 8:11 a.m.