Skip to content

Commit

Permalink
Merge pull request #66 from getwilds/test
Browse files Browse the repository at this point in the history
styled with lintr
  • Loading branch information
realbp authored Mar 7, 2024
2 parents a8eff65 + cc4ffd1 commit 6b43f48
Show file tree
Hide file tree
Showing 116 changed files with 3,929 additions and 3,203 deletions.
25 changes: 13 additions & 12 deletions R/create-request.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#' Create Request to State Cancer Profile Data
#'
#'
#' This creates a request to the State Cancer Profiles URL
#'
#'
#' @param topic Either "demographics", "risk", "incidencerates", "deathrates"
#'
#'
#' @importFrom httr2 request
#'
#' @returns returns the HTTP method with the state cancer profiles url and the http path
#'
#'
#' @returns returns the HTTP method with the state cancer profiles
#' url and the http path
#'
#' @noRd
#'
#'
#' @examples
#' \dontrun{
#' create_request("demographics")
#' create_request("risk")
#' }
create_request <- function(topic) {
url = "https://statecancerprofiles.cancer.gov/"
url_end = "/index.php"
url = paste0(url, topic, url_end)
url <- "https://statecancerprofiles.cancer.gov/"
url_end <- "/index.php"

url <- paste0(url, topic, url_end)

request(url)
}
103 changes: 56 additions & 47 deletions R/demo-crowding.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' Access to Crowding Data
#'
#' This function returns a data frame about crowding demographics from State
#'
#' This function returns a data frame about crowding demographics from State
#' Cancer Profiles.
#'
#' @param area A state/territory abbreviation or USA.
#' @param areatype One of the following values:
#' - `"county"`
#' - `"hsa"` (Health Service Area)
#' - `"state"`.
#' @param crowding The only permissible value is
#' @param crowding The only permissible value is
#' `"household with >1 person per room"`.
#' @param race
#' @param race
#' One of the following values:
#' - `"All Races (includes Hispanic)"`
#' - `"White (includes Hispanic)"`
Expand All @@ -19,66 +19,75 @@
#' - `"Amer. Indian/Alaskan Native (includes Hispanic)"`
#' - `"Asian or Pacific Islander (includes Hispanic)"`
#' - `"Hispanic (Any Race)"`.
#'
#'
#' @importFrom httr2 req_url_query req_perform
#' @importFrom stats setNames
#' @importFrom dplyr mutate across
#'
#' @returns A data frame with the following columns: Area, Area Code,
#'
#' @returns A data frame with the following columns: Area, Area Code,
#' Percent, Households, Rank.
#'
#'
#' @export
#'
#'
#' @examples
#' \dontrun{
#' demo_crowding(area = "WA",
#' areatype = "hsa",
#' crowding = "household with >1 person per room",
#' race = "All Races (includes Hispanic)")
#'
#' demo_crowding(area = "usa",
#' areatype = "state",
#' crowding = "household with >1 person per room",
#' race = "All Races (includes Hispanic)")
#' demo_crowding(
#' area = "WA",
#' areatype = "hsa",
#' crowding = "household with >1 person per room",
#' race = "All Races (includes Hispanic)"
#' )
#'
#' demo_crowding(
#' area = "usa",
#' areatype = "state",
#' crowding = "household with >1 person per room",
#' race = "All Races (includes Hispanic)"
#' )
#'
#' demo_crowding(area = "pr",
#' areatype = "hsa",
#' crowding = "household with >1 person per room",
#' race = "black")
#' demo_crowding(
#' area = "pr",
#' areatype = "hsa",
#' crowding = "household with >1 person per room",
#' race = "black"
#' )
#' }
#'
demo_crowding <- function(area, areatype,
crowding = "household with >1 person per room",
#'
demo_crowding <- function(area, areatype,
crowding = "household with >1 person per room",
race) {

areatype <- tolower(areatype)
req <- create_request("demographics")
resp <- req %>%

resp <- req %>%
req_url_query(
stateFIPS=fips_scp(area),
areatype=areatype,
topic="crowd",
demo=handle_crowding(crowding),
race=handle_race(race),
type="manyareacensus",
sortVariableName="value",
sortOrder="default",
output=1
) %>%
stateFIPS = fips_scp(area),
areatype = areatype,
topic = "crowd",
demo = handle_crowding(crowding),
race = handle_race(race),
type = "manyareacensus",
sortVariableName = "value",
sortOrder = "default",
output = 1
) %>%
req_perform()

resp <- process_response(resp)

areatype_map <- c("county" = "County", "hsa" = "Health_Service_Area",
"state" = "State")

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]

resp %>%
setNames(c(areatype_title, areacode_title, "Percent",
"Households", "Rank")) %>%

resp %>%
setNames(c(
areatype_title, areacode_title, "Percent",
"Households", "Rank"
)) %>%
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
}
101 changes: 54 additions & 47 deletions R/demo-education.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Access to Education Data
#'
#' This function returns a data frame about education demographics from State Cancer Profiles.
#' This function returns a data frame about education demographics from
#' State Cancer Profiles.
#'
#' @param area A state/territory abbreviation or USA.
#' @param areatype One of the following values:
Expand Down Expand Up @@ -28,32 +29,38 @@
#' @importFrom cli cli_abort
#' @importFrom stats setNames
#'
#' @returns A data frame with the following columns: Area Type, Area Code, Percent, Households, Rank.
#' @returns A data frame with the following columns:
#' Area Type, Area Code, Percent, Households, Rank.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' demo_education(area = "wa",
#' areatype = "county",
#' education = "at least high school",
#' sex = "males")
#'
#' demo_education(area = "usa",
#' areatype = "state",
#' education = "at least bachelors degree",
#' sex = "both sexes",
#' race = "all races (includes hispanic)")
#'
#' demo_education(area = "pr",
#' areatype = "hsa",
#' education = "less than 9th grade")
#' demo_education(
#' area = "wa",
#' areatype = "county",
#' education = "at least high school",
#' sex = "males"
#' )
#'
#' demo_education(
#' area = "usa",
#' areatype = "state",
#' education = "at least bachelors degree",
#' sex = "both sexes",
#' race = "all races (includes hispanic)"
#' )
#'
#' demo_education(
#' area = "pr",
#' areatype = "hsa",
#' education = "less than 9th grade"
#' )
#' }
demo_education <- function(area, areatype, education, sex=NULL, race=NULL) {

demo_education <- function(area, areatype, education, sex = NULL, race = NULL) {
req <- create_request("demographics")

if(education == "less than 9th grade" && (!is.null(race) || !is.null(sex))) {
if (education == "less than 9th grade" && (!is.null(race) || !is.null(sex))) {
cli_abort("For Less than 9th Grade, Race and Sex must be NULL.")
} else if (education == "at least high school" && (!is.null(race) || is.null(sex))) {
cli_abort("For At Least High School, Race must be NULL and Sex must be NOT NULL.")
Expand All @@ -63,37 +70,37 @@ demo_education <- function(area, areatype, education, sex=NULL, race=NULL) {

resp <- req %>%
req_url_query(
stateFIPS=fips_scp(area),
areatype=tolower(areatype),
topic="ed",
demo=handle_education(education),
type="manyareacensus",
sortVariableName="value",
sortOrder="default",
output=1
stateFIPS = fips_scp(area),
areatype = tolower(areatype),
topic = "ed",
demo = handle_education(education),
type = "manyareacensus",
sortVariableName = "value",
sortOrder = "default",
output = 1
)

if(!is.null(race)) {
resp <- resp %>%
req_url_query(race=handle_race(race))
}

if(!is.null(sex)) {
resp <- resp %>%
req_url_query(sex=handle_sex(sex))
}
if (!is.null(race)) {
resp <- resp %>%
req_url_query(race = handle_race(race))
}

if (!is.null(sex)) {
resp <- resp %>%
req_perform()
req_url_query(sex = handle_sex(sex))
}

resp <- resp %>%
req_perform()

resp <- process_response(resp)

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]

resp <- process_response(resp)

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]

resp %>%
setNames(c(areatype_title, areacode_title, "Percent", "Households", "Rank"))
resp %>%
setNames(c(areatype_title, areacode_title, "Percent", "Households", "Rank"))
}
Loading

0 comments on commit 6b43f48

Please sign in to comment.