Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised USA for changed upstream format - partial fix for #430 #431

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ importFrom(lubridate,as_date)
importFrom(lubridate,dmy)
importFrom(lubridate,mdy)
importFrom(lubridate,month)
importFrom(lubridate,now)
importFrom(lubridate,year)
importFrom(lubridate,ymd)
importFrom(lubridate,ymd_hms)
Expand All @@ -104,6 +105,7 @@ importFrom(purrr,map_chr)
importFrom(purrr,map_lgl)
importFrom(purrr,reduce)
importFrom(purrr,safely)
importFrom(purrr,set_names)
importFrom(purrr,walk)
importFrom(purrr,walk2)
importFrom(readxl,cell_limits)
Expand Down
37 changes: 31 additions & 6 deletions R/USA.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ USA <- R6::R6Class("USA",
"2" = "fips"
),
#' @field level_data_urls List of named links to raw data that are level
#' specific.
#' specific. Note that for the USA, the upstream source (@nytimes) splits
#' the level 2 (county) data into separate CSV files for each year, so
#' a template is listed here and expanded in this class' `download` method
#' before the parent `download` is called.
#'
# nolint start
level_data_urls = list(
"1" = list("state" = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv"),
"2" = list("county" = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
"1" = list("state" = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us-states.csv"),
"2" = list("county" = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us-counties-%04d.csv")
),
# nolint end
#' @field source_data_cols existing columns within the raw data
Expand Down Expand Up @@ -80,6 +84,26 @@ USA <- R6::R6Class("USA",
)
},

#' @description Specific function for downloading USA
#' data which expands the list of `data_urls` before calling
#' the parent class method, only if level 2 is sought.
#' @importFrom lubridate year now
#' @importFrom purrr map set_names
download = function() {
if (self$level == 2) {
years <- seq(
from = 2020,
to = year(now()),
by = 1)
self$data_urls <- set_names(
map(years,
~ sprintf(self$data_urls[["county"]], .x)),
map(years,
~ sprintf("county-%04d", .x)))
}
super$download()
},

#' @description State Level Data Cleaning
#' @importFrom dplyr rename mutate select left_join
clean_level_1 = function() {
Expand All @@ -101,13 +125,14 @@ USA <- R6::R6Class("USA",
},

#' @description County Level Data Cleaning
#' @importFrom dplyr mutate rename left_join
#' @importFrom dplyr mutate rename left_join bind_rows
clean_level_2 = function() {
self$data$clean <- self$data$raw$county %>%
collated_county <- bind_rows(self$data$raw)
self$data$clean <- collated_county %>%
rename(
level_2_region = county,
level_1_region = state,
level_2_region_code = fips,
level_2_region_code = geoid,
cases_total = cases,
deaths_total = deaths
) %>%
Expand Down
Binary file modified data/all_country_data.rda
Binary file not shown.
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Nam
nhs
nhsregions
nr
nytimes
OpenDataZH
ORCID
org
Expand Down
19 changes: 17 additions & 2 deletions man/USA.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/custom_data/USA_level_1.rds
Binary file not shown.
Binary file modified tests/testthat/custom_data/USA_level_2.rds
Binary file not shown.