r Heading

# Name of subfolder: pktableindiv

if(FromCalcPKRatios){
   MyFiles <- str_split_1(unique(MyPKResults[[i]]$File), " / ")
} else {
   MyFiles <- unique(MyPKResults[[i]]$File)
}

if("logical" %in% class(existing_exp_details)){
   # This is when existing_exp_details is NA.
   Deets <- data.frame(Inhibitor1 = "UNKNOWN INHIBITOR", 
                       File = MyFiles)
} else {
   Deets <- existing_exp_details$MainDetails %>% filter(File %in% MyFiles)
}

MyPerpetrator <- determine_myperpetrator(Deets = Deets, 
                                         prettify_compound_names = prettify_compound_names)

DosesIncluded <- c("Dose1" = any(str_detect(PKparameters[[i]]$PKparameter, "_dose1")),
                   "Last" = any(str_detect(PKparameters[[i]]$PKparameter, "_last")), 
                   "User" = any(complete.cases(PKparameters[[i]]$Sheet)))
DosesIncluded <- str_c(names(DosesIncluded)[DosesIncluded], collapse = " ")

if(FromCalcPKRatios){
   IndivAnnotations <- 
      list("table_heading" = 
              paste0(
                 "***Table XXX.*** *Simulated ", MeanType, " mean ", 
                 unique(MyPKResults[[i]]$Tissue),  " PK parameters for the file pair ", 
                 str_comma(MyFiles), "*"), 

           "table_caption" = 
              paste0("*Simulated values listed are the ", MeanType, 
                     " means. CV: coefficient of variation; CI: confidence interval. Source simulated data: ", 
                     str_comma(MyFiles), "*"))

} else {

   IndivAnnotations <- make_table_annotations(
      MyPKResults = MyPKResults[[i]] %>% purrr::discard(~all(is.na(.))), 
      MyFile = unique(MyPKResults[[i]]$File), 
      MyCompoundID = unique(MyPKResults[[i]]$CompoundID), 
      prettify_compound_names = prettify_compound_names,
      existing_exp_details = existing_exp_details, 
      mean_type = mean_type, 
      DosesIncluded = case_match(DosesIncluded, 
                                 "Dose1 User" ~ "Dose1 Last", 
                                 "Last User" ~ "Last", 
                                 .default = DosesIncluded), 
      tissue = unique(MyPKResults[[i]]$Tissue), 
      name_clinical_study = name_clinical_study)

   IndivAnnotations[["table_heading"]] <- 
      paste0("***Table XXX.*** *", 
             IndivAnnotations[["table_heading"]], "*")

   IndivAnnotations[["table_caption"]] <- 
      paste0("*", IndivAnnotations[["table_caption"]], "*")

}

r IndivAnnotations[["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(str_detect(DosesIncluded, "Last") & 
   any(CheckDoseInt_list[[i]]$IntervalRemaining != CheckDoseInt_list[[i]]$DoseInt_X)){

   Problems <- CheckDoseInt_list[[i]] %>% 
      filter(DoseInt_X != IntervalRemaining)

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

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

      paste0("**Last dose start time: ",
             unique(Problems$LastDoseTime), " h**"),

      paste0("**Simulation end time: ",
             unique(Problems$SimDuration), " h**"),

      paste0("**Time used for integrating the last-dose AUC: ", 
             unique(Problems$IntervalRemaining), " h**"),

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

   rm(Problems)

} else {
   DoseIntWarning_last <- list(c(), c(), c(), c(), c(), c())
}

if(str_detect(DosesIncluded, "User") & 
   any(CheckDoseInt_list[[i]]$UserIntervalRemaining != CheckDoseInt_list[[i]]$DoseInt_X, na.rm = T)){

   Problems <- CheckDoseInt_list[[i]] %>% 
      filter(DoseInt_X != UserIntervalRemaining)

   DoseIntWarning_user <- list(
      "***WARNING: The time used for integrating the AUC for the user-defined interval was not the same as the dosing interval.***", 

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

      paste0("**User-defined interval start time: ",
             unique(Problems$UserIntervalStart), " h**"),

      paste0("**User-defined interval end time: ", 
             unique(Problems$UserIntervalEnd), " h**"),

      paste0("**Time used for integrating the user-defined AUC: ", 
             unique(Problems$UserIntervalRemaining), " h**"),

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

   rm(Problems)

} else {
   DoseIntWarning_user <- list(c(), c(), c(), c(), c(), c())
}

r DoseIntWarning_last[[1]]

r DoseIntWarning_last[[2]]

r DoseIntWarning_last[[3]]

r DoseIntWarning_last[[4]]

r DoseIntWarning_last[[5]]

r DoseIntWarning_last[[6]]

r DoseIntWarning_user[[1]]

r DoseIntWarning_user[[2]]

r DoseIntWarning_user[[3]]

r DoseIntWarning_user[[4]]

r DoseIntWarning_user[[5]]

r DoseIntWarning_user[[6]]

if(is.na(include_dose_num_orig)){
   include_dose_num <- check_include_dose_num(MyPKResults[[i]], 
                                              include_dose_num)
}

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

TempTable <- MyPKResults[[i]] %>% ungroup() %>% 
   purrr::discard(~all(is.na(.))) %>% 
   select(-any_of(c("CompoundID", "Tissue", "File", "Compound")))
names(TempTable) <- sub("perpetrator", MyPerpetrator, names(TempTable))

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

   formatTable_Simcyp(TempTable,
                      merge_shaded_cells = TRUE,
                      perpetrator_name = MyPerpetrator, 
                      fontsize = fontsize,
                      prettify_columns = prettify_columns, 
                      add_header_for_DDI = add_header_for_DDI, 
                      highlight_so_cutoffs = highlight_so_cutoffs, 
                      highlight_so_colors = highlight_so_colors, 
                      highlight_gmr_colors = highlight_gmr_colors)

} else {

   formatTable_Simcyp(TempTable,
                      shading_column = !!shading_column, 
                      merge_shaded_cells = TRUE,
                      perpetrator_name = MyPerpetrator, 
                      fontsize = fontsize,
                      prettify_columns = prettify_columns, 
                      add_header_for_DDI = add_header_for_DDI, 
                      highlight_so_cutoffs = highlight_so_cutoffs, 
                      highlight_so_colors = highlight_so_colors, 
                      highlight_gmr_colors = highlight_gmr_colors)

}

r IndivAnnotations[["table_caption"]]



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