R/data_incoming.R

Defines functions get_ecdc

#' Get ECDC data for a date
#'
#' @param date ISO Date of reports
#'
#' @export
get_ecdc <- function(date = Sys.Date()) {

  date <- as.Date(date, "%Y-%m-%d")
  if (is.na(date)) {
    stop("Date must be provided in ISO format (i.e., YYYY-MM-DD)")
  }

  fmt <- "https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-%s.xlsx"
  date_iso <- as.character(date)
  url <- sprintf(fmt, date_iso)
  tf <- tempfile(fileext = ".xlsx")

  url_page <- "https://www.ecdc.europa.eu/en/publications-data/download-todays-data-geographic-distribution-covid-19-cases-worldwide"
  tryCatch({
    code <- download.file(url, tf, mode = "wb")
    if (code != 0) {
      stop("Error downloading file")
    }
  },
  error = function(e) {
    stop(sprintf("Error downloading file '%s': %s, please check %s",
                 url, e$message, url_page))
  })

  d <- readxl::read_excel(tf, progress = FALSE)
  names(d)[1] <- "dateRep"

  names(d)[names(d) %in% c("Countries and territories", "countriesAndTerritories")] <- "Region"

  # sort out the date malarkey that happened on the 16th May
  if(is.na(as.Date(d$dateRep[1], "%Y-%m-%d"))) {
    d$dateRep <- as.Date(d$dateRep, format = "%d/%m/%Y")
  }

  # remove imported early death in Philippines
  d[which(d$countryterritoryCode=="PHL" & as.Date(d$dateRep) == as.Date("2020-02-02")),]$deaths <- 0

  # remove negative deaths
  d$deaths[which(d$deaths < 0)] <- 0
  d$deaths[which(is.na(d$deaths))] <- 0

  return(d)

}
mrc-ide/global-lmic-meffs documentation built on July 24, 2020, 12:30 a.m.