diff --git a/.lintr b/.lintr new file mode 100644 index 0000000..dd74967 --- /dev/null +++ b/.lintr @@ -0,0 +1,4 @@ +linters: linters_with_defaults( + line_length_linter(120), + cyclocomp_linter(complexity_limit = 150) + ) diff --git a/R/demo-crowding.R b/R/demo-crowding.R index dd96acf..72a99be 100644 --- a/R/demo-crowding.R +++ b/R/demo-crowding.R @@ -26,7 +26,7 @@ #' #' @returns A data frame with the following columns: Area, Area Code, #' Percent, Households, Rank. -#' +#' #' @family demographics #' #' @export @@ -55,7 +55,6 @@ #' ) #' } demo_crowding <- function(area, areatype, crowding, race) { - req <- create_request("demographics") resp <- req %>% diff --git a/R/demo-education.R b/R/demo-education.R index 1e06ab4..4661146 100644 --- a/R/demo-education.R +++ b/R/demo-education.R @@ -31,7 +31,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Households, Rank. -#' +#' #' @family demographics #' #' @export @@ -96,7 +96,7 @@ demo_education <- function(area, areatype, education, sex = NULL, race = NULL) { req_perform() resp <- process_resp(resp, "demographics") - + area_type <- get_area(areatype)[1] area_code <- get_area(areatype)[2] @@ -107,6 +107,6 @@ demo_education <- function(area, areatype, education, sex = NULL, race = NULL) { "Percent", "Households", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "Households"), \(x) as.numeric(x))) } diff --git a/R/demo-food-access.R b/R/demo-food-access.R index 6ba1aa5..67d6930 100644 --- a/R/demo-food-access.R +++ b/R/demo-food-access.R @@ -21,7 +21,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Value, People. -#' +#' #' @family demographics #' #' @export @@ -84,11 +84,11 @@ demo_food <- function(area, areatype, food, race = NULL) { if (food == "limited access to healthy food") { resp %>% - setNames(c(area_type, area_code, "Percent", "People")) %>% + setNames(c(area_type, area_code, "Percent", "People")) %>% mutate(across(c("Percent", "People"), \(x) as.numeric(x))) } else if (food == "food insecurity") { resp %>% - setNames(c(area_type, area_code, "Percent")) %>% + setNames(c(area_type, area_code, "Percent")) %>% mutate(across(c("Percent"), \(x) as.numeric(x))) } } diff --git a/R/demo-income.R b/R/demo-income.R index 76927e3..faa432c 100644 --- a/R/demo-income.R +++ b/R/demo-income.R @@ -21,7 +21,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Dollars, Rank. -#' +#' #' @family demographics #' #' @export @@ -72,6 +72,6 @@ demo_income <- function(area, areatype, income, race) { area_code <- get_area(areatype)[2] resp %>% - setNames(c(area_type, area_code, "Dollars", "Rank")) %>% + setNames(c(area_type, area_code, "Dollars", "Rank")) %>% mutate(across(c("Dollars"), \(x) as.numeric(x))) } diff --git a/R/demo-insurance.R b/R/demo-insurance.R index b5e8acb..090a625 100644 --- a/R/demo-insurance.R +++ b/R/demo-insurance.R @@ -55,7 +55,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, People, Rank. -#' +#' #' @family demographics #' #' @export @@ -136,6 +136,6 @@ demo_insurance <- function(area, areatype, insurance, sex, age, race = NULL) { area_code <- get_area(areatype)[2] resp %>% - setNames(c(area_type, area_code, "Percent", "People", "Rank")) %>% + setNames(c(area_type, area_code, "Percent", "People", "Rank")) %>% mutate(across(c("Percent", "People"), \(x) as.numeric(x))) } diff --git a/R/demo-mobility.R b/R/demo-mobility.R index e1cd52e..f836dda 100644 --- a/R/demo-mobility.R +++ b/R/demo-mobility.R @@ -21,7 +21,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, People, Rank. -#' +#' #' @family demographics #' #' @export @@ -74,6 +74,6 @@ demo_mobility <- function(area, areatype, mobility) { "Percent", "People", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "People"), \(x) as.numeric(x))) } diff --git a/R/demo-non-english-language.R b/R/demo-non-english-language.R index 32417c7..67c7419 100644 --- a/R/demo-non-english-language.R +++ b/R/demo-non-english-language.R @@ -17,7 +17,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Households, Rank. -#' +#' #' @family demographics #' #' @export @@ -68,6 +68,6 @@ demo_language <- function(area, areatype, language) { "Percent", "Households", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "Households"), \(x) as.numeric(x))) } diff --git a/R/demo-population.R b/R/demo-population.R index cd81c8b..03a8a67 100644 --- a/R/demo-population.R +++ b/R/demo-population.R @@ -44,7 +44,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Households, Rank. -#' +#' #' @family demographics #' #' @export @@ -87,8 +87,8 @@ demo_population <- function(area, areatype, population, race = NULL, sex = NULL) cli_abort("for males, Race must not be NULL and Sex must be NULL") } else if (population == "foreign born" && (is.null(race) || is.null(sex))) { cli_abort("for foreign born, race and sex must not be NULL") - } else if ((population == "american indian/alaska native" || population == "asian/pacific islander" || population == "black" || - population == "hispanic" || population == "non-hispanic (origin recode)" || + } else if ((population == "american indian/alaska native" || population == "asian/pacific islander" || + population == "black" || population == "hispanic" || population == "non-hispanic (origin recode)" || population == "white") && (is.null(sex) || !is.null(race))) { cli_abort("for races other than foreign born, Sex must not be NULL and race must be NULL") } @@ -117,7 +117,7 @@ demo_population <- function(area, areatype, population, race = NULL, sex = NULL) resp <- resp %>% req_perform() - + resp <- process_resp(resp, "demographics") area_type <- get_area(areatype)[1] @@ -130,6 +130,6 @@ demo_population <- function(area, areatype, population, race = NULL, sex = NULL) "Percent", "People", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "People"), \(x) as.numeric(x))) } diff --git a/R/demo-poverty.R b/R/demo-poverty.R index f55c3c0..c83b45c 100644 --- a/R/demo-poverty.R +++ b/R/demo-poverty.R @@ -33,7 +33,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Households, Rank. -#' +#' #' @family demographics #' #' @export @@ -66,7 +66,8 @@ demo_poverty <- function(area, areatype, poverty, race = NULL, sex = NULL) { if (poverty == "persistent poverty" && (areatype == "hsa" || areatype == "state")) { cli_abort("For persistent poverty, areatype must be county") } - if ((poverty == "persistent poverty" || poverty == "persons < 150% of poverty") && (!is.null(race) || !is.null(sex))) { + if ((poverty == "persistent poverty" || poverty == "persons < 150% of poverty") && + (!is.null(race) || !is.null(sex))) { cli_abort("for persistent poverty and persons < 150% of poverty, Race and Sex must be NULL") } else if ((poverty == "families below poverty") && (!is.null(sex) || is.null(race))) { cli_abort("for families below poverty, Sex must be NULL and Race must not be NULL") @@ -119,7 +120,7 @@ demo_poverty <- function(area, areatype, poverty, race = NULL, sex = NULL) { "Percent", "People", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "People"), \(x) as.numeric(x))) } } diff --git a/R/demo-svi.R b/R/demo-svi.R index 38143c3..1d3b1e9 100644 --- a/R/demo-svi.R +++ b/R/demo-svi.R @@ -16,7 +16,7 @@ #' @importFrom dplyr mutate across #' #' @returns A data frame with the following columns: County, FIPS, Score. -#' +#' #' @family demographics #' #' @export @@ -53,10 +53,10 @@ demo_svi <- function(area, svi) { output = 1 ) %>% req_perform() - + resp <- process_resp(resp, "demographics") resp %>% - setNames(c("County", "FIPS", "Score")) %>% + setNames(c("County", "FIPS", "Score")) %>% mutate(across(c("Score"), \(x) as.numeric(x))) } diff --git a/R/demo-workforce.R b/R/demo-workforce.R index cc7bc47..95652cc 100644 --- a/R/demo-workforce.R +++ b/R/demo-workforce.R @@ -28,7 +28,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, People Unemployed, Rank. -#' +#' #' @family demographics #' #' @export @@ -89,6 +89,6 @@ demo_workforce <- function(area, areatype, workforce, race, sex) { "Percent", "People_Unemployed", "Rank" - )) %>% + )) %>% mutate(across(c("Percent", "People_Unemployed"), \(x) as.numeric(x))) } diff --git a/R/incidence-cancer.R b/R/incidence-cancer.R index 108df93..9efe1c5 100644 --- a/R/incidence-cancer.R +++ b/R/incidence-cancer.R @@ -57,7 +57,7 @@ #' @param year One of the following values: #' - `"latest 5 year average"` #' - `"latest single year (us by state)"`. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom cli cli_abort #' @importFrom stats setNames @@ -174,7 +174,7 @@ incidence_cancer <- function(area, areatype, cancer, race, sex, age, stage, year req_perform() resp <- process_resp(resp, "incidence") - + area_type <- get_area(areatype)[1] area_code <- get_area(areatype)[2] @@ -198,7 +198,7 @@ incidence_cancer <- function(area, areatype, cancer, race, sex, age, stage, year "Recent_5_Year_Trend", "Trend_Lower_95%_CI", "Trend_Upper_95%_CI" - )) %>% + )) %>% mutate(across(c( all_of(shared_names_to_numeric), "Recent_5_Year_Trend", @@ -213,7 +213,7 @@ incidence_cancer <- function(area, areatype, cancer, race, sex, age, stage, year shared_names_to_numeric, "Annual_Average_Count", "Percentage_of_Cases_with_Late_Stage" - )) %>% + )) %>% mutate(across(c( all_of(shared_names_to_numeric), "Percentage_of_Cases_with_Late_Stage" diff --git a/R/mortality-cancer.R b/R/mortality-cancer.R index 31d1f46..e1b8e90 100644 --- a/R/mortality-cancer.R +++ b/R/mortality-cancer.R @@ -52,7 +52,7 @@ #' @param year One of the following values: #' - `"latest 5 year average"` #' - `"latest single year (us by state)"`. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom cli cli_abort #' @importFrom stats setNames @@ -165,7 +165,7 @@ mortality_cancer <- function(area, areatype, cancer, race, sex, age, year) { "Lower_CI_Rank", "Upper_CI_Rank" ) - + resp %>% setNames(c( area_type, @@ -182,7 +182,7 @@ mortality_cancer <- function(area, areatype, cancer, race, sex, age, year) { "Recent_5_Year_Trend", "Lower_95%_CI_Trend", "Upper_95%_CI_Trend" - )) %>% + )) %>% mutate(across(c( all_of(names_to_numeric), "Recent_5_Year_Trend", diff --git a/R/process-resp.R b/R/process-resp.R index 9d71bc0..4f50147 100644 --- a/R/process-resp.R +++ b/R/process-resp.R @@ -8,7 +8,7 @@ #' - "risks" #' - "incidence" #' - "mortality" -#' +#' #' @importFrom httr2 resp_body_string #' @importFrom dplyr mutate_all na_if filter #' @importFrom rlang sym @@ -24,23 +24,22 @@ #' process_resp(resp, demographics) #' } process_resp <- function(resp, topic) { - if (httr2::resp_content_type(resp) != "text/csv") { cli_abort("Invalid input, please check documentation for valid arguments.") } - + nenv <- new.env() data("state", envir = nenv) state_name <- nenv$state.name - + resp_lines <- resp %>% resp_body_string() %>% strsplit("\\n") %>% unlist() - + if (topic == "demographics") { index_first_line_break <- which(resp_lines == "")[1] - index_second_line_break <- which(resp_lines == "")[2] + index_second_line_break <- which(resp_lines == "")[2] } else if (topic == "risks") { index_first_line_break <- which(resp_lines == "")[3] index_second_line_break <- which(resp_lines == "")[4] @@ -50,21 +49,20 @@ process_resp <- function(resp, topic) { } else { cli_abort("Incorrect topic argument, please ensure that correct.") } - + resp <- resp_lines[ (index_first_line_break + 1): - (index_second_line_break - 1) + (index_second_line_break - 1) ] %>% paste(collapse = "\n") %>% ( \(x) { read.csv(textConnection(x), - header = TRUE, - colClasses = "character" + header = TRUE, + colClasses = "character" ) - } - )() - + })() + column <- c( "Health.Service.Area", "County", @@ -74,17 +72,17 @@ process_resp <- function(resp, topic) { "County", "State" ) %in% colnames(resp)] - + resp <- resp %>% filter(!!sym(column) != "United States") - + if (column %in% c("Health.Service.Area", "County")) { resp <- resp %>% filter(!(!!sym(column) %in% state_name)) } resp %>% - mutate_all(stringr::str_trim) %>% + mutate_all(stringr::str_trim) %>% mutate_all(\(x) na_if(x, "N/A")) %>% - mutate_all(\(x) na_if(x, "data not available")) %>% + mutate_all(\(x) na_if(x, "data not available")) %>% mutate_all(\(x) na_if(x, "*")) } diff --git a/R/risk-alcohol.R b/R/risk-alcohol.R index 463ef37..236a04e 100644 --- a/R/risk-alcohol.R +++ b/R/risk-alcohol.R @@ -25,7 +25,7 @@ #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Lower 95% CI, #' Upper 95% CI, Number of Respondents. -#' +#' #' @family risks #' #' @export diff --git a/R/risk-colorectal-screening.R b/R/risk-colorectal-screening.R index 8d4415e..77bcf12 100644 --- a/R/risk-colorectal-screening.R +++ b/R/risk-colorectal-screening.R @@ -21,7 +21,7 @@ #' - `"male"` #' - `"female"`. #' @param area A state/territory abbreviation or USA. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom cli cli_abort #' @importFrom stats setNames @@ -29,7 +29,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, People Unemployed, Rank. -#' +#' #' @family risks #' #' @export @@ -111,7 +111,7 @@ risk_colorectal_screening <- function(screening, race = NULL, sex = NULL, area = "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", @@ -126,7 +126,7 @@ risk_colorectal_screening <- function(screening, race = NULL, sex = NULL, area = "Model_Based_Percent (95%_Confidence_Interval)", "Lower_95%_CI", "Upper_95%_CI" - )) %>% + )) %>% mutate(across(c( "Model_Based_Percent (95%_Confidence_Interval)", "Lower_95%_CI", diff --git a/R/risk-diet-exercise.R b/R/risk-diet-exercise.R index 2bf7400..1bdeb65 100644 --- a/R/risk-diet-exercise.R +++ b/R/risk-diet-exercise.R @@ -22,7 +22,7 @@ #' - `"both sexes"` #' - `"male"` #' - `"female"`. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom stats setNames #' @importFrom dplyr mutate across @@ -30,7 +30,7 @@ #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, #' Lower 95% CI, Upper 95% CI, Number of Respondents. -#' +#' #' @family risks #' #' @export @@ -79,7 +79,7 @@ risk_diet_exercise <- function(diet_exercise, race, sex) { "Percent", "Lower_95%_CI", "Upper_95%_CI" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", @@ -94,7 +94,7 @@ risk_diet_exercise <- function(diet_exercise, race, sex) { "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", diff --git a/R/risk-smoking.R b/R/risk-smoking.R index f74b3a2..b2ef7c3 100644 --- a/R/risk-smoking.R +++ b/R/risk-smoking.R @@ -47,7 +47,7 @@ #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, Lower CI 95%, Upper CI 95%, #' Number of Respondents. -#' +#' #' @family risks #' #' @export @@ -161,7 +161,8 @@ risk_smoking <- function(smoking, race = NULL, sex = NULL, datatype = NULL, area # smoking group 6 if (smoking %in% smoking_group6 && (is.null(race) || is.null(sex))) { cli_abort("For this smoking group, Race and Sex must not be NULL") - } else if (smoking %in% smoking_group6 && (!is.null(race) && !is.null(sex)) && race == "all races (includes hispanic)") { + } else if (smoking %in% smoking_group6 && (!is.null(race) && !is.null(sex)) && + race == "all races (includes hispanic)") { if (is.null(datatype)) { cli_abort("For all races for this smoking type, Datatype must not be NULL") } else if (datatype == "direct estimates" && !is.null(area)) { @@ -226,7 +227,7 @@ risk_smoking <- function(smoking, race = NULL, sex = NULL, datatype = NULL, area "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", @@ -248,7 +249,7 @@ risk_smoking <- function(smoking, race = NULL, sex = NULL, datatype = NULL, area "Percent", "Lower_95%_CI", "Upper_95%_CI" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", diff --git a/R/risk-vaccines.R b/R/risk-vaccines.R index 6bfd2b1..fb8ce18 100644 --- a/R/risk-vaccines.R +++ b/R/risk-vaccines.R @@ -10,7 +10,7 @@ #' - `"both sexes"` #' - `"male"` #' - `"female"`. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom stats setNames #' @importFrom dplyr mutate across @@ -18,7 +18,7 @@ #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, #' Lower 95% CI, Upper 95% CI, Number of Respondents. -#' +#' #' @family risks #' #' @export @@ -70,7 +70,7 @@ risk_vaccines <- function(vaccine, sex) { "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Met_Objective_of_80.0%?", "Percent", @@ -87,7 +87,7 @@ risk_vaccines <- function(vaccine, sex) { "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", diff --git a/R/risk-womens-health.R b/R/risk-womens-health.R index cd347b9..fc2d6fc 100644 --- a/R/risk-womens-health.R +++ b/R/risk-womens-health.R @@ -18,7 +18,7 @@ #' - `"direct estimates"` #' - `"county level modeled estimates"`. #' @param area A state/territory abbreviation or USA. -#' +#' #' @importFrom httr2 req_url_query req_perform #' @importFrom cli cli_abort #' @importFrom stats setNames @@ -26,7 +26,7 @@ #' #' @returns A data frame with the following columns: #' Area Type, Area Code, Percent, People Unemployed, Rank. -#' +#' #' @family risks #' #' @export @@ -67,7 +67,8 @@ risk_women_health <- function(women_health, race, datatype = "direct estimates", cli_abort("For all races (includes hispanic), datatype must NOT be NULL") } else if ((race %in% risk_races && race != "all races (includes hispanic)") && (!is.null(area))) { cli_abort("For races other than all races (includes hispanic), area must be NULL") - } else if ((race == "all races (includes hispanic)" && datatype == "county level modeled estimates") && is.null(area)) { + } else if ((race == "all races (includes hispanic)" && datatype == "county level modeled estimates") && + is.null(area)) { cli_abort("For county level modeled estimates, Area must NOT be NULL") } @@ -105,7 +106,7 @@ risk_women_health <- function(women_health, race, datatype = "direct estimates", "Percent", "Lower_95%_CI", "Upper_95%_CI" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", @@ -120,7 +121,7 @@ risk_women_health <- function(women_health, race, datatype = "direct estimates", "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents" - )) %>% + )) %>% mutate(across(c( "Percent", "Lower_95%_CI", diff --git a/R/utils.R b/R/utils.R index 9195b42..61ba0e5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,20 +7,18 @@ #' - "hsa" #' - "state". #' @return A character vector containing the title and code of the specified area type. -#' +#' @noRd #' @examples -#' \dontrun{ +#' \dontrun{ #' get_area("county") #' get_area("hsa") #' } - - get_area <- function(areatype) { areatype_map <- c("county" = "County", "hsa" = "Health_Service_Area", "state" = "State") areacode_map <- c("county" = "FIPS", "state" = "FIPS", "hsa" = "HSA_Code") - + areatype_title <- areatype_map[areatype] areacode_title <- areacode_map[areatype] - + return(c(areatype_title, areacode_title)) -} \ No newline at end of file +} diff --git a/tests/testthat/test-handle-alcohol.R b/tests/testthat/test-handle-alcohol.R index 77729ca..58e089c 100644 --- a/tests/testthat/test-handle-alcohol.R +++ b/tests/testthat/test-handle-alcohol.R @@ -2,8 +2,12 @@ #' #' This testthat file tests the handle-age function test_that("handle_alcohol correctly maps alcohol", { - result <- sapply(c("binge drinking (4+ drinks on one occasion for women, 5+ drinks for one occasion for men), ages 21+"), handle_alcohol) - expected <- c(`binge drinking (4+ drinks on one occasion for women, 5+ drinks for one occasion for men), ages 21+` = "v505") + result <- sapply( + c("binge drinking (4+ drinks on one occasion for women, 5+ drinks for one occasion for men), ages 21+"), + handle_alcohol + ) + expected <- c(`binge drinking (4+ drinks on one occasion for women, 5+ drinks for one occasion for men), + ages 21+` = "v505") expect_equal(result, expected) }) diff --git a/tests/testthat/test-incidence-cancer.R b/tests/testthat/test-incidence-cancer.R index 4c0525b..9fa3a5f 100644 --- a/tests/testthat/test-incidence-cancer.R +++ b/tests/testthat/test-incidence-cancer.R @@ -108,16 +108,20 @@ test_that("incidence_cancer has correct number of columns", { expect_equal(ncol(df2), expected_columns2) }) -#test error handling +# test error handling test_that("incidence_cancer handles invalid cancer parameters", { expect_error( - incidence_cancer("wa", "county", "all cancer sites", "black (non-hispanic)", - "both sexes", "ages 65+", "late stage (regional & distant)"), + incidence_cancer( + "wa", "county", "all cancer sites", "black (non-hispanic)", + "both sexes", "ages 65+", "late stage (regional & distant)" + ), "For this cancer type, stage must be all stages" ) expect_error( - incidence_cancer("ca", "hsa", "prostate", "all races (includes hispanic)", "both sexes", - "ages 50+", "all stages"), + incidence_cancer( + "ca", "hsa", "prostate", "all races (includes hispanic)", "both sexes", + "ages 50+", "all stages" + ), "For prostate cancer, sex must be males." ) }) diff --git a/tests/testthat/test-mortality-cancer.R b/tests/testthat/test-mortality-cancer.R index 67cf033..c38280f 100644 --- a/tests/testthat/test-mortality-cancer.R +++ b/tests/testthat/test-mortality-cancer.R @@ -105,23 +105,26 @@ test_that("mortality_cancer has correct number of columns", { expect_equal(ncol(df), expected_columns) }) -#test error handling +# test error handling test_that("mortality_cancer handles invalid cancer parameters", { expect_error( - mortality_cancer(area="wa", areatype="county", cancer="ovary", - race="all races (includes hispanic)", - sex="both sexes", age="ages 50+"), + mortality_cancer( + area = "wa", areatype = "county", cancer = "ovary", + race = "all races (includes hispanic)", + sex = "both sexes", age = "ages 50+" + ), "For this cancer type, sex must be females" ) expect_error( - mortality_cancer("usa", "state", "prostate", "all races (includes hispanic)", - "both sexes", "ages 50+"), + mortality_cancer( + "usa", "state", "prostate", "all races (includes hispanic)", + "both sexes", "ages 50+" + ), "For prostate cancer, sex must be males." ) }) -#parameter +# parameter test_that("mortality_cancer has correct parameters", { expect_error(mortality_cancer()) }) - diff --git a/tests/testthat/test-process-resp.R b/tests/testthat/test-process-resp.R index d4387a8..f4104a3 100644 --- a/tests/testthat/test-process-resp.R +++ b/tests/testthat/test-process-resp.R @@ -11,16 +11,15 @@ resp_list <- list( ) for (resp_name in names(resp_list)) { - resp <- resp_list[[resp_name]] result <- process_resp(resp, resp_name) - + # process response should return a data frame test_that("process_response should return a data frame", { skip_on_cran() expect_true(is.data.frame(result)) }) - + # process response should have the correct parameter test_that("process response should have resp as an argument", { skip_on_cran() @@ -28,30 +27,29 @@ for (resp_name in names(resp_list)) { process_response() ) }) - + # process response data should start on the column names and end with data # containing a FIPS value test_that("process response outputs data from the correct line", { skip_on_cran() area_headers <- c("County", "State", "Health.Service.Area") - + expect_true(any(colnames(result) %in% area_headers)) - + expect_true("FIPS" %in% colnames(result)) expect_true(!is.na(result[nrow(result), "FIPS"])) }) - + # process response filters out correct data test_that("process response filters out United States and state names", { skip_on_cran() # Filters out "United States" from all results expect_false(any(result[1] == "United States")) - + # Filters out State names from County and HSA county_hsa <- c("County", "Health.Service.Area") if (colnames(result)[1] %in% county_hsa) { expect_false(any(result[1] == state.name)) } - }) + }) } - diff --git a/tests/testthat/test-risk-smoking.R b/tests/testthat/test-risk-smoking.R index 7f00c5b..1c5caec 100644 --- a/tests/testthat/test-risk-smoking.R +++ b/tests/testthat/test-risk-smoking.R @@ -143,8 +143,10 @@ test_that("risk-smoking handles invalid smoking parameters", { sex = "both sexes", datatype = "county level modeled estimates" ), - paste("For county level modeled estimates on this smoking type,", - "area must NOT be null") + paste( + "For county level modeled estimates on this smoking type,", + "area must NOT be null" + ) ) expect_error( risk_smoking("smoking not allowed at work (current smokers)", @@ -158,16 +160,20 @@ test_that("risk-smoking handles invalid smoking parameters", { sex = "both sexes", datatype = "county level modeled estimates" ), - paste("For this smoking type, Sex, Datatype,", - "and Area must not be NULL AND Race must be NULL") + paste( + "For this smoking type, Sex, Datatype,", + "and Area must not be NULL AND Race must be NULL" + ) ) expect_error( risk_smoking("smokers (ever); ages 18+", race = "hispanic (any race)", sex = "both sexes" ), - paste("For this smoking type, Race, Sex,", - "and Datatype must not be NULL AND Datatype and Area must be NULL") + paste( + "For this smoking type, Race, Sex,", + "and Datatype must not be NULL AND Datatype and Area must be NULL" + ) ) expect_error( risk_smoking("smokers (current); ages 18+",