|
| 1 | +## code to prepare `can_prov_cases` dataset goes here |
| 2 | + |
| 3 | +library(dplyr) |
| 4 | +library(epiprocess) |
| 5 | +library(readr) |
| 6 | +library(purrr) |
| 7 | +library(httr) |
| 8 | +library(jsonlite) |
| 9 | + |
| 10 | + |
| 11 | +# Look for a GitHub API token. |
| 12 | +# Returns an empty string "" if env variable not found. |
| 13 | +gh_token <- Sys.getenv("GITHUB_PAT") |
| 14 | +if (gh_token == "") { |
| 15 | + # Try again with the secondary name. |
| 16 | + gh_token <- Sys.getenv("GITHUB_TOKEN") |
| 17 | +} |
| 18 | +if (gh_token == "") { |
| 19 | + warning("Token is not set or is not able to be fetched from the environment.", |
| 20 | + " Proceeding without authentication, but the requests may be blocked", |
| 21 | + " due to GitHub API rate limits.") |
| 22 | +} |
| 23 | + |
| 24 | +# Construct a header to send with GET requests |
| 25 | +if (gh_token == "") { |
| 26 | + # Empty header |
| 27 | + auth_header <- httr::add_headers() |
| 28 | +} else { |
| 29 | + auth_header <- httr::add_headers(Authorization = paste("Bearer", gh_token)) |
| 30 | +} |
| 31 | + |
| 32 | +## Get list of new and modified files to download |
| 33 | +# The `path` field filters commits to only those that modifying the listed dir |
| 34 | +# From https://www.github.com/ccodwg/Covid19Canada |
| 35 | +BASE_URL <- "https://api.github.com/repos/ccodwg/Covid19Canada/commits?sha=%s&per_page=%s&path=timeseries_prov/cases_timeseries_prov.csv&until=%s&page=%s" |
| 36 | +ITEMS_PER_PAGE <- 100 |
| 37 | +BRANCH <- "master" |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# We want to fetch all commits made since Mar 13 2022 (version the original |
| 42 | +# dataset was created from). |
| 43 | +# |
| 44 | +# Timestamp should be in ISO 8601 format. See |
| 45 | +# https://docs.github.com/en/rest/reference/commits#list-commits--parameters for |
| 46 | +# details. |
| 47 | +since_date <- strftime("2022-03-13", "%Y-%m-%dT%H:%M:%SZ", tz = "UTC") |
| 48 | + |
| 49 | +page <- 0 |
| 50 | +commit_pages <- list() |
| 51 | + |
| 52 | +# Fetch list of commits from API, one page at a time. Each page contains up to |
| 53 | +# 100 commits. If a page contains 100 commits, assume that there are more |
| 54 | +# results and fetch the next page. |
| 55 | +while (page == 0 || nrow(commit_page) == 100) { |
| 56 | + page <- page + 1 |
| 57 | + # Construct the URL |
| 58 | + commits_url <- sprintf(BASE_URL, BRANCH, ITEMS_PER_PAGE, since_date, page) |
| 59 | + |
| 60 | + request <- GET(commits_url, auth_header) |
| 61 | + # Convert any HTTP errors to R errors automatically. |
| 62 | + stop_for_status(request) |
| 63 | + |
| 64 | + # Convert results from nested JSON/list to dataframe. If no results returned, |
| 65 | + # `commit_page` will be an empty list. |
| 66 | + commit_page <- content(request, as = "text") %>% |
| 67 | + fromJSON(simplifyDataFrame = TRUE, flatten = TRUE) %>% |
| 68 | + # Trim message down a bit. |
| 69 | + mutate(message = substr(commit.message, 1, 40)) %>% |
| 70 | + select(sha, url = commit.url, message) |
| 71 | + |
| 72 | + # No more results are being returned. |
| 73 | + if (identical(commit_page, list())) { |
| 74 | + break |
| 75 | + } |
| 76 | + |
| 77 | + commit_pages[[page]] <- commit_page |
| 78 | +} |
| 79 | + |
| 80 | +# Combine all requested pages of commits into one dataframe |
| 81 | +commit_pages <- bind_rows(commit_pages) |
| 82 | + |
| 83 | +# Missing value `%s` to be filled in with a commit sha or branch name. |
| 84 | +BASE_DATA_URL <- "https://raw.githubusercontent.com/ccodwg/Covid19Canada/%s/timeseries_prov/cases_timeseries_prov.csv" |
| 85 | + |
| 86 | +fc_time_values <- seq(as.Date("2021-02-01"), as.Date("2021-12-01"), |
| 87 | + by = "1 month") |
| 88 | +commit_pages <- mutate( |
| 89 | + commit_pages, |
| 90 | + data_url = sprintf(BASE_DATA_URL, sha), |
| 91 | + date = strsplit(message, " ") %>% map_chr(~ substr(.x[3], start=1, stop=10)) %>% as.Date() |
| 92 | +) %>% |
| 93 | + # select(data_url, date) %>% |
| 94 | + na.omit() %>% |
| 95 | + filter(date %in% fc_time_values) |
| 96 | + |
| 97 | +# From https://github.com/mountainMath/BCCovidSnippets/blob/main/data/prov_pop.csv |
| 98 | +ca_pop_url <- "https://raw.githubusercontent.com/mountainMath/BCCovidSnippets/main/data/prov_pop.csv" |
| 99 | +ca_pop <- read_csv( |
| 100 | + ca_pop_url, |
| 101 | + col_types = cols( |
| 102 | + Province = col_character(), |
| 103 | + shortProvince = col_character(), |
| 104 | + Population = col_integer() |
| 105 | + ) |
| 106 | +) %>% |
| 107 | + rename(province = Province, abbreviation = shortProvince, population = Population) |
| 108 | +abbrev_map <- setNames(ca_pop$province, ca_pop$abbreviation) |
| 109 | + |
| 110 | +# Read in data and convert to `epi_df`s. |
| 111 | +can_prov_cases <- purrr::map2(commit_pages$data_url, commit_pages$date, function(url, date) { |
| 112 | + raw <- readr::read_csv( |
| 113 | + url, |
| 114 | + col_types = cols( |
| 115 | + province = col_character(), |
| 116 | + date_report = col_character(), |
| 117 | + cases = col_double(), |
| 118 | + cumulative_cases = col_double() |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + # Raw data uses a mix of full names and abbreviations. Switch to using only full names. |
| 123 | + raw$province <- case_when( |
| 124 | + raw$province == "NWT" ~ abbrev_map["NT"], |
| 125 | + raw$province == "PEI" ~ abbrev_map["PE"], |
| 126 | + raw$province %in% ca_pop$province ~ raw$province, |
| 127 | + raw$province %in% ca_pop$abbreviation ~ abbrev_map[raw$province], |
| 128 | + # Mark everything else as missing. Only applies to "Repatriated" region. |
| 129 | + TRUE ~ NA |
| 130 | + ) |
| 131 | + |
| 132 | + result <- raw %>% |
| 133 | + mutate(time_value = lubridate::dmy(date_report)) %>% |
| 134 | + left_join(ca_pop, by="province") %>% |
| 135 | + filter(!is.na(province), time_value > "2020-04-01") %>% |
| 136 | + mutate(geo_value = province, |
| 137 | + case_rate = cases / population * 1e5) %>% |
| 138 | + select(geo_value, time_value, case_rate) %>% |
| 139 | + as_epi_df(geo_type = "province", as_of = date) |
| 140 | + |
| 141 | + return(result) |
| 142 | +}) |
| 143 | +names(can_prov_cases) <- commit_pages$date |
| 144 | +can_prov_cases <- can_prov_cases %>% bind_rows(.id = "version") %>% |
| 145 | + mutate(version = lubridate::ymd(version)) %>% |
| 146 | + arrange(version) |
| 147 | + |
| 148 | +usethis::use_data(can_prov_cases, overwrite = TRUE) |
0 commit comments