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

if(FromCalcPKRatios){

   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")

   }
} 
Annotations <- make_table_annotations(MyPKResults = MyPKResults,
                                      MyFile = sim_data_file, 
                                      MyCompoundID = compoundToExtract, 
                                      prettify_compound_names = prettify_compound_names, 
                                      existing_exp_details = Deets %>% mutate(File = sim_data_file), 
                                      mean_type = mean_type, 
                                      tissue = tissue)

# 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(CheckDoseInt$message == "mismatch last dose"){

   DoseIntWarning <- list(
      "***WARNING: The time used for integrating the AUC for the last dose was not the same as the dosing interval.***",

      paste0("**Dosing interval: ", CheckDoseInt$interval$DoseInt_X, " h**"),

      paste0("**Last dose start time: ", CheckDoseInt$interval$LastDoseTime, " h**"),

      paste0("**Simulation end time: ", CheckDoseInt$interval$SimDuration, " h**"),

      paste0("**Interval for integrating the last-dose AUC: ", 
             CheckDoseInt$interval$IntervalRemaining, " h**"),

      "***Please check these results carefully.***")
} else {
   DoseIntWarning <- list(
      c(), c(), c(), c(), c(), c()
   )}

Table XXX. r Annotations[["table_heading"]]

r DoseIntWarning[[1]]

r DoseIntWarning[[2]]

r DoseIntWarning[[3]]

r DoseIntWarning[[4]]

r DoseIntWarning[[5]]

r DoseIntWarning[[6]]

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

formatTable_Simcyp(MyPKResults, 
                   fontsize = fontsize,
                   prettify_columns = TRUE,
                   add_header_for_DDI = add_header_for_DDI, 
                   perpetrator_name = 
                      ifelse(prettify_columns, 
                             determine_myperpetrator(Deets,
                                                     prettify_compound_names = prettify_compound_names), 
                             "perpetrator"),
                   highlight_so_cutoffs = highlight_so_cutoffs, 
                   highlight_so_colors = highlight_so_colors, 
                   highlight_gmr_colors = highlight_gmr_colors)

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))){
   highlight_gmr_colors <- set_boundary_colors(color_set = highlight_gmr_colors, 
                                               boundaries = c(1, 1.25, 2, 5), 
                                               break_type = "GMR")

   tibble(`Interaction level` = names(highlight_gmr_colors)) %>% 
      flextable::flextable() %>% 
      flextable::bold(part = "header") %>% 
      flextable::bg(i = 1, 
                    bg = highlight_gmr_colors["negligible"]) %>% 
      flextable::bg(i = 2, 
                    bg = highlight_gmr_colors["weak"]) %>% 
      flextable::bg(i = 3, 
                    bg = highlight_gmr_colors["moderate"]) %>% 
      flextable::bg(i = 4, 
                    bg = highlight_gmr_colors["strong"]) %>% 
      flextable::width(width = 1.5) %>% 
      flextable::align(align = "center", part = "all")

} 
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))){

   highlight_so_colors <- set_boundary_colors(color_set = highlight_so_colors, 
                                              boundaries = highlight_so_cutoffs, 
                                              break_type = "SO")

   SOkey <- data.frame(UpperA = highlight_so_cutoffs[1:(length(highlight_so_cutoffs)-1)], 
                       UpperB = highlight_so_cutoffs[2:(length(highlight_so_cutoffs))], 
                       LowerA = 1/highlight_so_cutoffs[2:(length(highlight_so_cutoffs))], 
                       LowerB = 1/highlight_so_cutoffs[1:(length(highlight_so_cutoffs)-1)]) %>% 
      bind_rows(data.frame(UpperA = NA, 
                           UpperB = highlight_so_cutoffs[length(highlight_so_cutoffs)], 
                           LowerA = NA, 
                           LowerB = 1/highlight_so_cutoffs[length(highlight_so_cutoffs)])) %>% 
      mutate(across(.fns = round_consultancy), 
             Text = paste(LowerA, "to", LowerB, "fold or", UpperA, "to", UpperB, "fold"))

   SOkey$Text[1] <- paste(SOkey$LowerA[1], "to", SOkey$UpperB[1], "fold")
   SOkey$Text[nrow(SOkey)] <- paste("<", SOkey$LowerB[nrow(SOkey)], 
                                    "fold or >", SOkey$UpperB[nrow(SOkey)], "fold")

   SOkey <- SOkey %>% select(Text) %>% 
      rename(`S/O cutoff` = Text) %>% 
      flextable::flextable() %>% 
      flextable::bold(part = "header") %>% 
      flextable::width(width = 3.5) %>% 
      flextable::align(align = "center", part = "all")

   for(i in 1:length(highlight_so_cutoffs)){
      SOkey <- SOkey %>% 
         flextable::bg(i = i, 
                       bg = highlight_so_colors[i])
   }

   SOkey 

} 


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