Skip to content

Commit

Permalink
moved search_taxonomy from northstar
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Apr 9, 2024
1 parent c434b42 commit 88b56b5
Show file tree
Hide file tree
Showing 16 changed files with 463 additions and 3 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Imports:
zeallot
Suggests:
covr,
fs,
fuimus,
data.table,
fipio,
fontawesome,
Expand Down Expand Up @@ -70,6 +72,7 @@ Suggests:
VignetteBuilder:
knitr
Remotes:
github::andrewallenbruce/fuimus,
github::anthonynorth/roxyglobals,
github::nt-williams/rxnorm,
github::rstudio/gt,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export(ror)
export(rx_years)
export(rxnorm)
export(summary_stats)
export(taxonomies)
export(taxonomy_codes)
export(taxonomy_crosswalk)
export(tidyup)
Expand Down
1 change: 1 addition & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ utils::globalVariables(c(
"individual_state_code", # <reassignments>
"group_state_code", # <reassignments>
"record_type", # <reassignments>
"hierarchy", # <taxonomies>
"taxonomy_code", # <taxonomy_codes>
"taxonomy_category", # <taxonomy_codes>
"taxonomy_grouping", # <taxonomy_codes>
Expand Down
150 changes: 150 additions & 0 deletions R/taxonomies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#' NUCC Provider Taxonomy Codeset
#'
#' `search_taxonomy()` returns a [tibble()] of the current Health Care Provider
#' Taxonomy code set
#'
#' __Update Frequency__: _Biannually_
#'
#' @section Taxonomy Codes:
#' The Health Care Provider Taxonomy code set is a collection of unique
#' alphanumeric codes, ten characters in length. They contain no embedded logic.
#'
#' The code set Levels are organized to allow for drilling down to the
#' provider's most specific level of specialization, with three distinct "Levels":
#'
#' @section __Level I__ _Provider Grouping_:
#' A major grouping of service(s) or occupation(s) of health care providers.
#'
#' _Examples:_ Allopathic & Osteopathic Physicians, Dental Providers, Hospitals
#'
#' @section __Level II__ _Classification_:
#' A more specific service or occupation related to the Provider Grouping.
#'
#' For example, the classification for Allopathic & Osteopathic Physicians is
#' based upon the General Specialty Certificates as issued by the appropriate
#' national boards.
#'
#' The following boards will, however, have their general certificates appear as
#' Level III Areas of specialization strictly due to display limitations of the
#' code set for Boards that have multiple general certificates:
#'
#' _Medical Genetics, Preventive Medicine, Psychiatry & Neurology, Radiology,_
#' _Surgery, Otolaryngology, Pathology_
#'
#' @section __Level III__ _Area of Specialization_:
#' A more specialized area of the Classification in which a provider chooses to
#' practice or make services available.
#'
#' For example, the area of specialization for Allopathic & Osteopathic
#' Physicians is based upon the Subspecialty Certificates as issued by the
#' appropriate national boards.
#'
#' @section __Categories__ _(Level 0)_:
#' The code set includes three specialty categories:
#'
#' __Group (of Individuals)__:
#' 1. Multi-Specialty
#' 1. Single Specialty
#'
#' __Individuals__:
#' 1. Allopathic & Osteopathic Physicians
#' 1. Behavioral Health and Social Service Providers
#' 1. Chiropractic Providers
#' 1. Dental Providers
#' 1. Dietary and Nutritional Service Providers
#' 1. Emergency Medical Service Providers
#' 1. Eye and Vision Service Providers
#' 1. Nursing Service Providers
#' 1. Nursing Service Related Providers
#' 1. Other Service Providers
#' 1. Pharmacy Service Providers
#' 1. Physician Assistants and Advanced Practice Nursing Providers
#' 1. Podiatric Medicine and Surgery Service Providers
#' 1. Respiratory, Developmental, Rehabilitative and Restorative Service Providers
#' 1. Speech, Language and Hearing Service Providers
#' 1. Student, Health Care
#' 1. Technologists, Technicians, and Other Technical Service Providers
#'
#' __Non-Individuals__:
#' 1. Agencies
#' 1. Ambulatory Health Care Facilities
#' 1. Hospital Units
#' 1. Hospitals
#' 1. Laboratories
#' 1. Managed Care Organizations
#' 1. Nursing and Custodial Care Facilities
#' 1. Other Service Providers
#' 1. Residential Treatment Facilities
#' 1. Respite Care Facilities
#' 1. Suppliers
#' 1. Transportation Services
#'
#' @section Display Name:
#' The display name is a combination of the code name and the Level in
#' which the code is nested, which more precisely identifies the code.
#'
#' For example, Addiction Medicine is a physician subspecialty in Anesthesiology,
#' Family Medicine, Internal Medicine, Preventive Medicine, and Psychiatry &
#' Neurology.
#'
#' "Addiction Medicine" does not identify the specialty of
#' the physician, but the display name of "Addiction Medicine (Internal
#' Medicine) Physician" clearly does.
#'
#' In another example, "Radiology" could be confused with several
#' taxonomies, but "Radiology Chiropractor" more accurately specifies the provider.
#'
#' @section Description:
#' + `code`: Provider Taxonomy Code
#' + `category`: Indicates whether Taxonomy is Individual or Non-Individual, i.e., a group taxonomy
#' + `grouping`: Level I, Provider Grouping
#' + `classification`: Level II, Classification
#' + `specialization`: Level III, Area of Specialization
#' + `display_name`: Consumer-friendly taxonomy name, made of the code name and the Level in which the code is nested.
#' + `definition`: Definition of Taxonomy
#' + `version`: Three digit version of the code set. The first two digits indicate the year and the third digit indicates either the first release of the year ("0") or the second release of the year ("1").
#' + `release_date`: Date the version of the code set was released
#'
#' @source
#' [National Uniform Claim Committee](https://www.nucc.org/index.php/code-sets-mainmenu-41/provider-taxonomy-mainmenu-40/csv-mainmenu-57)
#'
#' @param code `<chr>` vector of taxonomy codes
#'
#' @param shape `<chr>` shape of data to return, `wide` (default) or `long`
#'
#' @param unnest `<lgl>`unnest `hierarchy` column, default is `FALSE`
#'
#' @template args-dots
#'
#' @template returns
#'
#' @examples
#' taxonomies(code = c("207K00000X", "193200000X"))
#'
#' taxonomies(code = "207K00000X",
#' shape = "long",
#' unnest = TRUE)
#'
#' @autoglobal
#'
#' @export
taxonomies <- function(code = NULL,
shape = c('wide', 'long'),
unnest = FALSE,
...) {

shape <- match.arg(shape)

txn <- switch(
shape,
"wide" = pins::pin_read(mount_board(), "taxonomy"),
"long" = pins::pin_read(mount_board(), "taxlong")
)

txn <- fuimus::search_in_if(txn, txn$code, code)

if (shape == "long" && unnest) {
txn <- tidyr::unnest(txn, cols = c(hierarchy))
}
return(txn)
}
2 changes: 1 addition & 1 deletion R/taxonomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
taxonomy_codes <- function(shape = c('wide', 'long')) {

results <- pins::board_url(
github_raw("andrewallenbruce/provider/main/pkgdown/assets/pins-board/")) |>
fuimus::gh_raw("andrewallenbruce/provider/main/pkgdown/assets/pins-board/")) |>
pins::pin_read("taxonomy_codes")

shape <- match.arg(shape)
Expand Down
25 changes: 25 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,28 @@ format_cli <- function(df) {
wrap = TRUE)

}

#' Mount [pins][pins::pins-package] board
#'
#' @param source `"local"` or `"remote"`
#'
#' @return `<pins_board_folder>` or `<pins_board_url>`
#'
#' @noRd
mount_board <- function(source = c("local", "remote")) {

source <- match.arg(source)

switch(
source,
local = pins::board_folder(
fs::path_package(
"extdata/pins",
package = "provider")
),
remote = pins::board_url(
fuimus::gh_raw(
"andrewallenbruce/provider/master/inst/extdata/pins/")
)
)
}
5 changes: 5 additions & 0 deletions data-raw/taxonomy_codeset.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ nucc <- provider::download_nucc_csv()

board <- pins::board_folder(here::here("pkgdown/assets/pins-board"))

board2 <- pins::board_folder(here::here("pkgdown/assets/pins-board"))
pins::pin_search(board2)
pins::pin_delete(board2, names = ("taxlong"))


board |> pins::pin_write(nucc,
name = "taxonomy_codes",
description = "NUCC Health Care Provider Taxonomy code set",
Expand Down
90 changes: 90 additions & 0 deletions data-raw/taxonomy_new.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
x <- rvest::session("https://www.nucc.org") |>
rvest::session_follow_link("Code Sets") |>
rvest::session_follow_link("Taxonomy") |>
rvest::session_follow_link("CSV") |>
rvest::html_elements("a") |>
rvest::html_attr("href") |>
stringr::str_subset("taxonomy") |>
stringr::str_subset("csv")

x <- rvest::session(paste0("https://www.nucc.org", x)) |>
rvest::session_follow_link("Version")

x <- x$response$url

taxonomy <- data.table::fread(x) |>
dplyr::tibble() |>
janitor::clean_names() |>
dplyr::mutate(
dplyr::across(dplyr::everything(), ~ dplyr::na_if(., "")),
dplyr::across(dplyr::everything(), ~ stringr::str_squish(.))) |>
dplyr::mutate(version = as.character(240),
release_date = lubridate::ymd("2024-01-01"))
taxonomy <- taxonomy |>
dplyr::select(
code,
display_name,
section,
grouping,
classification,
specialization,
definition,
notes,
version,
release_date
)
# https://www.nucc.org/images/stories/CSV/nucc_taxonomy_240.csv
# "2023-07-01"
# "2024-01-01"

info <- taxonomy |>
dplyr::select(code, display_name, definition, notes)

long <- taxonomy |>
dplyr::select(code,
section,
grouping,
classification,
specialization) |>
dplyr::mutate(section_level = 0, .before = section) |>
dplyr::mutate(grouping_level = 1, .before = grouping) |>
dplyr::mutate(classification_level = 2, .before = classification) |>
dplyr::mutate(specialization_level = 3, .before = specialization) |>
tidyr::unite("section", section:section_level, remove = TRUE) |>
tidyr::unite("grouping", grouping:grouping_level, remove = TRUE) |>
tidyr::unite("classification", classification:classification_level, remove = TRUE, na.rm = TRUE) |>
tidyr::unite("specialization", specialization:specialization_level, remove = TRUE, na.rm = TRUE) |>
tidyr::pivot_longer(!code, names_to = "level", values_to = "description") |>
dplyr::filter(description != "3") |>
tidyr::separate_wider_delim(description, delim = "_", names = c("description", "group")) |>
dplyr::mutate(group = NULL,
level = factor(level,
levels = c("section", "grouping", "classification", "specialization"),
labels = c("I. Section", "II. Grouping", "III. Classification", "IV. Specialization"),
ordered = TRUE))

longnest <- long |>
dplyr::left_join(info, by = "code") |>
tidyr::nest(hierarchy = c(level, description)) |>
dplyr::select(code, display_name, hierarchy, definition, notes)

# Update Pin
board <- pins::board_folder(here::here("inst/extdata/pins"))

board |>
pins::pin_write(taxonomy,
name = "taxonomy",
title = "Provider Taxonomy Code Set",
description = "NUCC Health Care Provider Taxonomy Code Set January 2024",
type = "qs")

board |>
pins::pin_write(longnest,
name = "taxlong",
title = "Provider Taxonomy Code Set",
description = "NUCC Health Care Provider Taxonomy Code Set January 2024",
type = "qs")

board |> pins::write_board_manifest()

# pins::pin_delete(board, "tax_long")
4 changes: 4 additions & 0 deletions inst/extdata/pins/_pins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
taxlong:
- taxlong/20240409T213644Z-972c7/
taxonomy:
- taxonomy/20240409T213637Z-1a818/
10 changes: 10 additions & 0 deletions inst/extdata/pins/taxlong/20240409T213644Z-972c7/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
file: taxlong.qs
file_size: 105576
pin_hash: 972c745ebfbd01bf
type: qs
title: Provider Taxonomy Code Set
description: NUCC Health Care Provider Taxonomy Code Set January 2024
tags: ~
urls: ~
created: 20240409T213644Z
api_version: 1
Binary file not shown.
10 changes: 10 additions & 0 deletions inst/extdata/pins/taxonomy/20240409T213637Z-1a818/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
file: taxonomy.qs
file_size: 100601
pin_hash: 1a81842c74dcf89c
type: qs
title: Provider Taxonomy Code Set
description: NUCC Health Care Provider Taxonomy Code Set January 2024
tags: ~
urls: ~
created: 20240409T213637Z
api_version: 1
Binary file not shown.
Loading

0 comments on commit 88b56b5

Please sign in to comment.