---
title: "Generating Templates with table_templates.R"
date: "`r Sys.Date()`"
output:
    rmarkdown::html_document:
        theme: "spacelab"
        highlight: "kate"
        toc: true
        toc_float: true
author: 
  - Yolanda Zhou ([`yolandazzz13`])

vignette: >
  %\VignetteIndexEntry{Using Table Templates}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
editor_options:
    markdown:
        wrap: 72
---


```{r, include = FALSE}
suggested_dependent_pkgs <- c("filters")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = all(vapply(
    suggested_dependent_pkgs,
    requireNamespace,
    logical(1),
    quietly = TRUE
  ))
)
```

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


## Overview
This vignette demonstrates how to use predefined templates to create new R scripts for generating tables.The core idea is to provide a set of standard templates for common statistical tables, such as those for demography (`t_dm_slide`). Users can then modify them to suit their specific datasets and analysis needs.

## List available templates
Before generating a template, you can view all supported table templates using the ` list_all_templates()` function:

To get started, first navigate to your `autoslider.core` repo directory, make sure you have installed the `autoslider.core` package:


```{r, include = TRUE, eval = FALSE}
setwd("~/path/to/autoslider.core")

devtools::load_all()

list_all_templates()
```
This will return a character vector of supported templates (e.g., `t_dm_slide`, `g_ae_slide`, etc.), each corresponding to a table type stored in the `R/` folder of the package.

## Generate a template for your own table
Use the `use_template()` function to generate a template script that you can customize. For example:


```{r, eval = TRUE, include = FALSE}
# hidden setup
# Install and load the necessary packages
library(yaml)
library(assertthat)
library(tern)

# Create the YAML content
yaml_content <- '
ITT:
  title: Intent to Treat Population
  condition: ITTFL == "Y"
  target: adsl
  type: slref
SAS:
  title: Secondary Analysis Set
  condition: SASFL == "Y"
  target: adsl
  type: slref
SE:
  title: Safety Evaluable Population
  condition: SAFFL== "Y"
  target: adsl
  type: slref
SER:
  title: Serious Adverse Events
  condition: AESER == "Y"
  target: adae
  type: anl
LBCRP:
  title: CRP Values
  condition: PARAMCD == "CRP"
  target: adlb
  type: slref
LBNOBAS:
  title: Only Visits After Baseline
  condition: ABLFL != "Y" & !(AVISIT %in% c("SCREENING", "BASELINE"))
  target: adlb
  type: slref
'

# Create a temporary YAML file
filters <- tempfile(fileext = ".yaml")

# Write the YAML content to the temporary file
write(yaml_content, file = filters)

# Create the specs entry
specs_entry <- '
- program: t_ds_slide
  titles: Patient Disposition ({filter_titles("adsl")})
  footnotes: "t_ds footnotes"
  paper: L6
  suffix: ITT
- program: t_dm_slide
  titles: Patient Demographics and Baseline Characteristics
  footnotes: "t_dm_slide footnote"
  paper: L6
  suffix: ITT
  args:
    arm: "TRT01A"
    vars: ["SEX", "AGE", "RACE"]
'

# Create a temporary specs entry file
spec_file <- tempfile(fileext = ".yaml")

# Write the specs entry to the temporary file
write(specs_entry, file = spec_file)

# This chunk runs first and prepares the environment for the whole document

# 1. Load ALL necessary packages
library(rtables)      # For append_topleft()
library(dplyr)       # For %>% and other functions
library(assertthat)   # For assert_that() you had issues with before
# ... add any other packages your report uses

# 3. Load any data files needed
# data <- read.csv(here("data", "my_data.csv"))

# Clean up the temporary files
# file.remove(filters)
# file.remove(spec_file)
```

```{r_function_call, include = TRUE}
use_template(template = "t_dm_slide", function_name = "demography")

```
This will create a file with the demography template at `./programs/R/demography.R`, whose contents are displayed below.
```{r, include = TRUE}
library(dplyr)
library(autoslider.core)
demography <- function(adsl,
                       arm = "TRT01P",
                       vars = c("AGE", "SEX", "RACE"),
                       stats = c("median", "range", "count_fraction"),
                       split_by_study = FALSE,
                       side_by_side = NULL) {
  if (is.null(side_by_side)) {
    extra <- NULL
  } else {
    extra <- c("COUNTRY")
  }
  for (v in c(vars, extra)) {
    assert_that(has_name(adsl, v))
  }
  adsl1 <- adsl %>%
    select(all_of(c("STUDYID", "USUBJID", arm, vars, extra)))
  if (!is.null(side_by_side)) {
    adsl1$lvl <- "Global"
  }
  lyt <- build_table_header(adsl1, arm,
    split_by_study = split_by_study,
    side_by_side = side_by_side
  )
  lyt <- lyt %>%
    analyze_vars(
      na.rm = TRUE,
      .stats = stats,
      denom = "n",
      vars = vars,
      .formats = c(mean_sd = "xx.xx (xx.xx)", median = "xx.xx"),
      var_labels = formatters::var_labels(adsl1)[vars]
    )
  result <- lyt_to_side_by_side(lyt, adsl1, side_by_side)
  if (is.null(side_by_side)) {
    # adding "N" attribute
    arm <- col_paths(result)[[1]][1]
    n_r <- data.frame(
      ARM = toupper(names(result@col_info)),
      N = col_counts(result) %>% as.numeric()
    ) %>%
      `colnames<-`(c(paste(arm), "N")) %>%
      dplyr::arrange(get(arm))
    attr(result, "N") <- n_r
  }
  result@main_title <- "Demographic slide"
  result
}

```
Processing our data with the generated `t_dm_slide` script would look like：
```{r}
filters::load_filters(filters, overwrite = TRUE)
# read data
data <- list(
  "adsl" = eg_adsl %>%
    mutate(
      FASFL = SAFFL, # add FASFL for illustrative purpose for t_pop_slide
      # DISTRTFL is needed for t_ds_slide but is missing in example data
      DISTRTFL = sample(c("Y", "N"), size = length(TRT01A), replace = TRUE, prob = c(.1, .9))
    ) %>%
    preprocess_t_ds(), # this preproccessing is required by one of the autoslider.core functions
  "adae" = eg_adae,
  "adtte" = eg_adtte,
  "adrs" = eg_adrs,
  "adlb" = eg_adlb
)
demography(data$adsl)

```
Following this process, you have successfully generated a reusable `demography` function that processes clinical data, and convert it into a standard demographic table, including statistical insights like range, median, and count fractions to variables AGE, SEX, and RACE.
 
Naturally, you may want to customize the script to cater to the dataset characteristics or study objectives. To create multiple versions from the same template for specific analytics requirements, we have made it possible to do so by passing in different `function_name`. For example: 

```{r_function_call, include = TRUE, eval = FALSE}
use_template(template = "l_ae_slide", function_name = "l_ae_slide_001")
# which generates an Adverse Events listing at ./programs/R/l_ae_slide_001.R

use_template(template = "l_ae_slide", function_name = "l_ae_slide_v2")
# which generates an Adverse Events listing at ./programs/R/l_ae_slide_v2.R
```
### Notes
- The `template` argument must match one of the entries in list_all_templates(). 

- If a file with the same name already exists, it will not be overwritten unless you set `overwrite = TRUE`.

- In interactive mode, the generated file will open in your R editor for immediate editing.


## Customize the template
Feel free to modify the template script to suit your dataset or statistical needs. For instance, you can:

- Add or remove table columns

- Customize labels and formatting

- Modify the summary statistics



