Skip to content

Commit

Permalink
Add county_name to count_fcds() (#86)
Browse files Browse the repository at this point in the history
Add county_name to count_fcds()
  • Loading branch information
gadenbuie authored Oct 2, 2019
2 parents 35e2a78 + 66567e3 commit ba1728e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fcds
Title: Process Data from the Florida Cancer Data System
Version: 0.1.3
Version: 0.1.4
Authors@R:
c(person(given = "Garrick",
family = "Aden-Buie",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## fcds 0.1.4

* Added `county_name` argument to `count_fcds()` that adds the county into the
count groups. This value can be a vector of counties, or `TRUE` to include all
counties in the source data. Alternatively, if the value is `"moffitt"` then
the counts are filtered to counties in Moffitt's catchment area.

## fcds 0.1.2

* Add ICD-O-3 codes from [IACR](http://www.iacr.com.fr/index.php?option=com_content&view=category&layout=blog&id=100&Itemid=577)
Expand Down
49 changes: 42 additions & 7 deletions R/fcds.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ join_population_by_year <- function(
#'
#' fcds_example %>%
#' dplyr::filter(county_name == "Pinellas") %>%
#' count_fcds(cancer_site_group, sex = "Male")
#' count_fcds(cancer_site_group, sex = "Male", county_name = TRUE) %>%
#' head()
#'
#' fcds_example %>%
#' filter_age_groups(age_gt = 20, age_lt = 25) %>%
#' count_fcds(sex = TRUE, county_name = c("Pinellas", "Hillsborough"))
#'
#' fcds_example %>%
#' count_fcds(county_name = "moffitt") %>%
#' head()
#'
#' @return A grouped data frame with counts. The output groups includes the
#' union of the groups of the original input `data`, the groups specified by
Expand All @@ -80,8 +89,13 @@ join_population_by_year <- function(
#' `TRUE` to include all values of `race` present in input data
#' @param origin Character vector of values of `origin` to be included in count,
#' or `TRUE` to include all values of `origin` present in input data
#' @param moffitt_catchment Limit counties to those in the catchment area of the
#' [Moffitt Cancer Center](https://moffitt.org).
#' @param county_name Character vector of values of `county_name` to be included
#' in count, or `TRUE` to include all values of `county_name` present in the
#' input data, or `"moffitt"` to limit to the counties in the
#' catchment area of the [Moffitt Cancer Center](https://moffitt.org).
#' @param moffitt_catchment **Deprecated.** Please use `county_name =
#' "moffitt"` instead to limit counties to those in the catchment
#' area of the [Moffitt Cancer Center](https://moffitt.org).
#' @param default_groups Variables that should be included in the grouping,
#' prior to counting cancer cases. Set to `NULL` to use only the groups
#' already present in the input data.
Expand All @@ -96,14 +110,31 @@ count_fcds <- function(
sex = NULL,
race = NULL,
origin = NULL,
moffitt_catchment = FALSE,
county_name = NULL,
default_groups = c("year_group", "year", "age_group"),
discard_unseen_levels = TRUE
discard_unseen_levels = TRUE,
moffitt_catchment = FALSE
) {
if (!missing(moffitt_catchment)) {
.Deprecated(msg = paste(
"The moffitt_catchment argument is deprecated, please use",
"county_name = \"moffitt\""
))
if (!missing(county_name)) {
warning(
"Both `county` and `moffitt_catchment` were specified, ",
"only `county` will be used."
)
} else {
county_name <- if (moffitt_catchment) "moffitt"
}
}

filters <- list(
sex = sex,
race = race,
origin = origin
origin = origin,
county_name = county_name
)

for (var in names(filters)) {
Expand All @@ -116,7 +147,11 @@ count_fcds <- function(
))
}

if (moffitt_catchment) filters$county_name <- fcds_const("moffitt_catchment")
if (!is.null(filters$county_name) && length(filters$county_name) == 1) {
if (grepl("moffitt", tolower(filters$county_name))) {
filters$county_name <- fcds_const("moffitt_catchment")
}
}

filters <- purrr::compact(filters)

Expand Down
25 changes: 20 additions & 5 deletions man/count_fcds.Rd

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

38 changes: 35 additions & 3 deletions tests/testthat/test-fcds.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,40 @@ describe("count_fcds()", {

it("subsets to Moffitt counties", {
r_count_fcds_moffitt <- fcds::fcds_example %>%
count_fcds(moffitt_catchment = TRUE)
expect_known_hash(r_count_fcds_moffitt %>% dplyr::ungroup(), "a4ff52c455")
count_fcds(county_name = "moffitt_catchment") %>%
dplyr::ungroup()

r_count_fcds_moffitt2 <- fcds::fcds_example %>%
count_fcds(county_name = "Moffitt Cancer Center") %>%
dplyr::ungroup()

expect_known_hash(r_count_fcds_moffitt, "a4ff52c455")
expect_known_hash(r_count_fcds_moffitt2, "a4ff52c455")
})

it("moffitt_catchment is deprecated", {
expect_warning(
fcds::fcds_example %>% count_fcds(moffitt_catchment = TRUE),
"deprecated"
)

expect_identical(
suppressWarnings(fcds::fcds_example %>% count_fcds(moffitt_catchment = TRUE)),
fcds::fcds_example %>% count_fcds(county_name = "moffitt_catchment")
)
})

it("county_name = TRUE includes county_name in group vars", {
fcds_county <- fcds::fcds_example %>% count_fcds(county_name = TRUE)
expect_identical(
dplyr::group_vars(fcds_county),
c("county_name", "year_group", "year", "age_group")
)

expect_identical(
fcds_county %>% .$county_name %>% paste() %>% unique() %>% sort(),
fcds::fcds_example %>% .$county_name %>% paste() %>% unique() %>% sort()
)
})

it("errors when invalid FCDS constants are provided", {
Expand Down Expand Up @@ -138,7 +170,7 @@ describe("count_fcds()", {
it("removes un-observed factor levels in output groups", {
r_cfl <- fcds::fcds_example %>%
filter(year > 2000) %>%
count_fcds(moffitt_catchment = TRUE, sex = "Male")
count_fcds(county_name = "Moffitt", sex = "Male")

expect_true(
length(setdiff(levels(r_cfl$county_name), fcds_const("moffitt_catchment"))) == 0
Expand Down

0 comments on commit ba1728e

Please sign in to comment.