R/get_jhu_ts.R

Defines functions get_jhu_ts

Documented in get_jhu_ts

#' Read in cases data from JHU COVID-19 case respository
#' @param write If TRUE, writes output as csv to filepath. Defaults to FALSE
#' @param filepath Provide filename and path to write to.
#' @return Long format database of time cases, recoveries, deaths
#' @details Downloads time series of JHU Corona virus data.
#' Data source: https://github.com/CSSEGISandData/COVID-19. Code adapted
#' from: https://github.com/RamiKrispin/coronavirus. If write = TRUE and
#' fielpath = NULL, a covid19.csv will be written to the current directory
#' @importFrom magrittr `%>%`
#' @importFrom dplyr mutate select rename left_join
#' @importFrom tidyr pivot_longer
#' @importFrom rvest html_table
#' @examples
#' cases <- get_jhu_ts()
#' cases <- get_jhu_ts(write = TRUE)
#' @export
get_jhu_ts <- function(write = FALSE, filepath = NULL) {
  case_types <- c("Confirmed", "Deaths", "Recovered")
  path <- paste0("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/",
                 "master/csse_covid_19_data/csse_covid_19_time_series/",
                 "time_series_19-covid-", case_types, ".csv")

  cases <- lapply(1:3, function(x) {  # x <- path[2]
    dat <- readr::read_csv(file = path[x])  # read in
    dat_long <- dat %>%  # wide to long
      pivot_longer(cols = 5:ncol(.), names_to = "date",
                   values_to = case_types[x]) %>%
      mutate(date = lubridate::as_date(lubridate::mdy(date))) %>%
      rename(admin = `Province/State`, country = `Country/Region`,
             y = Lat, x = Long) %>%
      select(admin, country, x, y, date, !!case_types[x])
  }) %>% purrr::reduce(left_join) %>%
    rename(cases = Confirmed, deaths = Deaths, recovered = Recovered) %>%
    arrange(country, date)

  # write out
  if(write == TRUE) {
    readr::write_csv(cases,
                     path = ifelse(!is.null(filepath), filepath, "covid19.csv"))
  }
  return(cases)
}


#' Read in improved time series data from JHU for the US
#' @param write If TRUE, writes output as csv to filepath. Defaults to FALSE
#' @param filepath Provide filename and path to write to.
#' @return Long format database of time cases, recoveries, deaths
#' @details Downloads improved time series of JHU Corona virus data for the US,
#' which is at county-level
#' Data source: https://github.com/CSSEGISandData/COVID-19.
#' @importFrom magrittr `%>%`
#' @importFrom dplyr mutate select rename left_join rename_all
#' @importFrom tidyr pivot_longer
#' @importFrom tidyselect all_of
#' @importFrom rvest html_table
#' @examples
#' cases <- get_jhu_ts2()
#' cases <- get_jhu_ts2(write = TRUE)
#' @export
get_jhu_ts2 <- function(write = FALSE, filepath = NULL) {
  case_types <- c("confirmed", "deaths")
  path <- paste0("https://github.com/CSSEGISandData/COVID-19/raw/master/",
                 "csse_covid_19_data/csse_covid_19_time_series/",
                 "time_series_covid19_", case_types, "_US.csv")
  colpat <- "county|state|combined_key|x|y|date|population|confirmed|deaths"

  cases <- lapply(1:length(case_types), function(x) {  # x <- 2
    dat <- readr::read_csv(file = path[x]) %>% rename_all(tolower)
    datecols <- grep("/", colnames(dat))
    out <- dat %>%
      pivot_longer(cols = all_of(datecols), names_to = "date",
                   values_to = case_types[x]) %>%
      mutate(date = parse_date(date, format = "%m/%d/%y")) %>%
      rename(county = admin2, state = province_state, country = country_region,
             y = lat, x = long_) %>%
      select(grep(colpat, names(.)))
  }) %>% purrr::reduce(left_join) %>%
    rename(cases = confirmed) %>% arrange(county, date)

  # write out
  if(write == TRUE) {
    readr::write_csv(
      cases, path = ifelse(!is.null(filepath), filepath, "covid19_ts_us.csv"))
  }
  return(cases)
}
agroimpacts/covid19clark documentation built on Nov. 19, 2020, 7:22 p.m.