diff --git a/NAMESPACE b/NAMESPACE
index dfff5c37..a745039f 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -19,6 +19,8 @@ export(make_table_07)
export(make_table_08)
export(make_table_09)
export(make_table_09_gtsum)
+export(make_table_09_gtsummary)
+export(make_table_09_rtables)
export(make_table_09_tplyr)
export(make_table_10)
export(make_table_11)
diff --git a/R/fda-table_09.R b/R/fda-table_09.R
index 0a17e873..023e78a5 100644
--- a/R/fda-table_09.R
+++ b/R/fda-table_09.R
@@ -3,11 +3,11 @@
#'
#' @inheritParams argument_convention
#'
-#' @name make_table_09
+#' @name make_table_09_rtables
NULL
-#' @describeIn make_table_09 Create FDA table 9 using functions from `rtables` and `tern`.
+#' @describeIn make_table_09_rtables Create FDA table 9 using functions from `rtables` and `tern`.
#'
#' @details
#' * `adae` must contain `AESER`, `AESOC`, and the variables specified by `pref_var`, `id_var`, `arm_var`,
@@ -20,7 +20,7 @@ NULL
#' * All-zero rows are removed by default (see `prune_0` argument).
#'
#' @return
-#' * `make_table_09` returns an `rtables` table object.
+#' * `make_table_09_rtables` returns an `rtables` table object.
#'
#' @return An `rtable` object.
#'
@@ -28,22 +28,22 @@ NULL
#' adsl <- random.cdisc.data::cadsl
#' adae <- random.cdisc.data::cadae
#'
-#' tbl <- make_table_09(adae = adae, alt_counts_df = adsl)
+#' tbl <- make_table_09_rtables(adae = adae, alt_counts_df = adsl)
#' tbl
#'
#' @export
-make_table_09 <- function(adae,
- alt_counts_df = NULL,
- show_colcounts = TRUE,
- id_var = "USUBJID",
- arm_var = "ARM",
- saffl_var = "SAFFL",
- pref_var = "AEDECOD",
- lbl_pref_var = formatters::var_labels(adae, fill = TRUE)[pref_var],
- lbl_overall = NULL,
- risk_diff = NULL,
- prune_0 = TRUE,
- annotations = NULL) {
+make_table_09_rtables <- function(adae,
+ alt_counts_df = NULL,
+ show_colcounts = TRUE,
+ id_var = "USUBJID",
+ arm_var = "ARM",
+ saffl_var = "SAFFL",
+ pref_var = "AEDECOD",
+ lbl_pref_var = formatters::var_labels(adae, fill = TRUE)[pref_var],
+ lbl_overall = NULL,
+ risk_diff = NULL,
+ prune_0 = TRUE,
+ annotations = NULL) {
assert_subset(c("AESER", "AESOC", arm_var, id_var, saffl_var, pref_var), names(adae))
assert_flag_variables(adae, saffl_var)
@@ -683,3 +683,123 @@ calculate_riskdiff <- function(x, y, n_x, n_y) {
paste0(val, " (", conf_int[[1]], ", ", conf_int[[2]], ")")
})
}
+
+
+
+#' Engine-Specific Functions: Table 09
+#'
+#' The table engine used by each engine-specific function is identified by its suffix.
+#'
+#' @inheritParams argument_convention
+#'
+#' @return
+#' * `make_table_09_gtsummary()` returns a `gtsummary` object.
+#'
+#' @seealso [make_table_09()]
+#'
+#' @export
+#'
+#' @examples
+#' adsl <- random.cdisc.data::cadsl
+#' adae <- random.cdisc.data::cadae
+#'
+#' tbl_gtsummary <- make_table_09_gtsummary(df = adae, denominator = adsl)
+#' tbl_gtsummary
+make_table_09_gtsummary <- function(
+ df,
+ denominator = NULL,
+ saffl_var = "SAFFL",
+ ser_var = "AESER",
+ arm_var = "ARM",
+ pref_var = "AEDECOD",
+ id_var = "USUBJID",
+ soc_var = "AESOC",
+ lbl_overall = NULL) {
+ assert_data_frame(df)
+ assert_subset(c(saffl_var, id_var, ser_var, soc_var, arm_var, pref_var), names(df))
+ assert_flag_variables(df, saffl_var)
+ assert_factor(df[[arm_var]])
+
+ if (!is.null(denominator)) {
+ assert_data_frame(denominator)
+ assert_subset(c(id_var, arm_var), names(denominator))
+ }
+
+ if (is.null(denominator)) {
+ denominator <- df
+ }
+
+ df <- df %>%
+ dplyr::filter(.data[[saffl_var]] == "Y", .data[[ser_var]] == "Y")
+
+ tbl <- df |>
+ tbl_hierarchical(
+ variables = c(soc_var, pref_var),
+ by = arm_var,
+ id = id_var,
+ denominator = denominator,
+ overall_row = TRUE,
+ label = "overall" ~ "Any SAE"
+ )
+ if (!is.null(lbl_overall)) {
+ total <- df |>
+ tbl_hierarchical(
+ variables = c(soc_var, pref_var),
+ id = id_var,
+ denominator = denominator,
+ overall_row = TRUE
+ )
+
+ tbl <- tbl_merge(list(tbl, total), tab_spanner = FALSE)
+ }
+
+
+ return(tbl)
+}
+
+
+#' FDA Table 9: Patients With Serious Adverse Events by System Organ Class and Preferred Term,
+#' Safety Population, Pooled Analyses
+#'
+#' @inheritParams argument_convention
+#'
+#' @return A `gtsummary` table and, if `return_ard = TRUE`, an ARD.
+#' If `return_ard = TRUE`, they will be returned as a list with named elements `table` and `ard`.
+#' @export
+#'
+#' @examples
+#' adsl <- random.cdisc.data::cadsl
+#' adae <- random.cdisc.data::cadae
+#'
+#' tbl <- make_table_09(df = adae, denominator = adsl)
+#' tbl
+make_table_09 <- function(
+ df,
+ denominator = NULL,
+ return_ard = TRUE,
+ id_var = "USUBJID",
+ arm_var = "ARM",
+ saffl_var = "SAFFL",
+ ser_var = "AESER",
+ soc_var = "AESOC",
+ pref_var = "AEDECOD",
+ lbl_overall = NULL) {
+ tbl <- make_table_09_gtsummary(
+ df = df,
+ denominator = denominator,
+ saffl_var = saffl_var,
+ ser_var = ser_var,
+ arm_var = arm_var,
+ pref_var = pref_var,
+ id_var = id_var,
+ soc_var = soc_var,
+ lbl_overall = lbl_overall
+ )
+
+ if (return_ard) {
+ ard <- gather_ard(tbl)
+ return(list(table = tbl, ard = ard))
+ } else {
+ return(tbl)
+ }
+}
diff --git a/man/make_table_09.Rd b/man/make_table_09.Rd
index dd32f724..597a60c7 100644
--- a/man/make_table_09.Rd
+++ b/man/make_table_09.Rd
@@ -1,27 +1,12 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fda-table_09.R
-\name{make_table_09}
-\alias{make_table_09}
+\name{make_table_09_tplyr}
\alias{make_table_09_tplyr}
\alias{make_table_09_gtsum}
+\alias{make_table_09}
\title{FDA Table 9: Patients With Serious Adverse Events by System Organ Class and Preferred Term,
Safety Population, Pooled Analyses}
\usage{
-make_table_09(
- adae,
- alt_counts_df = NULL,
- show_colcounts = TRUE,
- id_var = "USUBJID",
- arm_var = "ARM",
- saffl_var = "SAFFL",
- pref_var = "AEDECOD",
- lbl_pref_var = formatters::var_labels(adae, fill = TRUE)[pref_var],
- lbl_overall = NULL,
- risk_diff = NULL,
- prune_0 = TRUE,
- annotations = NULL
-)
-
make_table_09_tplyr(
adae,
alt_counts_df = NULL,
@@ -57,58 +42,71 @@ make_table_09_gtsum(
annotations = NULL,
risk_diff = NULL
)
+
+make_table_09(
+ df,
+ denominator = NULL,
+ return_ard = TRUE,
+ id_var = "USUBJID",
+ arm_var = "ARM",
+ saffl_var = "SAFFL",
+ ser_var = "AESER",
+ soc_var = "AESOC",
+ pref_var = "AEDECOD",
+ lbl_overall = NULL
+)
}
\arguments{
\item{adae}{(\code{data.frame})\cr dataset (typically ADAE) required to build table.}
\item{alt_counts_df}{(\code{character})\cr alternative dataset (typically ADSL) used only to calculate column counts.}
-\item{show_colcounts}{(\code{flag})\cr Whether column counts should be printed. Boolean.}
-
\item{id_var}{(\code{character})\cr variable used as unique subject identifier.}
\item{arm_var}{(\code{character})\cr Name of the treatment arm variable used to split table into columns.}
\item{saffl_var}{(\code{character})\cr safety population flag variable from \code{adae} to include in the table.}
+\item{ser_var}{(\code{character})\cr serious Event variable from \code{adae} to include in the table.}
+
+\item{soc_var}{(\code{character})\cr system organ class variable from \code{adae} to include in the table.}
+
\item{pref_var}{(\code{character})\cr Name of the preferred term variable from \code{adae} to include in the table.}
-\item{lbl_pref_var}{(\code{character})\cr label corresponding to preferred term variable \code{pref_var} to print in the table.}
+\item{lbl_soc_var}{(\code{character})\cr label corresponding to system organ class variable
+\code{soc_var} to print in the table.}
-\item{lbl_overall}{(\code{character})\cr if specified, an overall column will be added to the table with
-the given value as the column label.}
+\item{lbl_pref_var}{(\code{character})\cr label corresponding to preferred term variable \code{pref_var} to print in the table.}
-\item{risk_diff}{(\code{list} of \code{character} vectors)\cr List of character vectors. Each vector must be
+\item{risk_diff_pairs}{(\code{list} of \code{character} vectors)\cr Optional. List of character vectors. Each vector must be
of length 2 and contain the name of treatment arms to calculate risk difference and its 95\% CI for. Those names
must exist in the \code{arm_var} column of the dataset specified in \code{adae}.}
+\item{show_colcounts}{(\code{flag})\cr Whether column counts should be printed. Boolean.}
+
+\item{lbl_overall}{(\code{character})\cr if specified, an overall column will be added to the table with
+the given value as the column label.}
+
\item{prune_0}{(\code{flag})\cr Whether all-zero rows should be removed from the table. Boolean.}
+\item{tplyr_raw}{(\code{flag})\cr Boolean. Indicates whether the raw \code{tibble} created using \code{Tplyr} functions should be
+returned or a presentation ready \code{gt_table} (default).}
+
\item{annotations}{(named \code{list} of \code{character})\cr list of annotations to add to the table. Valid
annotation types are \code{title}, \code{subtitles}, and a list of characters called \code{footnotes}.
Each name-value pair should use the annotation type as name and the desired string as the value.}
-\item{ser_var}{(\code{character})\cr serious Event variable from \code{adae} to include in the table.}
-
-\item{soc_var}{(\code{character})\cr system organ class variable from \code{adae} to include in the table.}
-
-\item{lbl_soc_var}{(\code{character})\cr label corresponding to system organ class variable
-\code{soc_var} to print in the table.}
-
-\item{risk_diff_pairs}{(\code{list} of \code{character} vectors)\cr Optional. List of character vectors. Each vector must be
+\item{risk_diff}{(\code{list} of \code{character} vectors)\cr List of character vectors. Each vector must be
of length 2 and contain the name of treatment arms to calculate risk difference and its 95\% CI for. Those names
must exist in the \code{arm_var} column of the dataset specified in \code{adae}.}
-\item{tplyr_raw}{(\code{flag})\cr Boolean. Indicates whether the raw \code{tibble} created using \code{Tplyr} functions should be
-returned or a presentation ready \code{gt_table} (default).}
-}
-\value{
-\itemize{
-\item \code{make_table_09} returns an \code{rtables} table object.
-}
+\item{df}{(\code{data.frame})\cr dataset required to build table.}
-An \code{rtable} object.
+\item{denominator}{(\code{data.frame})\cr alternative dataset (typically ADSL) used only to calculate denominator counts.}
+\item{return_ard}{(\code{flag})\cr whether an ARD should be returned. Defaults to \code{TRUE}.}
+}
+\value{
\itemize{
\item \code{make_table_09_tplyr()} returns a \code{gt_tbl} object when \code{tplyr_raw = FALSE} (default) and
a \code{tibble} object when \code{tplyr_raw = TRUE}.
@@ -117,23 +115,15 @@ a \code{tibble} object when \code{tplyr_raw = TRUE}.
\itemize{
\item \code{make_table_09_gtsum} returns a \code{gt} object.
}
+
+A \code{gtsummary} table and, if \code{return_ard = TRUE}, an ARD.
+If \code{return_ard = TRUE}, they will be returned as a list with named elements \code{table} and \code{ard}.
}
\description{
FDA Table 9: Patients With Serious Adverse Events by System Organ Class and Preferred Term,
Safety Population, Pooled Analyses
}
\details{
-\itemize{
-\item \code{adae} must contain \code{AESER}, \code{AESOC}, and the variables specified by \code{pref_var}, \code{id_var}, \code{arm_var},
-and \code{saffl_var}.
-\item If specified, \code{alt_counts_df} must contain the variables specified by \code{arm_var}, \code{id_var}, and \code{saffl_var}.
-\item Flag variables (i.e. \code{XXXFL}) are expected to have two levels: \code{"Y"} (true) and \code{"N"} (false). Missing values in
-flag variables are treated as \code{"N"}.
-\item Columns are split by arm. Overall population column is excluded by default (see \code{lbl_overall} argument).
-\item Numbers in table represent the absolute numbers of patients and fraction of \code{N}.
-\item All-zero rows are removed by default (see \code{prune_0} argument).
-}
-
For \code{make_table_09_tplyr()}
\itemize{
\item \code{adae} must contain the variables specified by \code{id_var}, \code{arm_var}, \code{saffl_var}, \code{ser_var}, \code{soc_var}, and
@@ -151,8 +141,6 @@ the safety population.
}
\section{Functions}{
\itemize{
-\item \code{make_table_09()}: Create FDA table 9 using functions from \code{rtables} and \code{tern}.
-
\item \code{make_table_09_tplyr()}: Create FDA table 9 using functions from \code{Tplyr}.
\item \code{make_table_09_gtsum()}: Create FDA table 9 using functions from \code{gt}.
@@ -169,12 +157,6 @@ the safety population.
adsl <- random.cdisc.data::cadsl
adae <- random.cdisc.data::cadae
-tbl <- make_table_09(adae = adae, alt_counts_df = adsl)
-tbl
-
-adsl <- random.cdisc.data::cadsl
-adae <- random.cdisc.data::cadae
-
# Basic table
make_table_09_tplyr(adae = adae, alt_counts_df = adsl)
@@ -218,4 +200,9 @@ tbl <- make_table_09_gtsum(
)
tbl
+adsl <- random.cdisc.data::cadsl
+adae <- random.cdisc.data::cadae
+
+tbl <- make_table_09(df = adae, denominator = adsl)
+tbl
}
diff --git a/man/make_table_09_gtsummary.Rd b/man/make_table_09_gtsummary.Rd
new file mode 100644
index 00000000..c473b8a4
--- /dev/null
+++ b/man/make_table_09_gtsummary.Rd
@@ -0,0 +1,57 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/fda-table_09.R
+\name{make_table_09_gtsummary}
+\alias{make_table_09_gtsummary}
+\title{Engine-Specific Functions: Table 09}
+\usage{
+make_table_09_gtsummary(
+ df,
+ denominator = NULL,
+ saffl_var = "SAFFL",
+ ser_var = "AESER",
+ arm_var = "ARM",
+ pref_var = "AEDECOD",
+ id_var = "USUBJID",
+ soc_var = "AESOC",
+ lbl_overall = NULL
+)
+}
+\arguments{
+\item{df}{(\code{data.frame})\cr dataset required to build table.}
+
+\item{denominator}{(\code{data.frame})\cr alternative dataset (typically ADSL) used only to calculate denominator counts.}
+
+\item{saffl_var}{(\code{character})\cr flag variable used to indicate inclusion in safety population.}
+
+\item{ser_var}{(\code{character})\cr Name of the serious adverse event flag variable which must contain at least
+"Y" for "Yes".}
+
+\item{arm_var}{(\code{character})\cr Name of the treatment arm variable used to split table into columns.}
+
+\item{pref_var}{(\code{character})\cr Name of the preferred term variable from \code{adae} to include in the table.}
+
+\item{id_var}{(\code{character})\cr variable used as unique subject identifier.}
+
+\item{soc_var}{(\code{character})\cr Name of the system organ class variable from \code{adae} to include in the table.}
+
+\item{lbl_overall}{(\code{character})\cr if specified, an overall column will be added to the table with
+the given value as the column label.}
+}
+\value{
+\itemize{
+\item \code{make_table_09_gtsummary()} returns a \code{gtsummary} object.
+}
+}
+\description{
+The table engine used by each engine-specific function is identified by its suffix.
+}
+\examples{
+adsl <- random.cdisc.data::cadsl
+adae <- random.cdisc.data::cadae
+
+tbl_gtsummary <- make_table_09_gtsummary(df = adae, denominator = adsl)
+tbl_gtsummary
+}
+\seealso{
+\code{\link[=make_table_09]{make_table_09()}}
+}
diff --git a/man/make_table_09_rtables.Rd b/man/make_table_09_rtables.Rd
new file mode 100644
index 00000000..e004148d
--- /dev/null
+++ b/man/make_table_09_rtables.Rd
@@ -0,0 +1,97 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/fda-table_09.R
+\name{make_table_09_rtables}
+\alias{make_table_09_rtables}
+\title{FDA Table 9: Patients With Serious Adverse Events by System Organ Class and Preferred Term,
+Safety Population, Pooled Analyses}
+\usage{
+make_table_09_rtables(
+ adae,
+ alt_counts_df = NULL,
+ show_colcounts = TRUE,
+ id_var = "USUBJID",
+ arm_var = "ARM",
+ saffl_var = "SAFFL",
+ pref_var = "AEDECOD",
+ lbl_pref_var = formatters::var_labels(adae, fill = TRUE)[pref_var],
+ lbl_overall = NULL,
+ risk_diff = NULL,
+ prune_0 = TRUE,
+ annotations = NULL
+)
+}
+\arguments{
+\item{adae}{(\code{data.frame})\cr dataset (typically ADAE) required to build table.}
+
+\item{alt_counts_df}{(\code{character})\cr alternative dataset (typically ADSL) used only to calculate column counts.}
+
+\item{show_colcounts}{(\code{flag})\cr Whether column counts should be printed. Boolean.}
+
+\item{id_var}{(\code{character})\cr variable used as unique subject identifier.}
+
+\item{arm_var}{(\code{character})\cr Name of the treatment arm variable used to split table into columns.}
+
+\item{saffl_var}{(\code{character})\cr flag variable used to indicate inclusion in safety population.}
+
+\item{pref_var}{(\code{character})\cr Name of the preferred term variable from \code{adae} to include in the table.}
+
+\item{lbl_pref_var}{(\code{character})\cr label corresponding to preferred term variable \code{pref_var} to print in the table.}
+
+\item{lbl_overall}{(\code{character})\cr if specified, an overall column will be added to the table with
+the given value as the column label.}
+
+\item{risk_diff}{(named \code{list})\cr list of settings to apply to add one or more risk difference columns to the table.
+Defaults to \code{NULL} (no risk difference column added). See \code{\link[tern:add_riskdiff]{tern::add_riskdiff()}} for more details. List should
+contain the following elements:
+\itemize{
+\item \code{arm_x}: (required) the name of reference arm.
+\item \code{arm_y}: (required) the names of the arms to compare to the reference arm. A new column will be added for each
+element of \code{arm_y}.
+\item \code{col_label}: (optional) labels to use for the risk difference columns. Defaults to
+\code{"Risk Difference (\%) (95\% CI)"}. For more than one risk difference column, \code{"arm x vs. arm y"} text will also
+be included in the column labels by default. The length of \code{col_label} must be equal to the length of \code{arm_y}.
+\item \code{pct}: (optional) whether the output should be returned as percentages. Defaults to \code{TRUE}.
+}}
+
+\item{prune_0}{(\code{flag})\cr Whether all-zero rows should be removed from the table. Boolean.}
+
+\item{annotations}{(named \code{list} of \code{character})\cr list of annotations to add to the table. Valid
+annotation types are \code{title}, \code{subtitles}, \code{main_footer}, and \code{prov_footer}. Each name-value pair should
+use the annotation type as name and the desired string as value.}
+}
+\value{
+\itemize{
+\item \code{make_table_09_rtables} returns an \code{rtables} table object.
+}
+
+An \code{rtable} object.
+}
+\description{
+FDA Table 9: Patients With Serious Adverse Events by System Organ Class and Preferred Term,
+Safety Population, Pooled Analyses
+}
+\details{
+\itemize{
+\item \code{adae} must contain \code{AESER}, \code{AESOC}, and the variables specified by \code{pref_var}, \code{id_var}, \code{arm_var},
+and \code{saffl_var}.
+\item If specified, \code{alt_counts_df} must contain the variables specified by \code{arm_var}, \code{id_var}, and \code{saffl_var}.
+\item Flag variables (i.e. \code{XXXFL}) are expected to have two levels: \code{"Y"} (true) and \code{"N"} (false). Missing values in
+flag variables are treated as \code{"N"}.
+\item Columns are split by arm. Overall population column is excluded by default (see \code{lbl_overall} argument).
+\item Numbers in table represent the absolute numbers of patients and fraction of \code{N}.
+\item All-zero rows are removed by default (see \code{prune_0} argument).
+}
+}
+\section{Functions}{
+\itemize{
+\item \code{make_table_09_rtables()}: Create FDA table 9 using functions from \code{rtables} and \code{tern}.
+
+}}
+\examples{
+adsl <- random.cdisc.data::cadsl
+adae <- random.cdisc.data::cadae
+
+tbl <- make_table_09_rtables(adae = adae, alt_counts_df = adsl)
+tbl
+
+}
diff --git a/quarto/table-templates/template-table_09.qmd b/quarto/table-templates/template-table_09.qmd
index 6d83cea3..73c75905 100644
--- a/quarto/table-templates/template-table_09.qmd
+++ b/quarto/table-templates/template-table_09.qmd
@@ -9,9 +9,82 @@ format: html
![](../assets/images/screenshots/table_09.png){fig-align="center" width="78%"}
+## gtsummary Table
+
+
+ gtsummary Table Setup
+```{r tbl_gtsummary, eval=FALSE, echo=TRUE}
+# Load Libraries & Data
+library(cardinal)
+
+adsl <- random.cdisc.data::cadsl
+adae <- random.cdisc.data::cadae
+
+# Output Table
+make_table_09(df = adae, denominator = adsl, return_ard = FALSE)
+```
+
+
+```{r tbl_gtsummary, message=FALSE, warning=FALSE, eval=TRUE}
+```
+
+
+ Function Details
+### `make_table_09()`
+
+------------------------------------------------------------------------
+
+Required variables:
+
+- **`df`**: must contain the variables specified by `id_var`, `arm_var`, `saffl_var`, `ser_var`, `soc_var`, and `pref_var`.
+- **`denominator`** (if specified): must contain the variables specified by `id_var`, `saffl_var`, and `arm_var`.
+
+| | | |
+|-----------------|----------------------------------------------------------------------------------------------------------------------|--------------|
+| **Argument** | **Description** | **Default** |
+| `df` | (`data.frame`) Dataset (typically ADSL) required to build table. | *No default* |
+| `denominator` | (`character`) Alternative dataset used only to calculate column counts. | `NULL` |
+| `return_ard` | (`flag`) Whether an ARD should be returned. | `TRUE` |
+| `id_var` | (`character`) Identifier variable used to count the participants within each flag. | `"USUBJID"` |
+| `arm_var` | (`character`) Arm variable used to split table into columns. | `"ARM"` |
+| `saffl_var` | (`character`) Flag variable used to indicate inclusion in safety population. | `"SAFFL"` |
+| `ser_var` | (`character`) FMQ scope variable to use in table. | `"AESER"` |
+| `soc_var` | (`character`) FMQ reference name variable to use in table. | `"AESOC"` |
+| `pref_var` | (`character`) FMQ scope, can be '"NARROW"' or '"BROAD"'. | `"AEDECOD"` |
+| `lbl_overall` | (`character`) (if specified): an overall column will be added to the table with the given value as the column label. | `NULL` |
+
+Source code for this function is available [here](https://github.com/pharmaverse/cardinal/blob/main/R/fda-table_09.R).
+
+
+## ARD
+
+
+ ARD Setup
+```{r ard, echo=TRUE, message=FALSE, warning=FALSE, results='hide'}
+# Load Libraries & Data
+library(cardinal)
+
+adsl <- random.cdisc.data::cadsl
+adae <- random.cdisc.data::cadae
+
+# Create Table & ARD
+result <- make_table_09(df = adae, denominator = adsl)
+
+# Output ARD
+result$ard
+```
+
+
+```{r ard_print, echo=FALSE}
+withr::local_options(width = 9999)
+print(result$ard, columns = "all")
+```
+
## rtables Table
-```{r tbl, message=FALSE, warning=FALSE}
+
+ rtables Table Setup
+```{r tbl_rtables, echo=TRUE, eval=FALSE}
# Load Libraries & Data
library(cardinal)
@@ -23,17 +96,18 @@ pref_var <- "AEDECOD"
# Output Table
risk_diff <- list(arm_x = "B: Placebo", arm_y = "A: Drug X") # optional
-make_table_09(adae = adae, alt_counts_df = adsl, pref_var = pref_var, risk_diff = risk_diff)
+make_table_09_rtables(adae = adae, alt_counts_df = adsl, pref_var = pref_var, risk_diff = risk_diff)
```
-## rtables Table Setup
+
-```{r tbl, eval=FALSE, echo=TRUE}
-```
-## Function Details: `make_table_09`
+```{r tbl_rtables, message=FALSE, warning=FALSE, eval=TRUE}
+```
-### `make_table_09()`
+
+ Function Details
+### `make_table_09()_rtables`
------------------------------------------------------------------------
@@ -77,10 +151,13 @@ Required variables:
+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------+
Source code for this function is available [here](https://github.com/pharmaverse/cardinal/blob/main/R/fda-table_09.R#L24).
+
## gt Table
-```{r tbl_gt, message=FALSE, warning=FALSE}
+
+ gt Table Setup
+```{r tbl_gt, echo=TRUE, eval=FALSE}
# Load Libraries & Data
library(cardinal)
@@ -91,21 +168,22 @@ adae <- random.cdisc.data::cadae
make_table_09_gtsum(adae = adae, alt_counts_df = adsl)
```
-## gt Table Setup
+
-```{r tbl_gt, eval=FALSE, echo=TRUE}
-```
-## Function Details: `make_table_09_gtsum`
+```{r tbl_gt, message=FALSE, warning=FALSE, eval=TRUE}
+```
+
+ Function Details
### `make_table_09_gtsum()`
------------------------------------------------------------------------
Required variables:
-- **`adae`**: `SAFFL`, `USUBJID`, `AESER`, `AESOC`, and the variables specified by `pref_var` and `arm_var`.
-- **`alt_counts_df`** (if specified): `SAFFL`, `USUBJID`, and the variable specified by `arm_var`.
+- **`adae`**: must contain the variables specified by `id_var`, `arm_var`, `saffl_var`, `ser_var`, `soc_var`, and `pref_var`.
+- **`alt_counts_df`** (if specified): must contain the variables specified by `id_var`, `saffl_var`, and `arm_var`.
| **Argument** | **Description** | **Default** |
|:-----------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------|
@@ -113,6 +191,7 @@ Required variables:
| `alt_counts_df` | (`character`) Alternative dataset (typically ADSL) used only to calculate column counts. | `NULL` |
| `show_colcounts` | (`flag`) Whether column counts should be printed. | `TRUE` |
| `arm_var` | (`character`) Arm variable used to split table into columns. | `"ARM"` |
+| `id_var` | (`character`) Name of the unique subject identifiers variable. | `"USUBJID"` |
| `saffl_var` | (`character`) Safety population flag variable from `adae` to include in the table. | `"SAFFL"` |
| `ser_var` | (`character`) Serious Event variable from `adae` to include in the table. | `"AESER"` |
| `pref_var` | (`character`) Preferred term variable from `adae` to include in the table. | `"AEDECOD"` |
@@ -125,10 +204,13 @@ Required variables:
| `risk_diff` | (`list` of `character` vectors) List of character vectors. Each vector must be of length 2 and contain the name of treatment arms to calculate risk difference and its 95% CI for. Those names must exist in the `arm_var` column of the dataset specified in `adae`. | `NULL` |
Source code for this function is available [here](https://github.com/pharmaverse/cardinal/blob/main/R/fda-table_09.R).
+
## Tplyr Table
-```{r tbl_tplyr, message=FALSE, warning=FALSE}
+
+ Tplyr Table Setup
+```{r tbl_typlr, echo=TRUE, eval=FALSE}
# Load Libraries & Data
library(cardinal)
@@ -139,13 +221,14 @@ adae <- random.cdisc.data::cadae
make_table_09_tplyr(adae = adae, alt_counts_df = adsl, tplyr_raw = TRUE)
```
-## Tplyr Table Setup
+
-```{r tbl_tplyr, eval=FALSE, echo=TRUE}
-```
-## Function Details: `make_table_09_tplyr`
+```{r tbl_typlr, message=FALSE, warning=FALSE, eval=TRUE}
+```
+
+ Function Details
### `make_table_09_tplyr()`
------------------------------------------------------------------------
@@ -175,4 +258,6 @@ Required variables:
| `annotations` | (named `list` of `character`) List of annotations to add to the table. Valid annotation types are `title`, `subtitles`, `main_footer`, and `prov_footer`. Each name-value pair should use the annotation type as name and the desired string as value. | `NULL` |
Source code for this function is available [here](https://github.com/pharmaverse/cardinal/blob/main/R/fda-table_09.R).
+
+
:::
diff --git a/tests/testthat/_snaps/fda-table_09.md b/tests/testthat/_snaps/fda-table_09.md
index 06ddc620..68452009 100644
--- a/tests/testthat/_snaps/fda-table_09.md
+++ b/tests/testthat/_snaps/fda-table_09.md
@@ -1,4 +1,4 @@
-# Table 09 generation works with default values
+# Table 09 (rtables) generation works with default values
Code
res
@@ -15,7 +15,7 @@
cl D 50 (37.3%) 42 (31.3%) 51 (38.6%)
dcd D.1.1.1.1 50 (37.3%) 42 (31.3%) 51 (38.6%)
-# Table 09 generation works with custom values
+# Table 09 (rtables) generation works with custom values
Code
res
@@ -48,7 +48,7 @@
n, number of patients with adverse event; PT, preferred term; SAE, serious adverse event;
SOC, System Organ Class
-# Table 09 (gt) generation works with risk difference column
+# Table 09 generation works with risk difference column
Code
res
@@ -210,3 +210,94 @@
7 cl D " 50 (48.1%)" " 42 (41.6%)" " 51 (51.5%)"
8 dcd D.1.1.1.1 " 50 (48.1%)" " 42 (41.6%)" " 51 (51.5%)"
+# Table 09 generation works with default values
+
+ Code
+ as.data.frame(res$table)
+ Output
+ **Primary System Organ Class** \n **Dictionary-Derived Term**
+ 1 Any SAE
+ 2 cl A
+ 3 dcd A.1.1.1.2
+ 4 cl B
+ 5 dcd B.1.1.1.1
+ 6 dcd B.2.2.3.1
+ 7 cl D
+ 8 dcd D.1.1.1.1
+ **A: Drug X** \nN = 134 **B: Placebo** \nN = 134
+ 1 104 (77.6%) 101 (75.4%)
+ 2 48 (35.8%) 48 (35.8%)
+ 3 48 (35.8%) 48 (35.8%)
+ 4 79 (59.0%) 78 (58.2%)
+ 5 47 (35.1%) 49 (36.6%)
+ 6 48 (35.8%) 54 (40.3%)
+ 7 50 (37.3%) 42 (31.3%)
+ 8 50 (37.3%) 42 (31.3%)
+ **C: Combination** \nN = 132
+ 1 99 (75.0%)
+ 2 50 (37.9%)
+ 3 50 (37.9%)
+ 4 76 (57.6%)
+ 5 43 (32.6%)
+ 6 51 (38.6%)
+ 7 51 (38.6%)
+ 8 51 (38.6%)
+
+---
+
+ Code
+ res$ard
+ Output
+ $tbl_hierarchical
+ # A tibble: 81 x 14
+ # Groups: group1_level [4]
+ group1 group1_level group2 group2_level variable variable_level context
+
+ 1 ARM AESOC hierarchical
+ 2 ARM AESOC hierarchical
+ 3 ARM AESOC hierarchical
+ 4 ARM AESOC hierarchical
+ 5 ARM AESOC hierarchical
+ 6 ARM AESOC hierarchical
+ 7 ARM AESOC hierarchical
+ 8 ARM AESOC hierarchical
+ 9 ARM AESOC hierarchical
+ 10 ARM AESOC hierarchical
+ # i 71 more rows
+ # i 7 more variables: stat_name , stat_label , stat ,
+ # fmt_fn , warning , error , gts_column
+
+
+# Table 09 generation works with gtsummary with custom values
+
+ Code
+ as.data.frame(res)
+ Output
+ **Primary System Organ Class** \n **Dictionary-Derived Term**
+ 1 Any SAE
+ 2 cl A
+ 3 dcd A.1.1.1.2
+ 4 cl B
+ 5 dcd B.1.1.1.1
+ 6 dcd B.2.2.3.1
+ 7 cl D
+ 8 dcd D.1.1.1.1
+ **A: Drug X** \nN = 134 **B: Placebo** \nN = 134
+ 1 104 (77.6%) 101 (75.4%)
+ 2 48 (35.8%) 48 (35.8%)
+ 3 48 (35.8%) 48 (35.8%)
+ 4 79 (59.0%) 78 (58.2%)
+ 5 47 (35.1%) 49 (36.6%)
+ 6 48 (35.8%) 54 (40.3%)
+ 7 50 (37.3%) 42 (31.3%)
+ 8 50 (37.3%) 42 (31.3%)
+ **C: Combination** \nN = 132
+ 1 99 (75.0%)
+ 2 50 (37.9%)
+ 3 50 (37.9%)
+ 4 76 (57.6%)
+ 5 43 (32.6%)
+ 6 51 (38.6%)
+ 7 51 (38.6%)
+ 8 51 (38.6%)
+
diff --git a/tests/testthat/test-fda-table_09.R b/tests/testthat/test-fda-table_09.R
index aed9ef05..dbbdfebd 100644
--- a/tests/testthat/test-fda-table_09.R
+++ b/tests/testthat/test-fda-table_09.R
@@ -1,15 +1,15 @@
adsl <- adsl_raw
adae <- adae_raw
-test_that("Table 09 generation works with default values", {
- result <- make_table_09(adae, adsl)
+test_that("Table 09 (rtables) generation works with default values", {
+ result <- make_table_09_rtables(adae, adsl)
res <- expect_silent(result)
expect_snapshot(res)
})
-test_that("Table 09 generation works with custom values", {
- result <- make_table_09(
+test_that("Table 09 (rtables) generation works with custom values", {
+ result <- make_table_09_rtables(
adae,
adsl,
lbl_overall = "Total",
@@ -39,9 +39,9 @@ test_that("Table 09 generation works with custom values", {
expect_snapshot(res)
})
-test_that("Table 09 (gt) generation works with risk difference column", {
+test_that("Table 09 generation works with risk difference column", {
risk_diff <- list(arm_x = "B: Placebo", arm_y = "A: Drug X")
- result <- make_table_09(adae, adsl, risk_diff = risk_diff)
+ result <- make_table_09_rtables(adae, adsl, risk_diff = risk_diff)
res <- expect_silent(result)
expect_snapshot(res)
@@ -348,3 +348,26 @@ test_that("make_table_09_tplyr() considers annotations if tplyr_raw = FALSE", {
expect_true(grepl(annot[["prov_footer"]][[1]], prov_footer_out))
expect_true(grepl(annot[["prov_footer"]][[2]], prov_footer_out))
})
+
+
+test_that("Table 09 generation works with default values", {
+ res <- make_table_09(adae, adsl)
+ expect_snapshot(res$table |> as.data.frame())
+ expect_snapshot(res$ard)
+
+ # no ARD
+ res2 <- make_table_09(adae, adsl, return_ard = FALSE)
+
+ # tables the same
+ expect_identical(res$table, res2)
+})
+
+
+test_that("Table 09 generation works with gtsummary with custom values", {
+ res <- make_table_09_gtsummary(
+ df = adae,
+ denominator = adsl
+ )
+
+ expect_snapshot(res |> as.data.frame())
+})