diff --git a/.Rbuildignore b/.Rbuildignore index ec2b02aa4a..c7366abf79 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -31,3 +31,5 @@ ^\.devcontainer$ ^vignettes/articles$ ^inst/cheatsheet$ +^data-raw$ +^doc$ diff --git a/.gitignore b/.gitignore index 2a6f8564ba..db7075c835 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ admiral*.tgz ._.DS_Store .Rprofile +/doc/ +/Meta/ diff --git a/.lycheeignore b/.lycheeignore index be3b4cf525..bcb04df144 100644 --- a/.lycheeignore +++ b/.lycheeignore @@ -7,3 +7,4 @@ https://raw.githubusercontent.com/pharmaverse/admiral/main/inst/cheatsheet/cheat https://rsc.niaid.nih.gov/clinical-research-sites/daids-adverse-event-grading-tables https://rsc.niaid.nih.gov/sites/default/files/daidsgradingcorrectedv21.pdf https://www.fda.gov/media/116737/download + diff --git a/NEWS.md b/NEWS.md index 7bb95dc688..91fb0405c8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -42,6 +42,8 @@
Developer Notes +- Adopted `data-raw/data` R Package Convention (#2427) +
# admiral 1.1.1 diff --git a/data-backup/admiral_adlb.rda b/data-backup/admiral_adlb.rda new file mode 100644 index 0000000000..9bf8663e65 Binary files /dev/null and b/data-backup/admiral_adlb.rda differ diff --git a/data-backup/admiral_adsl.rda b/data-backup/admiral_adsl.rda new file mode 100644 index 0000000000..79f5921fdb Binary files /dev/null and b/data-backup/admiral_adsl.rda differ diff --git a/data-raw/adlb_grading/adlb_grading_spec.xlsx b/data-raw/adlb_grading/adlb_grading_spec.xlsx new file mode 100644 index 0000000000..426c5b4951 Binary files /dev/null and b/data-raw/adlb_grading/adlb_grading_spec.xlsx differ diff --git a/data-raw/adlb_grading/atoxgr_sources.R b/data-raw/adlb_grading/atoxgr_sources.R new file mode 100644 index 0000000000..90c833891a --- /dev/null +++ b/data-raw/adlb_grading/atoxgr_sources.R @@ -0,0 +1,22 @@ +atoxgr_criteria <- system.file("adlb_grading/adlb_grading_spec.xlsx", package = "admiral") +# Contrary to our usual convention the use of `::` here is explicit. This way we +# avoid having to list {readxl} in "Imports" and instead get away with just +# listing it in "Depends". + +atoxgr_criteria_ctcv4 <- atoxgr_criteria %>% + readxl::read_excel(sheet = "NCICTCAEv4") %>% + dplyr::mutate(GRADE_CRITERIA_CODE = gsub("[\r\n]", " ", GRADE_CRITERIA_CODE)) + +usethis::use_data(atoxgr_criteria_ctcv4, overwrite = TRUE) + +atoxgr_criteria_ctcv5 <- atoxgr_criteria %>% + readxl::read_excel(sheet = "NCICTCAEv5") %>% + dplyr::mutate(GRADE_CRITERIA_CODE = gsub("[\r\n]", " ", GRADE_CRITERIA_CODE)) + +use_data(atoxgr_criteria_ctcv5, overwrite = TRUE) + +atoxgr_criteria_daids <- atoxgr_criteria %>% + readxl::read_excel(sheet = "DAIDS") %>% + dplyr::mutate(GRADE_CRITERIA_CODE = gsub("[\r\n]", " ", GRADE_CRITERIA_CODE)) + +use_data(atoxgr_criteria_daids, overwrite = TRUE) diff --git a/data-raw/admiral_adlb.R b/data-raw/admiral_adlb.R new file mode 100644 index 0000000000..f07345368b --- /dev/null +++ b/data-raw/admiral_adlb.R @@ -0,0 +1,466 @@ +# Name: ADLB +# +# Label: Lab Analysis Dataset +# +# Input: adsl, lb +# nolint start: undesirable_function_linter +library(admiral) +library(pharmaversesdtm) # Contains example datasets from the CDISC pilot project +library(dplyr) +library(lubridate) +library(stringr) +# nolint end + +# Load source datasets ---- + +# Use e.g. haven::read_sas to read in .sas7bdat, or other suitable functions +# as needed and assign to the variables below. +# For illustration purposes read in admiral test data + +data("lb") +data("admiral_adsl") + +adsl <- admiral_adsl + +# When SAS datasets are imported into R using haven::read_sas(), missing +# character values from SAS appear as "" characters in R, instead of appearing +# as NA values. Further details can be obtained via the following link: +# https://pharmaverse.github.io/admiral/articles/admiral.html#handling-of-missing-values # nolint + +lb <- convert_blanks_to_na(lb) + +# Look-up tables ---- + +# Assign PARAMCD, PARAM, and PARAMN +param_lookup <- tibble::tribble( + ~LBTESTCD, ~PARAMCD, ~PARAM, ~PARAMN, + "ALB", "ALB", "Albumin (g/L)", 1, + "ALP", "ALKPH", "Alkaline Phosphatase (U/L)", 2, + "ALT", "ALT", "Alanine Aminotransferase (U/L)", 3, + "ANISO", "ANISO", "Anisocytes", 4, + "AST", "AST", "Aspartate Aminotransferase (U/L)", 5, + "BASO", "BASO", "Basophils Abs (10^9/L)", 6, + "BASOLE", "BASOLE", "Basophils/Leukocytes (FRACTION)", 7, + "BILI", "BILI", "Bilirubin (umol/L)", 8, + "BUN", "BUN", "Blood Urea Nitrogen (mmol/L)", 9, + "CA", "CA", "Calcium (mmol/L)", 10, + "CHOL", "CHOLES", "Cholesterol (mmol/L)", 11, + "CK", "CK", "Creatinine Kinase (U/L)", 12, + "CL", "CL", "Chloride (mmol/L)", 13, + "COLOR", "COLOR", "Color", 14, + "CREAT", "CREAT", "Creatinine (umol/L)", 15, + "EOS", "EOS", "Eosinophils (10^9/L)", 16, + "EOSLE", "EOSLE", "Eosinophils/Leukocytes (FRACTION)", 17, + "GGT", "GGT", "Gamma Glutamyl Transferase (U/L)", 18, + "GLUC", "GLUC", "Glucose (mmol/L)", 19, + "HBA1C", "HBA1C", "Hemoglobin A1C (1)", 20, + "HCT", "HCT", "Hematocrit (1)", 21, + "HGB", "HGB", "Hemoglobin (mmol/L)", 22, + "K", "POTAS", "Potassium (mmol/L)", 23, + "KETONES", "KETON", "Ketones", 24, + "LYM", "LYMPH", "Lymphocytes Abs (10^9/L)", 25, + "LYMLE", "LYMPHLE", "Lymphocytes/Leukocytes (FRACTION)", 26, + "MACROCY", "MACROC", "Macrocytes", 27, + "MCH", "MCH", "Ery. Mean Corpuscular Hemoglobin (fmol(Fe))", 28, + "MCHC", "MCHC", "Ery. Mean Corpuscular HGB Concentration (mmol/L)", 29, + "MCV", "MCV", "Ery. Mean Corpuscular Volume (f/L)", 30, + "MICROCY", "MICROC", "Microcytes", 31, + "MONO", "MONO", "Monocytes (10^9/L)", 32, + "MONOLE", "MONOLE", "Monocytes/Leukocytes (FRACTION)", 33, + "PH", "PH", "pH", 34, + "PHOS", "PHOS", "Phosphate (mmol/L)", 35, + "PLAT", "PLAT", "Platelet (10^9/L)", 36, + "POIKILO", "POIKIL", "Poikilocytes", 37, + "POLYCHR", "POLYCH", "Polychromasia", 38, + "PROT", "PROT", "Protein (g/L)", 39, + "RBC", "RBC", "Erythrocytes (TI/L)", 40, + "SODIUM", "SODIUM", "Sodium (mmol/L)", 41, + "SPGRAV", "SPGRAV", "Specific Gravity", 42, + "TSH", "TSH", "Thyrotropin (mU/L)", 43, + "URATE", "URATE", "Urate (umol/L)", 44, + "UROBIL", "UROBIL", "Urobilinogen", 45, + "VITB12", "VITB12", "Vitamin B12 (pmol/L)", 46, + "WBC", "WBC", "Leukocytes (10^9/L)", 47 +) + + +# Derivations ---- + +# Get list of ADSL vars required for derivations +adsl_vars <- exprs(TRTSDT, TRTEDT, TRT01A, TRT01P) + +adlb <- lb %>% + # Join ADSL with LB (need TRTSDT for ADY derivation) + derive_vars_merged( + dataset_add = adsl, + new_vars = adsl_vars, + by_vars = exprs(STUDYID, USUBJID) + ) %>% + ## Calculate ADT, ADY ---- + derive_vars_dt( + new_vars_prefix = "A", + dtc = LBDTC + ) %>% + derive_vars_dy(reference_date = TRTSDT, source_vars = exprs(ADT)) + +adlb <- adlb %>% + ## Add PARAMCD PARAM and PARAMN - from LOOK-UP table ---- + # Replace with PARAMCD lookup function + derive_vars_merged_lookup( + dataset_add = param_lookup, + new_vars = exprs(PARAMCD, PARAM, PARAMN), + by_vars = exprs(LBTESTCD), + check_type = "none", + print_not_mapped = FALSE + ) %>% + ## Calculate PARCAT1 AVAL AVALC ANRLO ANRHI ---- + mutate( + PARCAT1 = LBCAT, + AVAL = LBSTRESN, + # Only populate AVALC if character value is non-redundant with AVAL + AVALC = ifelse( + is.na(LBSTRESN) | as.character(LBSTRESN) != LBSTRESC, + LBSTRESC, + NA + ), + ANRLO = LBSTNRLO, + ANRHI = LBSTNRHI + ) + +# Derive Absolute values from fractional Differentials using WBC +# Only derive where absolute values do not already exist +# Need to populate ANRLO and ANRHI for newly created records +adlb <- adlb %>% + # Derive absolute Basophils + derive_param_wbc_abs( + by_vars = exprs(STUDYID, USUBJID, !!!adsl_vars, DOMAIN, VISIT, VISITNUM, ADT, ADY), + set_values_to = exprs( + PARAMCD = "BASO", + PARAM = "Basophils Abs (10^9/L)", + PARAMN = 6, + DTYPE = "CALCULATION", + PARCAT1 = "HEMATOLOGY" + ), + get_unit_expr = extract_unit(PARAM), + diff_code = "BASOLE" + ) %>% + # Derive absolute Lymphocytes + derive_param_wbc_abs( + by_vars = exprs(STUDYID, USUBJID, !!!adsl_vars, DOMAIN, VISIT, VISITNUM, ADT, ADY), + set_values_to = exprs( + PARAMCD = "LYMPH", + PARAM = "Lymphocytes Abs (10^9/L)", + PARAMN = 25, + DTYPE = "CALCULATION", + PARCAT1 = "HEMATOLOGY" + ), + get_unit_expr = extract_unit(PARAM), + diff_code = "LYMPHLE" + ) + +## Get Visit Info ---- +# See also the "Visit and Period Variables" vignette +# (https://pharmaverse.github.io/admiral/articles/visits_periods.html#visits) +adlb <- adlb %>% + # Derive Timing + mutate( + AVISIT = case_when( + str_detect(VISIT, "SCREEN") ~ "Baseline", + !is.na(VISIT) ~ str_to_title(VISIT), + TRUE ~ NA_character_ + ), + AVISITN = case_when( + AVISIT == "Baseline" ~ 0, + !is.na(VISITNUM) ~ VISITNUM + ) + ) + +adlb <- adlb %>% + ## Calculate ONTRTFL ---- + derive_var_ontrtfl( + start_date = ADT, + ref_start_date = TRTSDT, + ref_end_date = TRTEDT, + filter_pre_timepoint = AVISIT == "Baseline" + ) + +## Calculate ANRIND : requires the reference ranges ANRLO, ANRHI ---- +adlb <- adlb %>% + derive_var_anrind() + +## Derive baseline flags ---- +adlb <- adlb %>% + # Calculate BASETYPE + mutate( + BASETYPE = "LAST" + ) %>% + # Calculate ABLFL + restrict_derivation( + derivation = derive_var_extreme_flag, + args = params( + by_vars = exprs(STUDYID, USUBJID, BASETYPE, PARAMCD), + order = exprs(ADT, VISITNUM, LBSEQ), + new_var = ABLFL, + mode = "last" + ), + filter = (!is.na(AVAL) & ADT <= TRTSDT & !is.na(BASETYPE)) + ) + +## Derive baseline information ---- +adlb <- adlb %>% + # Calculate BASE + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = AVAL, + new_var = BASE + ) %>% + # Calculate BASEC + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = AVALC, + new_var = BASEC + ) %>% + # Calculate BNRIND + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = ANRIND, + new_var = BNRIND + ) %>% # nolint start + # Calculate CHG for post-baseline records + # The decision on how to populate pre-baseline and baseline values of CHG is left to producer choice + # nolint end + restrict_derivation( + derivation = derive_var_chg, + filter = AVISITN > 0 + ) %>% # nolint start + # Calculate PCHG for post-baseline records + # The decision on how to populate pre-baseline and baseline values of PCHG is left to producer choice + # nolint end + restrict_derivation( + derivation = derive_var_pchg, + filter = AVISITN > 0 + ) + +## Calculate lab grading ---- + +# Assign ATOXDSCL and ATOXDSCH to hold lab grading terms +# ATOXDSCL and ATOXDSCH hold terms defined by NCI-CTCAEv4. +# See (https://pharmaverse.github.io/admiral/articles/lab_grading.html#implement_ctcv4) +grade_lookup <- tibble::tribble( + ~PARAMCD, ~ATOXDSCL, ~ATOXDSCH, + "ALB", "Hypoalbuminemia", NA_character_, + "ALKPH", NA_character_, "Alkaline phosphatase increased", + "ALT", NA_character_, "Alanine aminotransferase increased", + "AST", NA_character_, "Aspartate aminotransferase increased", + "BILI", NA_character_, "Blood bilirubin increased", + "CA", "Hypocalcemia", "Hypercalcemia", + "CHOLES", NA_character_, "Cholesterol high", + "CK", NA_character_, "CPK increased", + "CREAT", NA_character_, "Creatinine increased", + "GGT", NA_character_, "GGT increased", + "GLUC", "Hypoglycemia", "Hyperglycemia", + "HGB", "Anemia", "Hemoglobin increased", + "POTAS", "Hypokalemia", "Hyperkalemia", + "LYMPH", "CD4 lymphocytes decreased", NA_character_, + "PHOS", "Hypophosphatemia", NA_character_, + "PLAT", "Platelet count decreased", NA_character_, + "SODIUM", "Hyponatremia", "Hypernatremia", + "WBC", "White blood cell decreased", "Leukocytosis", +) + +# Assign grade criteria +# metadata atoxgr_criteria_ctcv4 used to implement NCI-CTCAEv4 +# user could change to atoxgr_criteria_ctcv5 to implement NCI-CTCAEv5 +# Note: Hyperglycemia and Hypophosphatemia not defined in NCI-CTCAEv5 so +# user would need to amend look-up table grade_lookup +# See (https://pharmaverse.github.io/admiral/articles/lab_grading.html#implement_ctcv5) +grade_crit <- atoxgr_criteria_ctcv4 + + +# Add ATOXDSCL and ATOXDSCH +adlb <- adlb %>% + derive_vars_merged( + dataset_add = grade_lookup, + by_vars = exprs(PARAMCD) + ) %>% + # Derive toxicity grade for low values ATOXGRL + + derive_var_atoxgr_dir( + meta_criteria = grade_crit, + new_var = ATOXGRL, + tox_description_var = ATOXDSCL, + criteria_direction = "L", + get_unit_expr = extract_unit(PARAM) + ) %>% + # Derive toxicity grade for low values ATOXGRH + # default metadata atoxgr_criteria_ctcv4 used + derive_var_atoxgr_dir( + meta_criteria = grade_crit, + new_var = ATOXGRH, + tox_description_var = ATOXDSCH, + criteria_direction = "H", + get_unit_expr = extract_unit(PARAM) + ) %>% + # (Optional) derive overall grade ATOXGR (combining ATOXGRL and ATOXGRH) + derive_var_atoxgr() %>% + # Derive baseline toxicity grade for low values BTOXGRL + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = ATOXGRL, + new_var = BTOXGRL + ) %>% + # Derive baseline toxicity grade for high values BTOXGRH + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = ATOXGRH, + new_var = BTOXGRH + ) %>% + # Derive baseline toxicity grade for for overall grade BTOXGR + derive_var_base( + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + source_var = ATOXGR, + new_var = BTOXGR + ) + + +## Calculate R2BASE, R2ANRLO and R2ANRHI ---- +adlb <- adlb %>% + derive_var_analysis_ratio( + numer_var = AVAL, + denom_var = BASE + ) %>% + derive_var_analysis_ratio( + numer_var = AVAL, + denom_var = ANRLO + ) %>% + derive_var_analysis_ratio( + numer_var = AVAL, + denom_var = ANRHI + ) + +## SHIFT derivation ---- +adlb <- adlb %>% + # Derive shift from baseline for analysis indicator + derive_var_shift( + new_var = SHIFT1, + from_var = BNRIND, + to_var = ANRIND + ) %>% + # Derive shift from baseline for overall grade + restrict_derivation( + derivation = derive_var_shift, + args = params( + new_var = SHIFT2, + from_var = BTOXGR, + to_var = ATOXGR + ), + filter = !is.na(ATOXDSCL) | !is.na(ATOXDSCH) + ) + +## Flag variables (ANL01FL, LVOTFL) ---- +# ANL01FL: Flag last result within an AVISIT for post-baseline records +# LVOTFL: Flag last valid on-treatment record +adlb <- adlb %>% + restrict_derivation( + derivation = derive_var_extreme_flag, + args = params( + by_vars = exprs(USUBJID, PARAMCD, AVISIT), + order = exprs(ADT, AVAL), + new_var = ANL01FL, + mode = "last" + ), + filter = !is.na(AVISITN) & ONTRTFL == "Y" + ) %>% + restrict_derivation( + derivation = derive_var_extreme_flag, + args = params( + by_vars = exprs(USUBJID, PARAMCD), + order = exprs(ADT, AVAL), + new_var = LVOTFL, + mode = "last" + ), + filter = ONTRTFL == "Y" + ) + +## Get treatment information ---- +# See also the "Visit and Period Variables" vignette +# (https://pharmaverse.github.io/admiral/articles/visits_periods.html#treatment_bds) +adlb <- adlb %>% + # Assign TRTA, TRTP + mutate( + TRTP = TRT01P, + TRTA = TRT01A + ) + +## Get extreme values ---- +adlb <- adlb %>% + # get MINIMUM value + derive_extreme_records( + dataset_add = adlb, + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + order = exprs(AVAL, ADT, AVISITN), + mode = "first", + filter_add = (!is.na(AVAL) & ONTRTFL == "Y"), + set_values_to = exprs( + AVISITN = 9997, + AVISIT = "POST-BASELINE MINIMUM", + DTYPE = "MINIMUM" + ) + ) %>% + # get MAXIMUM value + derive_extreme_records( + dataset_add = adlb, + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + order = exprs(desc(AVAL), ADT, AVISITN), + mode = "first", + filter_add = (!is.na(AVAL) & ONTRTFL == "Y"), + set_values_to = exprs( + AVISITN = 9998, + AVISIT = "POST-BASELINE MAXIMUM", + DTYPE = "MAXIMUM" + ) + ) %>% + # get LOV value + derive_extreme_records( + dataset_add = adlb, + by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), + order = exprs(ADT, AVISITN), + mode = "last", + filter_add = (ONTRTFL == "Y"), + set_values_to = exprs( + AVISITN = 9999, + AVISIT = "POST-BASELINE LAST", + DTYPE = "LOV" + ) + ) + +## Get ASEQ ---- +adlb <- adlb %>% + # Calculate ASEQ + derive_var_obs_number( + new_var = ASEQ, + by_vars = exprs(STUDYID, USUBJID), + order = exprs(PARAMCD, ADT, AVISITN, VISITNUM), + check_type = "error" + ) + +# Add all ADSL variables +adlb <- adlb %>% + derive_vars_merged( + dataset_add = select(adsl, !!!negate_vars(adsl_vars)), + by_vars = exprs(STUDYID, USUBJID) + ) + +# Final Steps, Select final variables and Add labels +# This process will be based on your metadata, no example given for this reason +# ... + +# Save output ---- + +# Change to whichever directory you want to save the dataset in +dir <- tools::R_user_dir("admiral_templates_data", which = "cache") +if (!file.exists(dir)) { + # Create the folder + dir.create(dir, recursive = TRUE, showWarnings = FALSE) +} +save(adlb, file = file.path(dir, "adlb.rda"), compress = "bzip2") diff --git a/data-raw/create_admiral_adlb.R b/data-raw/create_admiral_adlb.R new file mode 100644 index 0000000000..779d912e17 --- /dev/null +++ b/data-raw/create_admiral_adlb.R @@ -0,0 +1,111 @@ +# Create dataset: data/admiral_adlb.rda +# This script: create_admiral_adlb.R creates dataset data/admiral_adlb.rda. +# + +# Preliminary +library(diffdf) # nolint + +# To clarify directories (can be removed) +# nolint start: object_name_linter +CACHE_DIR <- "~/.cache/R/admiral_templates_data/" +DATA_DIR <- "data-dir" +DATA_RAW <- "data-raw" +TEMPLATE_DIR <- "inst/templates/" + +# clean CACHE_DIR +THE_FILE <- paste0(CACHE_DIR, "/adlb.rda") +THE_FILE <- paste0(CACHE_DIR, "/adsl.rda") +if (file.exists(THE_FILE)) file.remove(THE_FILE) +# nolint end + +# +# STEPS +# +# First, use template to create the R script (in data-raw/admiral_adlb.R). +# Next, source this script and create the data (~/.cache/R/admiral_template_data/admiral_adlb.rda) +# Finally, shorten this data (now ~ 1.2 MB) by selecting only certain USUBJID + +# orignal method - OMIT +if (FALSE) { + # ### original mehtod (method 1) + # # First, create the R script (from a template) + adam_name <- "adlb" + save_path <- paste0("./data-raw/admiral_", adam_name, ".R") + use_ad_template( + adam_name = adam_name, + save_path = save_path, + open = FALSE, + overwrite = TRUE + ) + # Second, source the script and save data in .cache + source("data-raw/admiral_adlb.R") # nolint + load("~/.cache/R/admiral_templates_data/adlb.rda") +} + +# +# Instead, USE template, as recommened by Buzz +# +source(paste0(TEMPLATE_DIR, "/ad_adlb.R")) # nolint +load(paste0(CACHE_DIR, "adlb.rda")) + +# +# limit rows, by selecting only these USUBJID +# +#' 01-701-1015, 01-701-1023, 01-701-1028, 01-701-1033, +#' 01-701-1034, 01-701-1047, 01-701-1097, 01-705-1186, +#' 01-705-1292, 01-705-1310, 01-708-1286 + +usubjid <- + c( + "01-701-1015", + "01-701-1023", + "01-701-1028", + "01-701-1033", + "01-701-1034", + "01-701-1047", + "01-701-1097", + "01-705-1186", + "01-705-1292", + "01-705-1310", + "01-708-1286" + ) + +# prepare for inner join +user <- tibble( + USUBJID = usubjid +) +result <- inner_join(adlb, user) +admiral_adlb <- result +admiral_adlb + +# +# Finally, save reduced ds +# +use_data(admiral_adlb, overwrite = TRUE) + +# +# TEST - is dataset identical to .... backup of unaltered dataset +# +e1 <- new.env() +e2 <- new.env() +load("data/admiral_adlb.rda", e1) + +# CHANGE to YOUR location of original dataset +load("data-backup/admiral_adlb.rda", e2) + +# compare field names +t <- tibble(e1 = names(e1$admiral_adlb), e2 = names(e2$admiral_adlb)) +t |> print(n = 111) + +identical(e1$admiral_adlb, e2$admiral_adlb) +diffdf(e1$admiral_adlb, e2$admiral_adlb) + +## Capture diffdf to file + +capture.output( + diffdf( + compare = e1$admiral_adlb, base = e2$admiral_adlb, + keys = c("STUDYID", "DOMAIN", "USUBJID", "AVAL", "VISIT") + ), + file = "data-raw/diffdf_adlb_23SEPT" +) diff --git a/data-raw/create_admiral_adsl.R b/data-raw/create_admiral_adsl.R new file mode 100644 index 0000000000..2649141e10 --- /dev/null +++ b/data-raw/create_admiral_adsl.R @@ -0,0 +1,78 @@ +# Create dataset: data/admiral_adsl.rda +# This script: create_admiral_adsl.R creates dataset data/admiral_adsl.rda. +# + +# Preliminary +library(diffdf) # nolint + + +# nolint start: object_name_linter. +# To clarify directories (can be removed) +CACHE_DIR <- "~/.cache/R/admiral_templates_data/" +DATA_DIR <- "data-dir" +DATA_RAW <- "data-raw" +TEMPLATE_DIR <- "inst/templates/" + +# clean CACHE_DIR +THE_FILE <- paste0(CACHE_DIR, "/adlb.rda") +THE_FILE <- paste0(CACHE_DIR, "/adsl.rda") +if (file.exists(THE_FILE)) file.remove(THE_FILE) +# nolint end + +# +# STEPS +# +# First, use template to create the R script (in data-raw/admiral_adlb.R). +# Next, source this script and create the data (~/.cache/R/admiral_template_data/admiral_adlb.rda) +# Finally, shorten this data (now ~ 1.2 MB) by selecting only certain USUBJID + +# OMIT -- orignal method - OMIT +if (FALSE) { + # ### original mehtod (method 1) + # # First, create the R script (from a template) + adam_name <- "adlb" + save_path <- paste0("./data-raw/admiral_", adam_name, ".R") + use_ad_template( + adam_name = adam_name, + save_path = save_path, + open = FALSE, + overwrite = TRUE + ) + # Second, source the script and save data in .cache + source("data-raw/admiral_adlb.R") # nolint + load("~/.cache/R/admiral_templates_data/adlb.rda") +} + +# +# +# Instead, USE template, as recommened by Buzz +# +source(paste0(TEMPLATE_DIR, "/ad_adsl.R")) # nolint +load(paste0(CACHE_DIR, "adsl.rda")) +admiral_adsl <- adsl + +# +# Finally, save reduced ds +# +use_data(admiral_adsl, overwrite = TRUE) + +# +# TEST - is dataset identical to .... backup of unaltered dataset +# +e1 <- new.env() +e2 <- new.env() +load("data/admiral_adsl.rda", e1) + +# CHANGE to YOUR location of original dataset +load("data-backup/admiral_adsl.rda", e2) + + +identical(e1$admiral_adsl, e2$admiral_adsl) +diffdf(compare = e1$admiral_adsl, base = e2$admiral_adsl, keys = c("STUDYID", "USUBJID")) +capture.output( + diffdf( + compare = e1$admiral_adsl, base = e2$admiral_adsl, + keys = c("STUDYID", "USUBJID") + ), + file = "data-raw/diffdf_23SEPT" +) diff --git a/inst/example_scripts/derive_single_dose.R b/data-raw/derive_single_dose.R similarity index 93% rename from inst/example_scripts/derive_single_dose.R rename to data-raw/derive_single_dose.R index d840653bdf..ff7cc9d29a 100644 --- a/inst/example_scripts/derive_single_dose.R +++ b/data-raw/derive_single_dose.R @@ -1,6 +1,9 @@ +# nolint start: undersiderable_function_linter library(pharmaversesdtm) library(admiral) library(dplyr) +# nolint end + ex <- pharmaversesdtm::ex # check that there is only one start/end date of exposure per subject and visit @@ -52,4 +55,4 @@ attr(ex_single$EXSTDTC, "label") <- attr(ex$EXSTDTC, "label") attr(ex_single$EXENDTC, "label") <- attr(ex$EXENDTC, "label") attr(ex_single$EXDOSFRQ, "label") <- attr(ex$EXDOSFRQ, "label") -save(ex_single, file = file.path("data", "ex_single.rda"), compress = "bzip2") +usethis::use_data(ex_single, overwrite = TRUE) diff --git a/inst/example_scripts/example_qs.R b/data-raw/example_qs.R similarity index 98% rename from inst/example_scripts/example_qs.R rename to data-raw/example_qs.R index f6c6e7ab3a..1286e93352 100644 --- a/inst/example_scripts/example_qs.R +++ b/data-raw/example_qs.R @@ -1,4 +1,8 @@ -library(dplyr) +## Code to create `example_qs.rda` dataset and save it in data/ +## This replaces inst/example_scripts/example_qs.R +## Follows the same standard as pharmaversesdtm + + # nolint start suppress_warning( qs_gad7 <- tibble::tribble( @@ -202,5 +206,5 @@ example_qs <- bind_rows(qs_gad7, qs_gdssf, qs_sp) %>% STUDYID = "STUDYX", .before = everything() ) - -save(example_qs, file = file.path("data", "example_qs.rda"), compress = "bzip2") +# create example_qs.rda in data/ +usethis::use_data(example_qs, overwrite = TRUE) diff --git a/inst/example_scripts/example_query_source.R b/data-raw/example_query_source.R similarity index 100% rename from inst/example_scripts/example_query_source.R rename to data-raw/example_query_source.R diff --git a/data/admiral_adlb.rda b/data/admiral_adlb.rda index 9bf8663e65..485940b76b 100644 Binary files a/data/admiral_adlb.rda and b/data/admiral_adlb.rda differ diff --git a/data/admiral_adsl.rda b/data/admiral_adsl.rda index 79f5921fdb..061a959380 100644 Binary files a/data/admiral_adsl.rda and b/data/admiral_adsl.rda differ diff --git a/data/atoxgr_criteria_ctcv4.rda b/data/atoxgr_criteria_ctcv4.rda index eee2737efb..35c00d7a94 100644 Binary files a/data/atoxgr_criteria_ctcv4.rda and b/data/atoxgr_criteria_ctcv4.rda differ diff --git a/data/atoxgr_criteria_ctcv5.rda b/data/atoxgr_criteria_ctcv5.rda index 1671095608..5d4df887e2 100644 Binary files a/data/atoxgr_criteria_ctcv5.rda and b/data/atoxgr_criteria_ctcv5.rda differ diff --git a/data/atoxgr_criteria_daids.rda b/data/atoxgr_criteria_daids.rda index 17aee71787..31934ec248 100644 Binary files a/data/atoxgr_criteria_daids.rda and b/data/atoxgr_criteria_daids.rda differ diff --git a/data/ex_single.rda b/data/ex_single.rda index ae359ca0db..c0db6a6d81 100644 Binary files a/data/ex_single.rda and b/data/ex_single.rda differ diff --git a/data/example_qs.rda b/data/example_qs.rda index df81153271..7274ba7665 100644 Binary files a/data/example_qs.rda and b/data/example_qs.rda differ diff --git a/inst/WORDLIST b/inst/WORDLIST index 23746554ac..e3a009b79f 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -189,6 +189,7 @@ Pharmaverse Phosphatase Pooja Pre +Premarketing QD QID README diff --git a/man/admiral_adsl.Rd b/man/admiral_adsl.Rd index dabde0e714..dc73f839aa 100644 --- a/man/admiral_adsl.Rd +++ b/man/admiral_adsl.Rd @@ -5,7 +5,7 @@ \alias{admiral_adsl} \title{Subject Level Analysis Dataset} \format{ -An object of class \code{tbl_df} (inherits from \code{tbl}, \code{data.frame}) with 306 rows and 50 columns. +An object of class \code{tbl_df} (inherits from \code{tbl}, \code{data.frame}) with 306 rows and 54 columns. } \source{ Derived from the \code{dm} and \code{ds} datasets using \code{{admiral}} (\url{https://github.com/pharmaverse/admiral/blob/main/inst/templates/ad_adsl.R}) diff --git a/vignettes/hys_law.Rmd b/vignettes/hys_law.Rmd index 2f6bdea729..ad67a0a475 100644 --- a/vignettes/hys_law.Rmd +++ b/vignettes/hys_law.Rmd @@ -13,12 +13,15 @@ knitr::opts_chunk$set( comment = "#>" ) +library(admiral) library(admiraldev) ``` # Introduction -During the drug development process, clinical trials are often required to assess for the potential that the experimental drug can cause severe liver injury, known as a drug induced liver injury [(DILI)](https://www.fda.gov/media/116737/download). There are multiple criteria that need to be evaluated to determine and classify a DILI "Event". Hy's Law, a common rule of thumb for a DILI Event , is usually comprised of three parts: +During the drug development process, clinical trials are often required to assess for the potential that the experimental drug can cause severe liver injury, known as a drug induced liver injury (DILI) [Drug-Induced Liver Injury: +Premarketing Clinical +Evaluation](https://www.fda.gov/media/116737/download). There are multiple criteria that need to be evaluated to determine and classify a DILI "Event". Hy's Law, a common rule of thumb for a DILI Event , is usually comprised of three parts: - Elevated alanine aminotransferase (ALT) or aspartate aminotransferase (AST) by 3-times or greater of the upper limit of normal. - Elevated serum total bilirubin (BILI) by 2-times or greater within a window of time, ~14 days after the elevated ALT/AST event.