# This controls default placeholders used in the figure and table labels,
# captions, etc  below. The commands and other strings just need a
# placeholder surrounded by === and that will be substituted for the
# placeholder value. The placeholder value can be a string or R Code. The
# underlying functions will attempt to evaluate them. If they evaluate
# successfully, they will have the return value of that evaluation. Otherwise
# the string will be kept as is. To force a string to be a string you need to
# double quote it. So if you specify "'ls()'" you will just see that string.
# If you specify ls() you will see the contents of the enviornment where the
# string is evaluated
placeholders:
  CMPD: Compound Name
  CUNITS: Conc Units
  TUNITS: Time Units
  RUN: RUNN
  OBJ: sprintf("%3g", fit$objf)
# Here you can specify the names of parameters used in reporting. For each
# parmaeter in your model you can create an entry and specify how it will be
# seen both formatted as markdown (md) or as text (txt). Unused parameters
# will be ignored and if you have parameters specified in the model but not
# defined here the names from the model will be used.
parameters:
  lVp:
    md:  "V~p~"
    txt: "Vp" 
  add_err:
    md:  "Add Err"
    txt: "Add Err"
  prop_err:
    md:  "Prop Err"
    txt: "Prop Err"
# Define covariates here as a character vector. For example to define the
# columns SEX and ROUTE as covariates you'd use:
# [ "SEX", "ROUTE"]
covariates:
  cat:  NULL
  cont: NULL
options:
  output_dir:   "file.path(getwd(), '===RUN===')"
  resolution:   300
  # Set fig_stamp to NULL to suppress
  fig_stamp:    "source: ===FILE==="
  figenv_preamble: |-
    # Loading required libraries
    library("ggplot2")
    library("xpose")
    library("ggforce")
    if(system.file(package="ggPMX") != ""){
      library("ggPMX")   
    }
    # Creating the xpdb object for xpose figures:
    xpdb = xpose.nlmixr2::xpose_data_nlmixr(fit)
  tabenv_preamble: NULL
#------
# Both figures and tables are generated based on user defined commands. These
# will be depdendnt on input data (e.g., nlmixr fit object), default
# dimensions (figure width and height), the type of report being generated
# (onbrand report object), etc. The following objects will be created
# in the environment where the user code is executed and can be used by the
# user defined code. Some variables are content type specific and are only
# present in the environtment for that content type. For example the resoluion
# option only applies to figures and is not defined in the table generation
# environment:
#
#  cat_covars   - character vector of categorical covariates from the dataset or NULL if there are none
#  cont_covars  - character vector of continuous  covariates from the dataset or NULL if there are none
#  fid          - current figure ID
#  fit          - nlmixr fit object
#  height       - figure height (figure enviornment only)
#  obnd         - onbrand object of the report document
#  output_dir   - directory where figures are stored
#  resolution   - resolution of the figure (figure environment only)
#  rptdetails   - result of reading in the report yaml file
#  rpttype      - either "Word" or "PowerPoint"
#  width        - figure width  (figure environment only)
#  xpdb         - the default yaml file contains code in the preamble to
#                 create this object by running xpose_data_nlmixr(fit) (figure
#                 environment only)
#
# The output must be stored in p_res and it can be either of the following:
#  - character variable containing the path to the figure file (you can have
#    multiple figures that will be treated as paginated).
#  - ggplot object (this can be a single figure or a paginated figure)
#
figures:
  # Here we list all figures to be generated.
  dv_vs_pred_ipred:
    orientation: "portrait"
    caption:     "Observed vs Predicted"
    title:       "Observed vs Predicted"
    cmd: |-
      # Population prediction  
      p_pred <- dv_vs_pred(xpdb, caption=NULL, title=NULL, subtitle=NULL) +
      ggtitle("===CMPD=== (===CUNITS===)") +
      coord_fixed()+
      ylab("Observed") +
      xlab("Population Predicted") +
      theme_light()
      yrange = layer_scales(p_pred)$y$range$range
      xrange = layer_scales(p_pred)$x$range$range
      lb = min(c(yrange,xrange))
      ub = max(c(yrange,xrange))
      p_pred = p_pred + xlim(c(lb, ub)) + ylim(c(lb,ub))

      ## Individual prediction  
      p_ipred <- dv_vs_ipred(xpdb, caption=NULL, title=NULL, subtitle=NULL) +
      ggtitle("===CMPD=== (===CUNITS===)") +
      coord_fixed()+
      ylab("Observed") +
      xlab("Individual Predicted") +
      theme_light()
      yrange = layer_scales(p_ipred)$y$range$range
      xrange = layer_scales(p_ipred)$x$range$range
      lb = min(c(yrange,xrange))
      ub = max(c(yrange,xrange))
      p_ipred = p_ipred + xlim(c(lb, ub)) + ylim(c(lb,ub))
      # Combining figures
      p_res <- ggpubr::ggarrange(p_pred, p_ipred, ncol=2, nrow=1 )
  res_vs_pred_idv:
    orientation: "portrait"
    caption:     "CWRES vs Pred and Time"
    title:       "CWRES vs Pred and Time"
    cmd: |-
      if("CWRES" %in% names(fit)){
        p_pred <- res_vs_pred(xpdb, caption=NULL, title=NULL, res="CWRES") +
        ggtitle("===CMPD=== (===CUNITS===)") +
        ylab("CWRES") +
        xlab("Population Predicted") +
        theme_light()
        
        p_idv  <- res_vs_idv(xpdb, caption=NULL, title=NULL, res="CWRES") +
        ggtitle("Time (===CUNITS===)") +
        ylab("CWRES") +
        xlab("Time (===TUNITS===)") +
        theme_light()

        p_res <- ggpubr::ggarrange(p_pred, p_idv, ncol=2, nrow=1 )
      } else {
        p_res <- NA
      }
  eta_cat:
    orientation: "landscape"
    caption:     "Effect of categorical covariates"
    title:       "Effect of categorical covariates"
    cmd: |-
      if(!is.null(cat_covars)){
        if(system.file(package="ggPMX") != ""){
          ctr = ggPMX::pmx_nlmixr(fit,
                  vpc = FALSE,
                  conts = cont_covars, 
                  cats  = cat_covars)
          p_res <- ggPMX::pmx_plot_eta_cats(ctr) +
            theme_light()
        } else {
          p_res <- mk_error_fig("ggPMX is not installed")
        }
      } else {
        p_res <- NA
      }
  eta_cont:
    orientation: "landscape"
    caption:     "Effect of continuous covariates"
    title:       "Effect of continuous covariates"
    cmd: |-
      if(!is.null(cont_covars)){
        if(system.file(package="ggPMX") != ""){
          ctr = ggPMX::pmx_nlmixr(fit,
                  vpc = FALSE,
                  conts = cont_covars, 
                  cats  = cat_covars)
          p_res <- ggPMX::pmx_plot_eta_conts(ctr) + 
            theme_light()
        } else {
          p_res = mk_error_fig("ggPMX is not installed")
        }
      } else {
        p_res <- NA
      }
  prm_vs_iteration:
    orientation: "landscape"
    caption:     "SAEM Stabilization"
    title:       "SAEM Stabilization"
    cmd: |-
      p_res <- prm_vs_iteration(xpdb, caption=NULL, title=NULL) +
        theme_light()
  ind_plots:
    orientation: "landscape"
    caption:     "Individual and population prediction overlay"
    title:       "Individual and population prediction overlay"
    cmd: |-
      p_res <- ind_plots(xpdb, nrow=3, ncol=4, caption=NULL, title=NULL)  +
      ylab(" ===CMPD=== (===CUNITS===)") +
      xlab("Time (===TUNITS===)")  +
      theme_light()
  eta_matrix:
    orientation: "landscape"
    caption:     "Correlation between IIV terms and parameters"
    title:       "IIV Correlation"
    cmd: |-
      if(system.file(package="ggPMX") != ""){
        ctr = ggPMX::pmx_nlmixr(fit,
                vpc = FALSE,
                conts = cont_covars, 
                cats  = cat_covars)
        p_res <- ggPMX::pmx_plot_eta_matrix(ctr) +
          theme_light()
      }
# vpc:        
#   orientation: "landscape"
#   caption:     "VPC"
#   title:       "VPC"
#   cmd: |-
#
#     p_res <- ind_plots(xpdb, nrow=3, ncol=4, caption=NULL, title=NULL)  +
#     ylab("Predicted and Observed ===CMPD=== Concentrations (===CUNITS===)") +
#     xlab("Time (===TUNITS===)")  +
#     theme_light()
  skip_figure:
    orientation: "landscape"
    caption:     "ind_plots caption"
    title:       "ind_plots title"
    cmd: |-
      p_res <- NA
tables:
  # Here we list all the tables to be generated
  skip_table:
    orientation: "portrait"
    caption:     "Parameter Estimates"
    title:       "Parameter Estimates"
    cmd: |-
      t_res <- NA
  pest_table:
    orientation: "portrait"
    caption:     "Parameter Estimates"
    title:       "Parameter Estimates"
    cmd: |-
      t_res <- gen_pest_table(
      fit        = fit,
      obnd       = obnd,
      rptdetails = rptdetails)
pptx:
  # Tables and figures aree generated 1 per slide, here you need to give the
  # information about that slide master found in the onbrand template for
  # your organization
  figures:
    master:
      name:       "content_text"
      title_ph:   "title"
      content_ph: "content_body"
    # The figure dimensions here are taken from the
    # master slide and have the same units as those
    # specified in analysis
    width:  9.5
    height: 5.0
  tables:
    master:
      name:       "content_text"
      title_ph:   "title"
      content_ph: "content_body"
  content:
    - table: pest_table
    - table: skip_table
    - figure: ind_plots
    - figure: dv_vs_pred_ipred
    - figure: res_vs_pred_idv
    - figure: prm_vs_iteration
    - figure: eta_cont
    - figure: eta_cat
    - figure: skip_figure
docx:
  figures:
    landscape:
      width:  8.0
      height: 4.2
    portrait:
      width:  6.5
      height: 6.0
  content:
    - text:
        text:  "Tables"
        style: Heading_1
    - table: pest_table
    - text:
        text:  "Figures"
        style: Heading_1
    - figure: ind_plots
    - figure: dv_vs_pred_ipred
    - figure: res_vs_pred_idv
    - figure: prm_vs_iteration
    - figure: skip_figure
    - figure: eta_cont
    - figure: eta_cat
