Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Oct 8, 2023
1 parent 3ed6fa0 commit 7442407
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 54 deletions.
4 changes: 4 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ utils::globalVariables(c(
"y", # <hospitals>
"address_line_1", # <hospitals>
"address_line_2", # <hospitals>
"organization_type_structure", # <hospitals>
"organization_other_type_text", # <hospitals>
"practice_location_type", # <hospitals>
"location_other_type_text", # <hospitals>
"flag", # <hospitals>
"y", # <laboratories>
"pgm_trmntn_cd", # <laboratories>
Expand Down
141 changes: 98 additions & 43 deletions R/hospitals.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
#' on the CMS form
#' @param reh_conversion < *boolean* > Indicates a former Hospital or Critical
#' Access Hospital that converted to a Rural Emergency Hospital
#' @param tidy < *boolean* > Tidy output; default is `TRUE`.
#' @param na.rm < *boolean* > Remove empty rows and columns; default is `TRUE`.
#' @param tidy < *boolean* > // __default:__ `TRUE` Tidy output
#' @param pivot < *boolean* > // __default:__ `TRUE` Pivot output
#' @param na.rm < *boolean* > // __default:__ `TRUE` Remove empty rows and columns
#'
#' @return A [tibble][tibble::tibble-package] containing the search results.
#'
Expand Down Expand Up @@ -96,6 +97,7 @@ hospitals <- function(npi = NULL,
other = NULL,
reh_conversion = NULL,
tidy = TRUE,
pivot = TRUE,
na.rm = TRUE) {

if (!is.null(npi)) {npi <- npi_check(npi)}
Expand All @@ -106,37 +108,37 @@ hospitals <- function(npi = NULL,
if (!is.null(enroll_id_org)) {enroll_org_check(enroll_id_org)}
if (!is.null(proprietary_nonprofit)) {rlang::arg_match(proprietary_nonprofit, c("P", "N"))}

if (!is.null(multiple_npis)) {multiple_npis <- tf_2_yn(multiple_npis)}
if (!is.null(general)) {general <- tf_2_yn(general)}
if (!is.null(acute_care)) {acute_care <- tf_2_yn(acute_care)}
if (!is.null(alcohol_drug)) {alcohol_drug <- tf_2_yn(alcohol_drug)}
if (!is.null(childrens)) {childrens <- tf_2_yn(childrens)}
if (!is.null(long_term)) {long_term <- tf_2_yn(long_term)}
if (!is.null(psychiatric)) {psychiatric <- tf_2_yn(psychiatric)}
if (!is.null(rehabilitation)) {rehabilitation <- tf_2_yn(rehabilitation)}
if (!is.null(short_term)) {short_term <- tf_2_yn(short_term)}
if (!is.null(swing_bed)) {swing_bed <- tf_2_yn(swing_bed)}
if (!is.null(psych_unit)) {psych_unit <- tf_2_yn(psych_unit)}
if (!is.null(rehab_unit)) {rehab_unit <- tf_2_yn(rehab_unit)}
if (!is.null(multiple_npis)) {multiple_npis <- tf_2_yn(multiple_npis)}
if (!is.null(general)) {general <- tf_2_yn(general)}
if (!is.null(acute_care)) {acute_care <- tf_2_yn(acute_care)}
if (!is.null(alcohol_drug)) {alcohol_drug <- tf_2_yn(alcohol_drug)}
if (!is.null(childrens)) {childrens <- tf_2_yn(childrens)}
if (!is.null(long_term)) {long_term <- tf_2_yn(long_term)}
if (!is.null(psychiatric)) {psychiatric <- tf_2_yn(psychiatric)}
if (!is.null(rehabilitation)) {rehabilitation <- tf_2_yn(rehabilitation)}
if (!is.null(short_term)) {short_term <- tf_2_yn(short_term)}
if (!is.null(swing_bed)) {swing_bed <- tf_2_yn(swing_bed)}
if (!is.null(psych_unit)) {psych_unit <- tf_2_yn(psych_unit)}
if (!is.null(rehab_unit)) {rehab_unit <- tf_2_yn(rehab_unit)}
if (!is.null(specialty_hospital)) {specialty_hospital <- tf_2_yn(specialty_hospital)}
if (!is.null(other)) {other <- tf_2_yn(other)}
if (!is.null(reh_conversion)) {reh_conversion <- tf_2_yn(reh_conversion)}
if (!is.null(other)) {other <- tf_2_yn(other)}
if (!is.null(reh_conversion)) {reh_conversion <- tf_2_yn(reh_conversion)}

args <- dplyr::tribble(
~param, ~arg,
"NPI", npi,
"CCN", facility_ccn,
"ENROLLMENT ID", enroll_id_org,
"ENROLLMENT STATE", enroll_state,
"PROVIDER TYPE CODE", specialty_code,
"ASSOCIATE ID", pac_org,
"ORGANIZATION NAME", organization,
"DOING BUSINESS AS NAME", doing_business_as,
"CITY", city,
"STATE", state,
"ZIP CODE", zip,
"PROPRIETARY_NONPROFIT", proprietary_nonprofit,
"MULTIPLE NPI FLAG", multiple_npis,
~param, ~arg,
"NPI", npi,
"CCN", facility_ccn,
"ENROLLMENT ID", enroll_id_org,
"ENROLLMENT STATE", enroll_state,
"PROVIDER TYPE CODE", specialty_code,
"ASSOCIATE ID", pac_org,
"ORGANIZATION NAME", organization,
"DOING BUSINESS AS NAME", doing_business_as,
"CITY", city,
"STATE", state,
"ZIP CODE", zip,
"PROPRIETARY_NONPROFIT", proprietary_nonprofit,
"MULTIPLE NPI FLAG", multiple_npis,
"SUBGROUP %2D GENERAL", general,
"SUBGROUP %2D ACUTE CARE", acute_care,
"SUBGROUP %2D ALCOHOL DRUG", alcohol_drug,
Expand All @@ -150,7 +152,7 @@ hospitals <- function(npi = NULL,
"SUBGROUP %2D REHABILITATION UNIT", rehab_unit,
"SUBGROUP %2D SPECIALTY HOSPITAL", specialty_hospital,
"SUBGROUP %2D OTHER", other,
"REH CONVERSION FLAG", reh_conversion)
"REH CONVERSION FLAG", reh_conversion)

response <- httr2::request(build_url("hos", args)) |> httr2::req_perform()

Expand Down Expand Up @@ -207,19 +209,28 @@ hospitals <- function(npi = NULL,
tidyr::unite("address",
address_line_1:address_line_2,
remove = TRUE, na.rm = TRUE) |>
hosp_cols() |>
tidyr::pivot_longer(
cols = c("Multiple NPIs",
"REH Conversion",
dplyr::contains("Subgroup")),
tidyr::unite("org_structure",
organization_type_structure:organization_other_type_text,
remove = TRUE, na.rm = TRUE, sep = ": ") |>
tidyr::unite("location_type",
practice_location_type:location_other_type_text,
remove = TRUE, na.rm = TRUE, sep = ": ") |>
hosp_cols()

if (pivot) {
results <- hosp_cols2(results) |>
tidyr::pivot_longer(cols = c("Multiple NPIs", "REH Conversion",
dplyr::contains("Subgroup")),
names_to = "status",
values_to = "flag") |>
dplyr::filter(flag == TRUE) |>
dplyr::mutate(flag = NULL)
}

if (na.rm) {results <- janitor::remove_empty(results,
which = c("rows", "cols"))}
which = c("rows", "cols"))
}
}
return(results)
}

Expand All @@ -239,15 +250,59 @@ hosp_cols <- function(df) {
'enroll_state' = 'enrollment_state',
'incorporation_date',
'incorporation_state',
'organization_structure' = 'organization_type_structure',
'org_other' = 'organization_other_type_text',
'org_structure',
'address',
'city',
'state',
'zip' = 'zip_code',
'location_type' = 'practice_location_type',
'location_other' = 'location_other_type_text',
'cah_or_hospital_ccn',
'location_type',
'associated_ccn' = 'cah_or_hospital_ccn',
'reh_conversion_date',
'proprietary_nonprofit',
'multiple_npi_flag',
'reh_conversion_flag',
'subgroup_general',
'subgroup_acute_care',
'subgroup_alcohol_drug',
'subgroup_childrens',
'subgroup_long_term',
'subgroup_psychiatric',
'subgroup_rehabilitation',
'subgroup_short_term',
'subgroup_swing_bed_approved',
'subgroup_psychiatric_unit',
'subgroup_rehabilitation_unit',
'subgroup_specialty_hospital',
'subgroup_other',
'subgroup_other_text')

df |> dplyr::select(dplyr::any_of(cols))

}

#' @param df data frame
#' @autoglobal
#' @noRd
hosp_cols2 <- function(df) {

cols <- c('npi',
'organization',
'doing_business_as',
'pac_org',
'enroll_id_org',
'facility_ccn',
'specialty_code',
'specialty',
'enroll_state',
'incorporation_date',
'incorporation_state',
'org_structure',
'address',
'city',
'state',
'zip',
'location_type',
'associated_ccn',
'reh_conversion_date',
'proprietary_nonprofit',
"Multiple NPIs" = 'multiple_npi_flag',
Expand All @@ -266,6 +321,6 @@ hosp_cols <- function(df) {
"Subgroup Specialty Hospital" = 'subgroup_specialty_hospital',
"Subgroup Other" = 'subgroup_other')

df |> dplyr::select(dplyr::all_of(cols))
df |> dplyr::select(dplyr::any_of(cols))

}
12 changes: 6 additions & 6 deletions R/utilization.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ NULL
#' largest number of claims submitted
#' @param par < *boolean* > Identifies whether the provider participates in
#' Medicare and/or accepts assignment of Medicare allowed amounts
#' @param tidy < *boolean* > // _default:_ `TRUE` Tidy output
#' @param nest < *boolean* > // _default:_ `TRUE` Nest `hcpcs_detailed`,
#' @param tidy < *boolean* > // __default:__ `TRUE` Tidy output
#' @param nest < *boolean* > // __default:__ `TRUE` Nest `hcpcs_detailed`,
#' `demographics` and `conditions` columns
#' @rdname utilization
#' @autoglobal
Expand Down Expand Up @@ -436,8 +436,8 @@ prov_cols <- function(df) {
#' submitted on the claims is a:
#' + Facility (`"F"`): Hospital, Skilled Nursing Facility, etc.
#' + Non-facility (`"O"`): Office, Home, etc.
#' @param tidy < *boolean* > // _default:_ `TRUE` Tidy output
#' @param rbcs < *boolean* > // _default:_ `TRUE` Add Restructured BETOS
#' @param tidy < *boolean* > // __default:__ `TRUE` Tidy output
#' @param rbcs < *boolean* > // __default:__ `TRUE` Add Restructured BETOS
#' Classifications to HCPCS codes
#' @rdname utilization
#' @autoglobal
Expand Down Expand Up @@ -689,8 +689,8 @@ serv_cols2 <- function(df) {
#' submitted on the claims is a:
#' + Facility (`"F"`): Hospital, Skilled Nursing Facility, etc.
#' + Non-facility (`"O"`): Office, Home, etc.
#' @param tidy < *boolean* > // _default is_ `TRUE` Tidy output
#' @param rbcs < *boolean* > // _default is_ `TRUE` Add Restructured BETOS
#' @param tidy < *boolean* > // __default:__ `TRUE` Tidy output
#' @param rbcs < *boolean* > // __default:__ `TRUE` Add Restructured BETOS
#' Classifications to HCPCS codes
#' @rdname utilization
#' @autoglobal
Expand Down
7 changes: 5 additions & 2 deletions man/hospitals.Rd

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

6 changes: 3 additions & 3 deletions man/utilization.Rd

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

0 comments on commit 7442407

Please sign in to comment.