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

Modify get available datasets #382

Merged
merged 6 commits into from
Jun 16, 2021
Merged
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export(get_regional_data)
export(initialise_dataclass)
export(make_github_workflow)
export(make_new_data_source)
export(render_available_datasets)
export(reset_cache)
export(start_using_memoise)
export(stop_using_memoise)
Expand Down
7 changes: 7 additions & 0 deletions R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@
#' @description The region codes for JHU
#' @return A tibble of region codes and related information.
"JHU_codes"

#' Table of available datasets along with level and other information.
#' Rendered from the individual R6 class objects included in this package.
#'
#' @description Available datasets
#' @return A tibble of available datasets and related information.
"all_country_data"
joseph-palmer marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 31 additions & 15 deletions R/get_available_datasets.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#' Get supported data sets
#' Fetch table of supported data sets
#'
#' @description The function searches the environment for R6 class objects and
#' extracts the summary information from the available classes using
#' their `summary` methods. In practice this means that it can be used
#' to indicate supported data sets.
#' @description The function returns data on what countries are available from
joseph-palmer marked this conversation as resolved.
Show resolved Hide resolved
#' the data provided with this package. This is rendered through the function
#' `render_available_datasets()`.
#' @param type A character vector indicating the types of data to
#' return. Current options include "national" (which are datasets at the
#' national level which inherit from `CountryDataClass`) and
Expand All @@ -26,6 +25,33 @@
#' # see only regional level datasets
#' get_available_datasets("regional")
get_available_datasets <- function(type) {
seabbs marked this conversation as resolved.
Show resolved Hide resolved
if (!missing(type)) {
target_type <- match.arg(
type,
choices = c("national", "regional"),
several.ok = TRUE
)
all_country_data <- all_country_data %>%
filter(type %in% target_type)
}
return(all_country_data)
}

#' Make table of supported data sets and store as package data
#'
#' @description The function searches the environment for R6 class objects and
#' extracts the summary information from the available classes using
#' their `summary` methods. In practice this means that it can be used
#' to indicate supported data sets.
#' @family interface
#' @importFrom rlang .data
#' @importFrom dplyr select bind_rows filter
#' @importFrom tibble as_tibble
#' @export
#' @examples
#' # see all available datasets
#' render_available_datasets()
render_available_datasets <- function() {
envi <- ls(getNamespace("covidregionaldata"), all.names = TRUE)
# regional data
starts_with_capitals_idx <- grep("^[A-Z]", envi)
Expand All @@ -44,15 +70,5 @@ get_available_datasets <- function(type) {
)
available_country_data <- valid_country_objects %>%
bind_rows()

if (!missing(type)) {
target_type <- match.arg(
type,
choices = c("national", "regional"),
several.ok = TRUE
)
available_country_data <- available_country_data %>%
filter(type %in% target_type)
}
return(available_country_data)
}
21 changes: 21 additions & 0 deletions data-raw/render_available_datasets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# render available datasets table

envi <- ls(getNamespace("covidregionaldata"), all.names = TRUE)
joseph-palmer marked this conversation as resolved.
Show resolved Hide resolved
# regional data
starts_with_capitals_idx <- grep("^[A-Z]", envi)
starts_with_capitals <- envi[starts_with_capitals_idx]
exclude <- c("DataClass", "CountryDataClass")
valid_country_objects <- lapply(
starts_with_capitals,
function(x) {
country_obj <- get(x)
if (class(country_obj) == "R6ClassGenerator" & !(x %in% c(exclude))) {
dat <- get(x)$new()
dat <- dat$summary()
return(dat)
}
}
)
all_country_data <- valid_country_objects %>%
bind_rows()
usethis::use_data(all_country_data, overwrite = TRUE)
Binary file added data/all_country_data.rda
Binary file not shown.
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.

20 changes: 20 additions & 0 deletions man/all_country_data.Rd

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

12 changes: 6 additions & 6 deletions 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.

28 changes: 28 additions & 0 deletions man/render_available_datasets.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/test-get_available_datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ test_that("National level datasets can be filtered for", {
expect_equal(nrow(dplyr::filter(nat, class %in% "Italy")), 0)
expect_equal(unique(nat$type), "national")
})

get_available_datasets_data <- get_available_datasets()
render_available_datasets_data <- render_available_datasets()
test_that(
paste(
"all_country_data is up to date.",
"This will fail if a new data source has been added, or an existing one",
"modified, but the table outlining available data (all_country_data) has",
"not been re-rendered. Run the code in",
"data-raw/render_available_datasets.R to update all_country_data"
),
{
expect_identical(
get_available_datasets_data,
render_available_datasets_data
)
}
)