Skip to content

Commit

Permalink
Merge pull request #77 from getwilds/add-dot-lintr
Browse files Browse the repository at this point in the history
Add dot lintr
  • Loading branch information
realbp authored Mar 18, 2024
2 parents ee8903a + 1070705 commit 8a4bc13
Show file tree
Hide file tree
Showing 27 changed files with 130 additions and 113 deletions.
4 changes: 4 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: linters_with_defaults(
line_length_linter(120),
cyclocomp_linter(complexity_limit = 150)
)
3 changes: 1 addition & 2 deletions R/demo-crowding.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#'
#' @returns A data frame with the following columns: Area, Area Code,
#' Percent, Households, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -55,7 +55,6 @@
#' )
#' }
demo_crowding <- function(area, areatype, crowding, race) {

req <- create_request("demographics")

resp <- req %>%
Expand Down
6 changes: 3 additions & 3 deletions R/demo-education.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, Households, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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]

Expand All @@ -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)))
}
6 changes: 3 additions & 3 deletions R/demo-food-access.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Value, People.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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)))
}
}
4 changes: 2 additions & 2 deletions R/demo-income.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Dollars, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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)))
}
4 changes: 2 additions & 2 deletions R/demo-insurance.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, People, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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)))
}
4 changes: 2 additions & 2 deletions R/demo-mobility.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, People, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -74,6 +74,6 @@ demo_mobility <- function(area, areatype, mobility) {
"Percent",
"People",
"Rank"
)) %>%
)) %>%
mutate(across(c("Percent", "People"), \(x) as.numeric(x)))
}
4 changes: 2 additions & 2 deletions R/demo-non-english-language.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, Households, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -68,6 +68,6 @@ demo_language <- function(area, areatype, language) {
"Percent",
"Households",
"Rank"
)) %>%
)) %>%
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
}
10 changes: 5 additions & 5 deletions R/demo-population.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, Households, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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]
Expand All @@ -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)))
}
7 changes: 4 additions & 3 deletions R/demo-poverty.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, Households, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)))
}
}
6 changes: 3 additions & 3 deletions R/demo-svi.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' @importFrom dplyr mutate across
#'
#' @returns A data frame with the following columns: County, FIPS, Score.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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)))
}
4 changes: 2 additions & 2 deletions R/demo-workforce.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#'
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, People Unemployed, Rank.
#'
#'
#' @family demographics
#'
#' @export
Expand Down Expand Up @@ -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)))
}
8 changes: 4 additions & 4 deletions R/incidence-cancer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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",
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions R/mortality-cancer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down
32 changes: 15 additions & 17 deletions R/process-resp.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' - "risks"
#' - "incidence"
#' - "mortality"
#'
#'
#' @importFrom httr2 resp_body_string
#' @importFrom dplyr mutate_all na_if filter
#' @importFrom rlang sym
Expand All @@ -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]
Expand All @@ -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",
Expand All @@ -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, "*"))
}
Loading

0 comments on commit 8a4bc13

Please sign in to comment.