## ----include = FALSE----------------------------------------------------------
# Vignettes use precomputed example data by default.
# To rebuild examples with live RxNorm/RxClass API calls, set:
# Sys.setenv(RXREF_BUILD_VIGNETTES_ONLINE = "true")

online_env <- identical(
  tolower(Sys.getenv("RXREF_BUILD_VIGNETTES_ONLINE")),
  "true"
)

has_net <- tryCatch({
  requireNamespace("curl", quietly = TRUE) && curl::has_internet()
}, error = function(e) FALSE)

run_live <- online_env && has_net

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(rxref)
library(dplyr)

read_rxref_example <- function(file) {
  path <- system.file("extdata", file, package = "rxref")
  if (!nzchar(path)) {
    stop(
      "The example data file '", file, "' was not found. ",
      "Reinstall rxref or rebuild the vignette with ",
      "RXREF_BUILD_VIGNETTES_ONLINE=true."
    )
  }
  readRDS(path)
}

## ----eval = FALSE-------------------------------------------------------------
# Sys.setenv(RXREF_BUILD_VIGNETTES_ONLINE = "true")

## ----define-ingredients-------------------------------------------------------
glp1.names <- c(
  "semaglutide",
  "exenatide",
  "liraglutide",
  "lixisenatide",
  "dulaglutide",
  "albiglutide",
  "tirzepatide"
)

## ----ings---------------------------------------------------------------------
if (run_live) {
  glp1.ings <- find_ingredients(glp1.names) |>
    filter(tty == "IN") |>
    distinct(
      input,
      ingredient_rxcui = rxcui,
      ingredient_name = name,
      ingredient_tty = tty
    )
} else {
  glp1.ings <- read_rxref_example("glp1_ings.rds")
}

glp1.ings

## ----tty-sets-----------------------------------------------------------------
product_ttys("default")
product_ttys("extended_product")

## ----prods--------------------------------------------------------------------
if (run_live) {
  glp1.prods <- products_for_ingredients(
    glp1.ings$ingredient_rxcui,
    ttys = product_ttys("default"),
    include_combos = TRUE,
    concept_status = "active"
  )
} else {
  glp1.prods <- read_rxref_example("glp1_prods.rds")
}

glp1.prods |>
  head(30)

## ----historical-products, eval = FALSE----------------------------------------
# glp1.prods_historical <- products_for_ingredients(
#   glp1.ings$ingredient_rxcui,
#   ttys = product_ttys("default"),
#   include_combos = TRUE,
#   concept_status = "active_and_historical"
# )

## ----ndcs---------------------------------------------------------------------
if (run_live) {
  glp1.ndc.map <- map_rxcui_to_ndc(
    unique(glp1.prods$product_rxcui),
    status = "ACTIVE"
  )
} else {
  glp1.ndc.map <- read_rxref_example("glp1_ndc_map.rds")
}

glp1.ndcs <- glp1.ndc.map |>
  left_join(
    glp1.prods,
    by = c("rxcui" = "product_rxcui")
  ) |>
  left_join(
    glp1.ings |>
      select(ingredient_rxcui, ingredient_name),
    by = "ingredient_rxcui"
  ) |>
  distinct(
    ingredient_rxcui,
    ingredient_name,
    product_rxcui = rxcui,
    ndc11,
    ndc_status,
    name,
    tty
  ) |>
  arrange(ingredient_name, product_rxcui, ndc11)

glp1.ndcs |>
  head(30)

## ----search-------------------------------------------------------------------
if (run_live) {
  alt.glp1.ndcs <- search_drug(
    term = glp1.names,
    return = "ndc",
    concept_status = "active",
    ndc_status = c("ACTIVE", "OBSOLETE", "UNSPECIFIED")
  )
} else {
  alt.glp1.ndcs <- read_rxref_example("alt_glp1_ndc.rds")
}

alt.glp1.ndcs |>
  arrange(ingredient_name, product_rxcui, ndc11) |>
  head(30)

## ----search-historical, eval = FALSE------------------------------------------
# search_drug(
#   term = glp1.names,
#   return = "ndc",
#   concept_status = "active_and_historical",
#   ndc_status = c("ACTIVE", "OBSOLETE", "UNSPECIFIED")
# )

## ----check--------------------------------------------------------------------
glp1.ndcs |>
  filter(!is.na(ndc11)) |>
  arrange(ingredient_name, product_rxcui, ndc11) |>
  head(30)

## ----status-example, eval = FALSE---------------------------------------------
# search_drug(
#   term = "semaglutide",
#   return = "ndc",
#   concept_status = "active_and_historical",
#   ndc_status = c("ACTIVE", "OBSOLETE", "UNSPECIFIED")
# )

