knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, 
                      dpi = 600, 
                      fig.width = 6, 
                      fig.height = 5)
# Name of subfolder: pktable

Observedincluded <- any(str_detect(MyPKResults$Statistic, "S/O"))
if(FromCalcPKRatios){

   highlight_so_cutoffs = NA
   highlight_so_colors = "yellow to red"

   if(paired){
      OptionalSection <- knitr::knit_child(
         system.file("rmarkdown/templates/pairedci/skeleton/skeleton.Rmd",
                     package="SimcypConsultancy"), 
         envir = environment(), 
         quiet = TRUE)

      cat(OptionalSection, sep = "\n")

   } else {
      OptionalSection <- knitr::knit_child(
         system.file("rmarkdown/templates/unpairedci/skeleton/skeleton.Rmd",
                     package="SimcypConsultancy"), 
         envir = environment(), 
         quiet = TRUE)

      cat(OptionalSection, sep = "\n")

   }

   tissues <- str_comma(unique(PKparameters$Tissue))
   compoundsToExtract <- str_comma(unique(PKparameters$CompoundID))

} 

r Annotations[["table_heading"]]

# Adding a warning if there was a mismatch between the last dosing interval and
# the AUC interval if any of the PK included last dose.

if(any(str_detect(CheckDoseInt$message, "mismatch")) & 
   single_table){

   DoseIntWarning <- list(
      "***WARNING: There was a mismatch between the dosing interval and the AUC interval for the following files and compounds:***\n", 

      CheckDoseInt$interval[which(str_detect(CheckDoseInt$message, "mismatch")), ] %>% 
         mutate(TimeLastInterval = ifelse(OneDoseIntRemaining == TRUE, 
                                          0, SimDuration - LastDoseTime)) %>% 
         select(File, Sheet, CompoundID, DoseInt_X, UserIntervalRemaining, TimeLastInterval) %>% 
         rename("Dosing interval" = DoseInt_X, 
                "Last-dose interval (h)" = TimeLastInterval, 
                "User-defined interval (h)" = UserIntervalRemaining) %>% 
         flextable::flextable() %>% 
         flextable::width(j = 1:2, width = 1.5) %>% 
         flextable::width(j = 3, width = 1), 

      "***Please check these results carefully.***")

} else {

   DoseIntWarning <- list(
      c(), c(), c()
   )
}

r DoseIntWarning[[1]]

r DoseIntWarning[[2]]

r DoseIntWarning[[3]]

# Setting things up for nonstandard evaluation 
shading_column <- rlang::enquo(shading_column)

if("File" %in% names(MyPKResults)){
   MyPKResults <- MyPKResults %>% 
      relocate(File, .after = last_col())
}

if(single_table){

   # If they only wanted 1 tissue, don't include a column for that in the table.
   # It'll be listed in the text and takes up room needlessly.
   if(length(unique(MyPKResults$Tissue)) == 1 &&
      str_detect(unique(MyPKResults$Tissue), " / ") == FALSE){
      MyPKResults <- MyPKResults %>% 
         select(-any_of("Tissue"))
   }

   # Similarly, if they only wanted 1 compoundID, don't include a column for that
   # in the table.
   if(length(unique(MyPKResults$CompoundID)) == 1 &&
      str_detect(unique(MyPKResults$CompoundID), " / ") == FALSE){
      MyPKResults <- MyPKResults %>% 
         select(-any_of("CompoundID"))
   }

   # If the column names were prettified, then they will include the dosing
   # interval and there is thus no need for the "Sheet" column. Similarly,
   # regardless of whether they prettified the column names, if there is only
   # one value present for the Sheet column, remove it. 
   if(prettify_columns | 
      length(unique(MyPKResults$Sheet)) == 1){
      MyPKResults <- MyPKResults %>% 
         select(-any_of("Sheet"))
   }

   include_dose_num <- check_include_dose_num(MyPKResults, 
                                              include_dose_num)

   if(include_dose_num == FALSE){
      names(MyPKResults) <- sub("Dose 1 |Last dose |_dose1|_last", "", names(MyPKResults))
   }

   if(as_label(shading_column) == "<empty>"){

      formatTable_Simcyp(MyPKResults,
                         shading_column = File, 
                         merge_shaded_cells = TRUE, 
                         fontsize = fontsize,
                         prettify_columns = prettify_columns, 
                         add_header_for_DDI = add_header_for_DDI, 
                         highlight_gmr_colors = highlight_gmr_colors, 
                         highlight_so_cutoffs = highlight_so_cutoffs, 
                         highlight_so_colors = highlight_so_colors)

   } else {

      formatTable_Simcyp(MyPKResults,
                         shading_column = !!shading_column, 
                         merge_shaded_cells = TRUE, 
                         fontsize = fontsize,
                         prettify_columns = prettify_columns, 
                         add_header_for_DDI = add_header_for_DDI, 
                         highlight_gmr_colors = highlight_gmr_colors, 
                         highlight_so_cutoffs = highlight_so_cutoffs, 
                         highlight_so_colors = highlight_so_colors)

   }

}
if(single_table == FALSE){

   MyPKResults <- 
      split(MyPKResults, f = list(MyPKResults$File, 
                                  MyPKResults$CompoundID, 
                                  MyPKResults$Tissue))

   PKparameters <- split(PKparameters, 
                         f = list(PKparameters$File, 
                                  PKparameters$CompoundID, 
                                  PKparameters$Tissue))

   CheckDoseInt_list <- split(CheckDoseInt$interval, 
                              f = list(CheckDoseInt$interval$File, 
                                       CheckDoseInt$interval$CompoundID, 
                                       CheckDoseInt$interval$Tissue))

   MyPKResults <- MyPKResults[which(lapply(MyPKResults, nrow) > 0)]

   for(i in names(MyPKResults)){
      # assign("ITERATION_STEP", value = i, envir = global_env())

      Heading <- paste0(
         str_to_title(unique(MyPKResults[[i]]$Tissue)), 
         " PK for the ", 
         unique(MyPKResults[[i]]$CompoundID), 
         " for the file '", 
         unique(MyPKResults[[i]]$File), "'")

      Heading <- ifelse(FromCalcPKRatios, 
                        sub("for the file", "for the file pair", Heading), 
                        Heading)

      TableSection <- knitr::knit_child(
         system.file("rmarkdown/templates/pktableindiv/skeleton/skeleton.Rmd",
                     package="SimcypConsultancy"), 
         envir = environment(), 
         quiet = TRUE)

      cat(TableSection, sep = "\n")

      rm(Heading, Deets, MyPerpetrator, MyFiles, MyAnnotations)

   }

   MyPKResults <- bind_rows(MyPKResults)

}

r Annotations[["table_caption"]]

if(any(complete.cases(highlight_gmr_colors))){
   TableKeyText_gmr <- "**Key for color-coded GMR values:**"
} else {
   TableKeyText_gmr <- ""
}

r TableKeyText_gmr

if(any(complete.cases(highlight_gmr_colors))){

   make_gmr_highlight_key(highlight_gmr_colors = highlight_gmr_colors)

} 
if(any(complete.cases(highlight_so_cutoffs))){
   TableKeyText_so <- "**Key for color-coded S/O values:**"
} else {
   TableKeyText_so <- ""
}

r TableKeyText_so

if(any(complete.cases(highlight_so_cutoffs))){

   make_so_highlight_key(highlight_so_cutoffs = highlight_so_cutoffs, 
                         highlight_so_colors = highlight_so_colors)

} 


shirewoman2/Consultancy documentation built on Feb. 18, 2025, 10 p.m.