From 9955da6cae2b790b5c66f7a8cb7b53b0b8a3fd2d Mon Sep 17 00:00:00 2001
From: "Alcaraz, Josue {TBB~SOUTH SAN FRANCISCO}" <alcaraz.josue@gene.com>
Date: Tue, 16 Jul 2024 17:14:50 +0200
Subject: [PATCH 1/4] Added DM parameter and merge when SITEID variable exists

---
 R/check_lb_lbstresn_missing.R | 68 ++++++++++++++++++++++++-----------
 R/check_lb_lbstresu.R         | 47 ++++++++++++++++++------
 2 files changed, 84 insertions(+), 31 deletions(-)

diff --git a/R/check_lb_lbstresn_missing.R b/R/check_lb_lbstresn_missing.R
index bb500cf1..cd0f2510 100755
--- a/R/check_lb_lbstresn_missing.R
+++ b/R/check_lb_lbstresn_missing.R
@@ -1,10 +1,12 @@
 #' @title Check missing standard lab values (LBSTRESN/LBSTRESC)
 #'
 #' @description This check looks for missing standardized finding (LBSTRESN/LBSTRESC)
-#'              when original finding (LBORRES) is not missing
+#'              when original finding (LBORRES) is not missing. Merges with DM dataset
+#'              when DM$SITEID is present
 #'
 #' @param LB Lab SDTM dataset with variables USUBJID, LBTESTCD, LBDTC, LBORRES,
 #' LBORRESU, LBSTRESN, LBSTRESC, VISIT (optional), LBSPID (optional)
+#' @param DM Demographics SDTM with variables USUBJID, SITEID
 #' @param preproc An optional company specific preprocessing script
 #' @param ... Other arguments passed to methods
 #'
@@ -33,55 +35,79 @@
 #'  LBSTRESN  = c(5,6,NA),
 #'  stringsAsFactors=FALSE
 #'  )
+#'  
+#'  DM <- data.frame(
+#'  USUBJID = c("Patient 1","Patient 2","Patient 3"),
+#'  SITEID = c("123","124","125"),
+#'  stringsAsFactors=FALSE
+#'  )
+#'
+#'  DM2 <- data.frame(
+#'  USUBJID = c("Patient 1","Patient 2","Patient 3"),
+#'  stringsAsFactors=FALSE
+#'  )
 #'
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
 #'
 #' LB$LBSTRESC[3] = ""
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
+#' 
+#' LB$LBSTRESC[3] = ""
+#' check_lb_lbstresn_missing(LB, DM2)
 #'
 #' LB$LBSTRESC[1] = ""
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
 #'
 #' LB$VISIT = "SCREENING"
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
 #'
 #' LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 #' check_lb_lbstresn_missing(LB,preproc=roche_derive_rave_row)
 #'
 #' LB$LBSTRESN = NULL
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
 #'
 #' LB$LBSTRESC = NULL
-#' check_lb_lbstresn_missing(LB)
+#' check_lb_lbstresn_missing(LB, DM)
 #'
 
-check_lb_lbstresn_missing <- function(LB,preproc=identity,...){
-
+check_lb_lbstresn_missing <- function(LB, DM,preproc=identity,...){
+  
   if(LB %lacks_any% c("USUBJID", "LBTESTCD", "LBDTC", "LBORRES", "LBORRESU", "LBSTRESN", "LBSTRESC")){
-
+    
     fail(lacks_msg(LB, c("USUBJID", "LBTESTCD", "LBDTC", "LBORRES", "LBORRESU", "LBSTRESN", "LBSTRESC")))
-
+    
   }else{
-
+    
+    # Subset DM to fewer variables
+    DM <- DM %>%
+      select(any_of(c("USUBJID", "SITEID")))
+    
+    # Merge DM to LB if SITEID exists
+    if("SITEID" %in% names(DM)){
+      LB <- left_join(LB, DM, by="USUBJID")
+    }
+    
     #Apply company specific preprocessing function
     LB = preproc(LB,...)
-
+    
     # Subset LB to fewer variables
     LB <- LB %>%
-      select(any_of(c("USUBJID", "LBTESTCD", "LBDTC", "LBORRES", "LBORRESU", "LBSTRESN", "LBSTRESC","RAVE","VISIT")))
-
+      select(any_of(c("USUBJID", "LBTESTCD", "LBDTC", "LBORRES", "LBORRESU", 
+                      "LBSTRESN", "LBSTRESC", "RAVE", "VISIT", "SITEID" )))
+    
     # Subset to LBORRES populated but LBSTRESN not
     mydf <- subset(LB, !is_sas_na(LB$LBORRES) & is_sas_na(LB$LBSTRESN) & is_sas_na(LB$LBSTRESC))
-
+    
     if (nrow(mydf)==0){
       pass()
     }
     else if (nrow(mydf)>0) {
-        fail(paste0(length(unique(mydf$USUBJID)),
-                    " unique patient(s) with ",
-                    nrow(mydf),
-                    " lab record(s) with result reported without standard value. "),
-             mydf)
+      fail(paste0(length(unique(mydf$USUBJID)),
+                  " unique patient(s) with ",
+                  nrow(mydf),
+                  " lab record(s) with result reported without standard value. "),
+           mydf)
     }
   }
 }
diff --git a/R/check_lb_lbstresu.R b/R/check_lb_lbstresu.R
index c1e5357f..f0ebf2a7 100755
--- a/R/check_lb_lbstresu.R
+++ b/R/check_lb_lbstresu.R
@@ -3,11 +3,12 @@
 #' @description This check identifies records where original lab values (LBORRES)
 #' exist but standard lab units (LBSTRESU) are not populated, excluding
 #' qualitative results (LBMETHOD) and excluding records when LBTESTCD in
-#' ("PH" "SPGRAV")
+#' ("PH" "SPGRAV"). Merges with DM dataset when DM$SITEID is present
 #'
 #' @param LB Lab SDTM dataset with variables USUBJID, LBSTRESC, LBSTRESN,
 #'   LBORRES, LBSTRESU, LBTESTCD, LBDTC, LBMETHOD (optional),
 #'   LBSPID (optional), and VISIT (optional)
+#' @param DM Demographics SDTM with variables USUBJID, SITEID
 #' @param preproc An optional company specific preprocessing script
 #' @param ... Other arguments passed to methods
 #'
@@ -34,28 +35,43 @@
 #' stringsAsFactors=FALSE
 #' )
 #'
-#' check_lb_lbstresu(LB)
+#'  DM <- data.frame(
+#'  USUBJID = 1:10,
+#'  SITEID = 111:120,
+#'  stringsAsFactors=FALSE
+#'  )
+#'  
+#'  DM2 <- data.frame(
+#'  USUBJID = 1:10,
+#'  stringsAsFactors=FALSE
+#'  )
+#'
+#'
+#' check_lb_lbstresu(LB, DM)
 #'
 #' LB$LBSTRESU[1]=""
-#' check_lb_lbstresu(LB)
+#' check_lb_lbstresu(LB, DM)
+#' 
+#' LB$LBSTRESU[1]=""
+#' check_lb_lbstresu(LB, DM2)
 #'
 #' LB$LBSTRESU[2]="NA"
-#' check_lb_lbstresu(LB)
+#' check_lb_lbstresu(LB, DM)
 #'
 #' LB$LBSTRESU[3]=NA
-#' check_lb_lbstresu(LB)
+#' check_lb_lbstresu(LB, DM)
 #'
 #' LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
-#' check_lb_lbstresu(LB,preproc=roche_derive_rave_row)
+#' check_lb_lbstresu(LB, DM, preproc=roche_derive_rave_row)
 #'
 #' LB$VISIT= "SCREENING"
-#' check_lb_lbstresu(LB)
+#' check_lb_lbstresu(LB, DM)
 #'
 #' LB$LBSTRESU=NULL
-#' check_lb_lbstresu(LB)
+#' check_lb_lbstresu(LB, DM)
 #'
 
-check_lb_lbstresu <- function(LB,preproc=identity,...){
+check_lb_lbstresu <- function(LB, DM, preproc=identity,...){
     
     ###Check that required variables exist and return a message if they don't.
     if(LB %lacks_any% c("USUBJID", "LBSTRESC", "LBSTRESN", "LBSTRESU", "LBORRES",
@@ -64,6 +80,16 @@ check_lb_lbstresu <- function(LB,preproc=identity,...){
                              "LBTESTCD", "LBDTC")))
     } else{
         
+        # Subset DM to fewer variables
+        DM <- DM %>%
+            select(any_of(c("USUBJID", "SITEID")))
+        
+        # Merge DM to LB if SITEID exists
+        if("SITEID" %in% names(DM)){
+            LB <- left_join(LB, DM, by="USUBJID")
+        }
+        
+        
         #Apply company specific preprocessing function
         LB = preproc(LB,...)
         
@@ -82,7 +108,8 @@ check_lb_lbstresu <- function(LB,preproc=identity,...){
         
         # Subset LB to fewer variables
         df <- LB %>%
-            select(any_of(c('USUBJID','LBTESTCD','LBORRES','LBSTRESU','LBSTRESC','LBDTC','RAVE','VISIT')))
+            select(any_of(c('USUBJID','LBTESTCD','LBORRES','LBSTRESU','LBSTRESC','LBDTC',
+                            'RAVE','VISIT', 'SITEID')))
         
         ### Exclude particular labs known to be unitless
         df <- df %>%

From 3a8c97cb5c65225ca5effc8505cf86a42ea184a9 Mon Sep 17 00:00:00 2001
From: AlcJ123 <AlcJ123@users.noreply.github.com>
Date: Tue, 16 Jul 2024 15:22:25 +0000
Subject: [PATCH 2/4] Update documentation

---
 man/check_lb_lbstresn_missing.Rd | 33 ++++++++++++++++++++++--------
 man/check_lb_lbstresu.Rd         | 35 ++++++++++++++++++++++++--------
 2 files changed, 51 insertions(+), 17 deletions(-)

diff --git a/man/check_lb_lbstresn_missing.Rd b/man/check_lb_lbstresn_missing.Rd
index d3a0e66a..fd0bb46f 100644
--- a/man/check_lb_lbstresn_missing.Rd
+++ b/man/check_lb_lbstresn_missing.Rd
@@ -4,12 +4,14 @@
 \alias{check_lb_lbstresn_missing}
 \title{Check missing standard lab values (LBSTRESN/LBSTRESC)}
 \usage{
-check_lb_lbstresn_missing(LB, preproc = identity, ...)
+check_lb_lbstresn_missing(LB, DM, preproc = identity, ...)
 }
 \arguments{
 \item{LB}{Lab SDTM dataset with variables USUBJID, LBTESTCD, LBDTC, LBORRES,
 LBORRESU, LBSTRESN, LBSTRESC, VISIT (optional), LBSPID (optional)}
 
+\item{DM}{Demographics SDTM with variables USUBJID, SITEID}
+
 \item{preproc}{An optional company specific preprocessing script}
 
 \item{...}{Other arguments passed to methods}
@@ -20,7 +22,8 @@ test failed
 }
 \description{
 This check looks for missing standardized finding (LBSTRESN/LBSTRESC)
-when original finding (LBORRES) is not missing
+when original finding (LBORRES) is not missing. Merges with DM dataset
+when DM$SITEID is present
 }
 \examples{
 
@@ -35,26 +38,40 @@ LB <- data.frame(
  LBSTRESN  = c(5,6,NA),
  stringsAsFactors=FALSE
  )
+ 
+ DM <- data.frame(
+ USUBJID = c("Patient 1","Patient 2","Patient 3"),
+ SITEID = c("123","124","125"),
+ stringsAsFactors=FALSE
+ )
+
+ DM2 <- data.frame(
+ USUBJID = c("Patient 1","Patient 2","Patient 3"),
+ stringsAsFactors=FALSE
+ )
 
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM)
+
+LB$LBSTRESC[3] = ""
+check_lb_lbstresn_missing(LB, DM)
 
 LB$LBSTRESC[3] = ""
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM2)
 
 LB$LBSTRESC[1] = ""
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM)
 
 LB$VISIT = "SCREENING"
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM)
 
 LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 check_lb_lbstresn_missing(LB,preproc=roche_derive_rave_row)
 
 LB$LBSTRESN = NULL
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM)
 
 LB$LBSTRESC = NULL
-check_lb_lbstresn_missing(LB)
+check_lb_lbstresn_missing(LB, DM)
 
 }
 \author{
diff --git a/man/check_lb_lbstresu.Rd b/man/check_lb_lbstresu.Rd
index 54f224b9..ecee4a4e 100644
--- a/man/check_lb_lbstresu.Rd
+++ b/man/check_lb_lbstresu.Rd
@@ -4,13 +4,15 @@
 \alias{check_lb_lbstresu}
 \title{Check for missing lab units (LBSTRESU)}
 \usage{
-check_lb_lbstresu(LB, preproc = identity, ...)
+check_lb_lbstresu(LB, DM, preproc = identity, ...)
 }
 \arguments{
 \item{LB}{Lab SDTM dataset with variables USUBJID, LBSTRESC, LBSTRESN,
 LBORRES, LBSTRESU, LBTESTCD, LBDTC, LBMETHOD (optional),
 LBSPID (optional), and VISIT (optional)}
 
+\item{DM}{Demographics SDTM with variables USUBJID, SITEID}
+
 \item{preproc}{An optional company specific preprocessing script}
 
 \item{...}{Other arguments passed to methods}
@@ -23,7 +25,7 @@ test failed
 This check identifies records where original lab values (LBORRES)
 exist but standard lab units (LBSTRESU) are not populated, excluding
 qualitative results (LBMETHOD) and excluding records when LBTESTCD in
-("PH" "SPGRAV")
+("PH" "SPGRAV"). Merges with DM dataset when DM$SITEID is present
 }
 \examples{
 
@@ -38,25 +40,40 @@ LBDTC = 1:10,
 stringsAsFactors=FALSE
 )
 
-check_lb_lbstresu(LB)
+ DM <- data.frame(
+ USUBJID = 1:10,
+ SITEID = 111:120,
+ stringsAsFactors=FALSE
+ )
+ 
+ DM2 <- data.frame(
+ USUBJID = 1:10,
+ stringsAsFactors=FALSE
+ )
+
+
+check_lb_lbstresu(LB, DM)
+
+LB$LBSTRESU[1]=""
+check_lb_lbstresu(LB, DM)
 
 LB$LBSTRESU[1]=""
-check_lb_lbstresu(LB)
+check_lb_lbstresu(LB, DM2)
 
 LB$LBSTRESU[2]="NA"
-check_lb_lbstresu(LB)
+check_lb_lbstresu(LB, DM)
 
 LB$LBSTRESU[3]=NA
-check_lb_lbstresu(LB)
+check_lb_lbstresu(LB, DM)
 
 LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
-check_lb_lbstresu(LB,preproc=roche_derive_rave_row)
+check_lb_lbstresu(LB, DM, preproc=roche_derive_rave_row)
 
 LB$VISIT= "SCREENING"
-check_lb_lbstresu(LB)
+check_lb_lbstresu(LB, DM)
 
 LB$LBSTRESU=NULL
-check_lb_lbstresu(LB)
+check_lb_lbstresu(LB, DM)
 
 }
 \author{

From cdc46a466d3179402043ad63aef52cb176b17847 Mon Sep 17 00:00:00 2001
From: "Alcaraz, Josue {TBB~SOUTH SAN FRANCISCO}" <alcaraz.josue@gene.com>
Date: Thu, 25 Jul 2024 03:31:03 +0200
Subject: [PATCH 3/4] Default to DM=NULL, added logic for if !is.null(DM) for
 both functions. Created new unit checks for when DM has and does not have
 siteid variable.

---
 R/check_lb_lbstresn_missing.R                 |  30 +--
 R/check_lb_lbstresu.R                         |  36 +--
 .../testthat/test-check_lb_lbstresn_missing.R | 196 ++++++++++++++--
 tests/testthat/test-check_lb_lbstresu.R       | 215 +++++++++++++++---
 4 files changed, 406 insertions(+), 71 deletions(-)

diff --git a/R/check_lb_lbstresn_missing.R b/R/check_lb_lbstresn_missing.R
index cd0f2510..e2bf6461 100755
--- a/R/check_lb_lbstresn_missing.R
+++ b/R/check_lb_lbstresn_missing.R
@@ -6,7 +6,7 @@
 #'
 #' @param LB Lab SDTM dataset with variables USUBJID, LBTESTCD, LBDTC, LBORRES,
 #' LBORRESU, LBSTRESN, LBSTRESC, VISIT (optional), LBSPID (optional)
-#' @param DM Demographics SDTM with variables USUBJID, SITEID
+#' @param DM Demographics SDTM with variables USUBJID, SITEID. Set to NULL.
 #' @param preproc An optional company specific preprocessing script
 #' @param ... Other arguments passed to methods
 #'
@@ -47,31 +47,32 @@
 #'  stringsAsFactors=FALSE
 #'  )
 #'
-#' check_lb_lbstresn_missing(LB, DM)
+#' check_lb_lbstresn_missing(LB)
 #'
 #' LB$LBSTRESC[3] = ""
+#' check_lb_lbstresn_missing(LB)
+#' 
 #' check_lb_lbstresn_missing(LB, DM)
 #' 
-#' LB$LBSTRESC[3] = ""
 #' check_lb_lbstresn_missing(LB, DM2)
 #'
 #' LB$LBSTRESC[1] = ""
-#' check_lb_lbstresn_missing(LB, DM)
+#' check_lb_lbstresn_missing(LB)
 #'
 #' LB$VISIT = "SCREENING"
-#' check_lb_lbstresn_missing(LB, DM)
+#' check_lb_lbstresn_missing(LB)
 #'
 #' LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 #' check_lb_lbstresn_missing(LB,preproc=roche_derive_rave_row)
 #'
 #' LB$LBSTRESN = NULL
-#' check_lb_lbstresn_missing(LB, DM)
+#' check_lb_lbstresn_missing(LB)
 #'
 #' LB$LBSTRESC = NULL
-#' check_lb_lbstresn_missing(LB, DM)
+#' check_lb_lbstresn_missing(LB)
 #'
 
-check_lb_lbstresn_missing <- function(LB, DM,preproc=identity,...){
+check_lb_lbstresn_missing <- function(LB, DM = NULL,preproc=identity,...){
   
   if(LB %lacks_any% c("USUBJID", "LBTESTCD", "LBDTC", "LBORRES", "LBORRESU", "LBSTRESN", "LBSTRESC")){
     
@@ -79,12 +80,12 @@ check_lb_lbstresn_missing <- function(LB, DM,preproc=identity,...){
     
   }else{
     
-    # Subset DM to fewer variables
-    DM <- DM %>%
-      select(any_of(c("USUBJID", "SITEID")))
-    
-    # Merge DM to LB if SITEID exists
-    if("SITEID" %in% names(DM)){
+    # If DM is present, merge by USUBJID
+    if(!is.null(DM) & "SITEID" %in% names(DM)){
+      
+      DM <- DM %>%
+        select(any_of(c("USUBJID", "SITEID")))
+      
       LB <- left_join(LB, DM, by="USUBJID")
     }
     
@@ -111,3 +112,4 @@ check_lb_lbstresn_missing <- function(LB, DM,preproc=identity,...){
     }
   }
 }
+
diff --git a/R/check_lb_lbstresu.R b/R/check_lb_lbstresu.R
index f0ebf2a7..f17c04b9 100755
--- a/R/check_lb_lbstresu.R
+++ b/R/check_lb_lbstresu.R
@@ -8,7 +8,7 @@
 #' @param LB Lab SDTM dataset with variables USUBJID, LBSTRESC, LBSTRESN,
 #'   LBORRES, LBSTRESU, LBTESTCD, LBDTC, LBMETHOD (optional),
 #'   LBSPID (optional), and VISIT (optional)
-#' @param DM Demographics SDTM with variables USUBJID, SITEID
+#' @param DM Demographics SDTM with variables USUBJID, SITEID. Set to NULL.
 #' @param preproc An optional company specific preprocessing script
 #' @param ... Other arguments passed to methods
 #'
@@ -47,31 +47,35 @@
 #'  )
 #'
 #'
-#' check_lb_lbstresu(LB, DM)
+#' check_lb_lbstresu(LB)
 #'
 #' LB$LBSTRESU[1]=""
+#' check_lb_lbstresu(LB)
+#' 
+#' check_lb_lbstresu(LB, DM2)
+#' 
 #' check_lb_lbstresu(LB, DM)
 #' 
 #' LB$LBSTRESU[1]=""
-#' check_lb_lbstresu(LB, DM2)
+#' check_lb_lbstresu(LB)
 #'
 #' LB$LBSTRESU[2]="NA"
-#' check_lb_lbstresu(LB, DM)
+#' check_lb_lbstresu(LB)
 #'
 #' LB$LBSTRESU[3]=NA
-#' check_lb_lbstresu(LB, DM)
+#' check_lb_lbstresu(LB)
 #'
 #' LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 #' check_lb_lbstresu(LB, DM, preproc=roche_derive_rave_row)
 #'
 #' LB$VISIT= "SCREENING"
-#' check_lb_lbstresu(LB, DM)
+#' check_lb_lbstresu(LB)
 #'
 #' LB$LBSTRESU=NULL
-#' check_lb_lbstresu(LB, DM)
+#' check_lb_lbstresu(LB)
 #'
 
-check_lb_lbstresu <- function(LB, DM, preproc=identity,...){
+check_lb_lbstresu <- function(LB, DM = NULL, preproc=identity,...){
     
     ###Check that required variables exist and return a message if they don't.
     if(LB %lacks_any% c("USUBJID", "LBSTRESC", "LBSTRESN", "LBSTRESU", "LBORRES",
@@ -80,16 +84,15 @@ check_lb_lbstresu <- function(LB, DM, preproc=identity,...){
                              "LBTESTCD", "LBDTC")))
     } else{
         
-        # Subset DM to fewer variables
-        DM <- DM %>%
-            select(any_of(c("USUBJID", "SITEID")))
-        
-        # Merge DM to LB if SITEID exists
-        if("SITEID" %in% names(DM)){
+        # If DM is present, merge by USUBJID
+        if(!is.null(DM) & "SITEID" %in% names(DM)){
+            
+            DM <- DM %>%
+                select(any_of(c("USUBJID", "SITEID")))
+            
             LB <- left_join(LB, DM, by="USUBJID")
         }
         
-        
         #Apply company specific preprocessing function
         LB = preproc(LB,...)
         
@@ -133,4 +136,5 @@ check_lb_lbstresu <- function(LB, DM, preproc=identity,...){
         
     }
     
-}
\ No newline at end of file
+}
+
diff --git a/tests/testthat/test-check_lb_lbstresn_missing.R b/tests/testthat/test-check_lb_lbstresn_missing.R
index d0ea716d..28faba12 100644
--- a/tests/testthat/test-check_lb_lbstresn_missing.R
+++ b/tests/testthat/test-check_lb_lbstresn_missing.R
@@ -1,20 +1,22 @@
 test_that("Function returns true when no errors are present", {
   
   LB <- data.frame(
-   USUBJID = c("Patient 1","Patient 2","Patient 3"),
-   LBTEST   = "Test A",
-   LBTESTCD   = "TA",
-   LBDTC   = "2017-01-01",
-   LBORRES = c("5","6","7"),
-   LBSTRESC = c("5","6","7"),
-   LBORRESU = rep("mg",3),
-   LBSTRESN  = c(5,6,NA),
-   stringsAsFactors=FALSE
-   )
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
   
   expect_true(check_lb_lbstresn_missing(LB))
 })
 
+
+
 test_that("Function returns false when errors are present", {
   
   LB <- data.frame(
@@ -39,10 +41,87 @@ test_that("Function returns false when errors are present", {
 
 
 
+test_that("Function returns false when expected column not present", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESC=NULL
+  LB$LBSTRESN=NULL
+  
+  expect_false(check_lb_lbstresn_missing(LB))
+  
+})
 
 
-test_that("Function returns false when expected column not present", {
 
+test_that("Function returns true when no errors are present, DM is present with SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  DM <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    SITEID = c("123","124","125"),
+    stringsAsFactors=FALSE
+  )
+  
+  expect_true(check_lb_lbstresn_missing(LB, DM))
+})
+
+
+
+test_that("Function returns false when errors are present, DM is present with SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  DM <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    SITEID = c("123","124","125"),
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESC[3] = ""
+  LB$LBSTRESC[1] = ""
+  LB$VISIT = "SCREENING"
+  
+  
+  
+  expect_false(check_lb_lbstresn_missing(LB, DM))
+  
+})
+
+
+test_that("Function returns false when expected column not present, DM is present with SITEID variable", {
+  
   LB <- data.frame(
     USUBJID = c("Patient 1","Patient 2","Patient 3"),
     LBTEST   = "Test A",
@@ -55,10 +134,99 @@ test_that("Function returns false when expected column not present", {
     stringsAsFactors=FALSE
   )
   
+  DM <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    SITEID = c("123","124","125"),
+    stringsAsFactors=FALSE
+  )
+  
   LB$LBSTRESC=NULL
   LB$LBSTRESN=NULL
   
-  expect_false(check_lb_lbstresn_missing(LB))
-    
-  })
+  expect_false(check_lb_lbstresn_missing(LB, DM))
+  
+})
+
+
+
+test_that("Function returns true when no errors are present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  DM2 <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    stringsAsFactors=FALSE
+  )
+  
+  expect_true(check_lb_lbstresn_missing(LB, DM2))
+})
+
+
+
+test_that("Function returns false when errors are present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  DM2 <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESC[3] = ""
+  LB$LBSTRESC[1] = ""
+  LB$VISIT = "SCREENING"
+  
+  expect_false(check_lb_lbstresn_missing(LB, DM2))
+  
+})
+
+
+
+test_that("Function returns false when expected column not present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    LBTEST   = "Test A",
+    LBTESTCD   = "TA",
+    LBDTC   = "2017-01-01",
+    LBORRES = c("5","6","7"),
+    LBSTRESC = c("5","6","7"),
+    LBORRESU = rep("mg",3),
+    LBSTRESN  = c(5,6,NA),
+    stringsAsFactors=FALSE
+  )
+  
+  DM2 <- data.frame(
+    USUBJID = c("Patient 1","Patient 2","Patient 3"),
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESC=NULL
+  LB$LBSTRESN=NULL
+  
+  expect_false(check_lb_lbstresn_missing(LB,DM2))
+  
+})
+
+
 
diff --git a/tests/testthat/test-check_lb_lbstresu.R b/tests/testthat/test-check_lb_lbstresu.R
index 1c8564f2..be835fad 100644
--- a/tests/testthat/test-check_lb_lbstresu.R
+++ b/tests/testthat/test-check_lb_lbstresu.R
@@ -1,20 +1,22 @@
 test_that("Function returns true when no errors are present", {
   
   LB <- data.frame(
-      USUBJID = 1:10,
-      LBSTRESC = "5",
-      LBSTRESN = 1:10,
-      LBORRES = "5",
-      LBSTRESU = "g/L",
-      LBTESTCD = "ALB",
-      LBDTC = 1:10,
-      stringsAsFactors=FALSE
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
   )
   
   
   expect_true(check_lb_lbstresu(LB))
 })
 
+
+
 test_that("Function returns false when errors are present", {
   
   LB <- data.frame(
@@ -28,9 +30,9 @@ test_that("Function returns false when errors are present", {
     stringsAsFactors=FALSE
   )
   
- LB$LBSTRESU[1]=""
- LB$LBSTRESU[2]="NA"
- LB$LBSTRESU[3]=NA
+  LB$LBSTRESU[1]=""
+  LB$LBSTRESU[2]="NA"
+  LB$LBSTRESU[3]=NA
   
   expect_false(check_lb_lbstresu(LB))
   
@@ -39,21 +41,180 @@ test_that("Function returns false when errors are present", {
 
 
 test_that("Function returns false when expected column not present", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESU=NULL
+  
+  expect_false(check_lb_lbstresu(LB))
+  
+})
+
+
+
+test_that("Function returns true when no errors are present, DM is present with SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  DM <- data.frame(
+    USUBJID = 1:10,
+    SITEID = 111:120,
+    stringsAsFactors=FALSE
+  )
+  
+  expect_true(check_lb_lbstresu(LB, DM))
+})
+
+
+
+test_that("Function returns false when errors are present, DM is present with SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  DM <- data.frame(
+    USUBJID = 1:10,
+    SITEID = 111:120,
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESU[1]=""
+  LB$LBSTRESU[2]="NA"
+  LB$LBSTRESU[3]=NA
+  
+  expect_false(check_lb_lbstresu(LB, DM))
+  
+})
+
+
+
+test_that("Function returns false when expected column not present, DM is present with SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  DM <- data.frame(
+    USUBJID = 1:10,
+    SITEID = 111:120,
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESU=NULL
+  
+  expect_false(check_lb_lbstresu(LB, DM))
+  
+})
+
+
+
+test_that("Function returns true when no errors are present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  DM2 <- data.frame(
+    USUBJID = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  expect_true(check_lb_lbstresu(LB, DM2))
+})
+
+
+
+test_that("Function returns false when errors are present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESU[1]=""
+  LB$LBSTRESU[2]="NA"
+  LB$LBSTRESU[3]=NA
+  
+  DM2 <- data.frame(
+    USUBJID = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  expect_false(check_lb_lbstresu(LB, DM2))
+  
+})
+
+
+
+test_that("Function returns false when expected column not present, DM is present without SITEID variable", {
+  
+  LB <- data.frame(
+    USUBJID = 1:10,
+    LBSTRESC = "5",
+    LBSTRESN = 1:10,
+    LBORRES = "5",
+    LBSTRESU = "g/L",
+    LBTESTCD = "ALB",
+    LBDTC = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  LB$LBSTRESU=NULL
+  
+  DM2 <- data.frame(
+    USUBJID = 1:10,
+    stringsAsFactors=FALSE
+  )
+  
+  expect_false(check_lb_lbstresu(LB, DM2))
+  
+})
+
 
-    LB <- data.frame(
-      USUBJID = 1:10,
-      LBSTRESC = "5",
-      LBSTRESN = 1:10,
-      LBORRES = "5",
-      LBSTRESU = "g/L",
-      LBTESTCD = "ALB",
-      LBDTC = 1:10,
-      stringsAsFactors=FALSE
-    )
-    
-    LB$LBSTRESU=NULL
-    
-    expect_false(check_lb_lbstresu(LB))
-    
-  })
 

From 83f45bfb88a76d206e85ad6f14ba4bffdb244dc4 Mon Sep 17 00:00:00 2001
From: AlcJ123 <AlcJ123@users.noreply.github.com>
Date: Thu, 25 Jul 2024 01:36:54 +0000
Subject: [PATCH 4/4] Update documentation

---
 man/check_lb_lbstresn_missing.Rd | 17 +++++++++--------
 man/check_lb_lbstresu.Rd         | 20 ++++++++++++--------
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/man/check_lb_lbstresn_missing.Rd b/man/check_lb_lbstresn_missing.Rd
index fd0bb46f..2e4616d4 100644
--- a/man/check_lb_lbstresn_missing.Rd
+++ b/man/check_lb_lbstresn_missing.Rd
@@ -4,13 +4,13 @@
 \alias{check_lb_lbstresn_missing}
 \title{Check missing standard lab values (LBSTRESN/LBSTRESC)}
 \usage{
-check_lb_lbstresn_missing(LB, DM, preproc = identity, ...)
+check_lb_lbstresn_missing(LB, DM = NULL, preproc = identity, ...)
 }
 \arguments{
 \item{LB}{Lab SDTM dataset with variables USUBJID, LBTESTCD, LBDTC, LBORRES,
 LBORRESU, LBSTRESN, LBSTRESC, VISIT (optional), LBSPID (optional)}
 
-\item{DM}{Demographics SDTM with variables USUBJID, SITEID}
+\item{DM}{Demographics SDTM with variables USUBJID, SITEID. Set to NULL.}
 
 \item{preproc}{An optional company specific preprocessing script}
 
@@ -50,28 +50,29 @@ LB <- data.frame(
  stringsAsFactors=FALSE
  )
 
-check_lb_lbstresn_missing(LB, DM)
+check_lb_lbstresn_missing(LB)
 
 LB$LBSTRESC[3] = ""
+check_lb_lbstresn_missing(LB)
+
 check_lb_lbstresn_missing(LB, DM)
 
-LB$LBSTRESC[3] = ""
 check_lb_lbstresn_missing(LB, DM2)
 
 LB$LBSTRESC[1] = ""
-check_lb_lbstresn_missing(LB, DM)
+check_lb_lbstresn_missing(LB)
 
 LB$VISIT = "SCREENING"
-check_lb_lbstresn_missing(LB, DM)
+check_lb_lbstresn_missing(LB)
 
 LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 check_lb_lbstresn_missing(LB,preproc=roche_derive_rave_row)
 
 LB$LBSTRESN = NULL
-check_lb_lbstresn_missing(LB, DM)
+check_lb_lbstresn_missing(LB)
 
 LB$LBSTRESC = NULL
-check_lb_lbstresn_missing(LB, DM)
+check_lb_lbstresn_missing(LB)
 
 }
 \author{
diff --git a/man/check_lb_lbstresu.Rd b/man/check_lb_lbstresu.Rd
index ecee4a4e..6834d75f 100644
--- a/man/check_lb_lbstresu.Rd
+++ b/man/check_lb_lbstresu.Rd
@@ -4,14 +4,14 @@
 \alias{check_lb_lbstresu}
 \title{Check for missing lab units (LBSTRESU)}
 \usage{
-check_lb_lbstresu(LB, DM, preproc = identity, ...)
+check_lb_lbstresu(LB, DM = NULL, preproc = identity, ...)
 }
 \arguments{
 \item{LB}{Lab SDTM dataset with variables USUBJID, LBSTRESC, LBSTRESN,
 LBORRES, LBSTRESU, LBTESTCD, LBDTC, LBMETHOD (optional),
 LBSPID (optional), and VISIT (optional)}
 
-\item{DM}{Demographics SDTM with variables USUBJID, SITEID}
+\item{DM}{Demographics SDTM with variables USUBJID, SITEID. Set to NULL.}
 
 \item{preproc}{An optional company specific preprocessing script}
 
@@ -52,28 +52,32 @@ stringsAsFactors=FALSE
  )
 
 
-check_lb_lbstresu(LB, DM)
+check_lb_lbstresu(LB)
 
 LB$LBSTRESU[1]=""
+check_lb_lbstresu(LB)
+
+check_lb_lbstresu(LB, DM2)
+
 check_lb_lbstresu(LB, DM)
 
 LB$LBSTRESU[1]=""
-check_lb_lbstresu(LB, DM2)
+check_lb_lbstresu(LB)
 
 LB$LBSTRESU[2]="NA"
-check_lb_lbstresu(LB, DM)
+check_lb_lbstresu(LB)
 
 LB$LBSTRESU[3]=NA
-check_lb_lbstresu(LB, DM)
+check_lb_lbstresu(LB)
 
 LB$LBSPID= "FORMNAME-R:2/L:2XXXX"
 check_lb_lbstresu(LB, DM, preproc=roche_derive_rave_row)
 
 LB$VISIT= "SCREENING"
-check_lb_lbstresu(LB, DM)
+check_lb_lbstresu(LB)
 
 LB$LBSTRESU=NULL
-check_lb_lbstresu(LB, DM)
+check_lb_lbstresu(LB)
 
 }
 \author{