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

rewrite get_regional_data so it uses Google when there is no country-specific class #406 #435

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export(test_cleaning)
export(test_download)
export(test_processing)
export(test_return)
export(try_regional_data)
importFrom(R6,R6Class)
importFrom(countrycode,countrycode)
importFrom(countrycode,countryname)
Expand Down
125 changes: 125 additions & 0 deletions R/try_regional_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#' Try for regional-level data
#'
#' @description Provides an interface to source specific classes which
seabbs marked this conversation as resolved.
Show resolved Hide resolved
#' support regional level data, and where these do not exist, seeks regional
#' level data from JHU or google. For simple use cases this allows downloading
#' clean, standardised, regional-level COVID-19 data sets. Internally this uses
#' the `DataClass()` parent class which allows documented downloading, cleaning,
#' and processing. Optionally all steps of data processing can be returned
#' along with the functions used for processing but by default just the
#' finalised processed data is returned. See the examples for some potential
#' use cases and the links to lower level functions for more details and
#' options.
#'
#' @param country A character string specifying the country to get data from.
#' Not case dependent. Name should be the English name. For a list of
#' options use `get_available_datasets()`.
#' @param include_level_2_regions `r lifecycle::badge("deprecated")` Boolean. If TRUE, returns data stratified by
#' level 2 regions. If FALSE, stratified by Level 1. Note that Level 2 region
#' data is not always available. In these cases the user will get a warning
#' and the Level 1 data will be returned.
#' @param localise_regions `r lifecycle::badge("deprecated")` Logical, defaults to TRUE. Should region names be localised.
#' @inheritParams return_data
#' @inheritParams initialise_dataclass
#' @return A tibble with data related to cases, deaths, hospitalisations,
#' recoveries and testing stratified by regions within the given country.
#' @importFrom lifecycle deprecated is_present deprecate_warn
#' @family interface
#' @seealso [Italy()], [UK()]
#' @export
#' @examples
#' \dontrun{
#' # set up a data cache
#' start_using_memoise()
#'
#' # download data for Italy
#' try_regional_data("italy")
#'
#' # return totals for Italy with no localisation
#' try_regional_data("italy", localise = FALSE, totals = TRUE)
#'
#' # download data for the UK but return the class
#' uk <- try_regional_data("United Kingdom", class = TRUE)
#' uk
#'
#' # return UK data from the class object]
#' uk$return()
#' }
try_regional_data <- function(country, level = "1", totals = FALSE,
localise = TRUE, steps = FALSE,
class = FALSE, verbose = TRUE, regions,
include_level_2_regions = deprecated(),
localise_regions = deprecated(),
...) {
if (is_present(include_level_2_regions)) {
deprecate_warn(
"0.9.0",
"covidregionaldata::try_regional_data(include_level_2_regions = )", "covidregionaldata::try_regional_data(level = )"
)
if (include_level_2_regions) {
level <- "1"
} else {
level <- "2"
}
}

if (is_present(localise_regions)) {
deprecate_warn(
"0.9.0",
"covidregionaldata::try_regional_data(localise_regions = )", "covidregionaldata::try_regional_data(localise = )"
)
localise <- localise_regions
}

# construct short hand options
title_country <- str_to_title(country)
seabbs marked this conversation as resolved.
Show resolved Hide resolved
nospace <- str_replace_all(title_country, " ", "")
targets <- c(
title_country, toupper(title_country), nospace, toupper(nospace)
)

# check we have data for desired class
datasets <- covidregionaldata::get_available_datasets("regional")
target_class <- bind_rows(
filter(datasets, map_lgl(.data$class, ~ any(str_detect(., targets)))),
filter(datasets, map_lgl(.data$origin, ~ any(str_detect(., targets))))
) %>%
distinct()

if (nrow(target_class) != 0) {
# check data availability and initiate country class if available
region_class <- initialise_dataclass(
class = country, level = level, regions = regions,
totals = totals, localise = localise,
verbose = verbose, steps = steps, get = TRUE,
type = "regional", ...
)

return(
return_data(region_class, class = class)
)
} else {
google_sources <- csv_reader(Google$public_fields$common_data_urls$index)
seabbs marked this conversation as resolved.
Show resolved Hide resolved

google_inputs <-
filter(google_sources, map_lgl(.data$country_name, ~ any(str_detect(., targets))))
if (nrow(google_inputs) != 0) {
message_verbose(verbose = verbose,
seabbs marked this conversation as resolved.
Show resolved Hide resolved
"No country-specific data class found. Getting data from Google for ",
country)
google_class <- initialise_dataclass(
class = "Google", level = sprintf("%d", as.integer(level)+1),
totals = totals, localise = TRUE,
verbose = verbose, steps = steps,
regions = country, get = TRUE,
type = "national", ...
)
return_data(google_class, class = class)
} else {
stop(
"No data available for ", country, ", including from Google.\n",
"See get_available_datasets(type = c(\"regional\")) for supported datasets."
seabbs marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
}
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ reset_cache <- function() {

#' Control data return
#'
#' @description Controls data return for `get_reigonal_data` and
#' @description Controls data return for `get_regional_data` and
#' `get_national_data`
#' @param obj A Class based on a `DataClass`
#' @param class Logical, defaults to FALSE. If TRUE returns the
Expand Down
3 changes: 2 additions & 1 deletion man/CountryDataClass.Rd

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

3 changes: 2 additions & 1 deletion man/DataClass.Rd

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

3 changes: 2 additions & 1 deletion man/get_available_datasets.Rd

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

3 changes: 2 additions & 1 deletion man/get_national_data.Rd

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

3 changes: 2 additions & 1 deletion man/get_regional_data.Rd

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

3 changes: 2 additions & 1 deletion man/initialise_dataclass.Rd

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

1 change: 0 additions & 1 deletion man/reexports.Rd

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

2 changes: 1 addition & 1 deletion man/return_data.Rd

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

105 changes: 105 additions & 0 deletions man/try_regional_data.Rd

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