-
Notifications
You must be signed in to change notification settings - Fork 17
Separate CARDs section from TLG section #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeffreyad
merged 7 commits into
104_separate_out_cards
from
copilot/separate-cards-from-tlg
Nov 14, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90ada68
Initial plan
Copilot 2e3811e
Separate CARDs section from TLG section
Copilot 13ef99a
Add 'cards' to WORDLIST for spell checking
Copilot 7492543
Add rendered R script
jeffreyad 2b25a94
Enhance introduction with detailed CDISC ARD and cards package inform…
Copilot 07b682d
Rename cards demographic files to cards_demographic to avoid name con…
Copilot 23165bc
Add 'See also' section in TLG demographic linking to CARDs example
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| ## ----r preproc---------------------------------------------------------------- | ||
| library(dplyr) | ||
|
|
||
| # Create categorical variables, remove screen failures, and assign column labels | ||
| adsl <- pharmaverseadam::adsl |> | ||
| filter(!ACTARM %in% "Screen Failure") |> | ||
| mutate( | ||
| SEX = case_match(SEX, "M" ~ "MALE", "F" ~ "FEMALE"), | ||
| AGEGR1 = | ||
| case_when( | ||
| between(AGE, 18, 40) ~ "18-40", | ||
| between(AGE, 41, 64) ~ "41-64", | ||
| AGE > 64 ~ ">=65" | ||
| ) |> | ||
| factor(levels = c("18-40", "41-64", ">=65")) | ||
| ) |> | ||
| labelled::set_variable_labels( | ||
| AGE = "Age (yr)", | ||
| AGEGR1 = "Age group", | ||
| SEX = "Sex", | ||
| RACE = "Race" | ||
| ) | ||
|
|
||
| ## ----r gtsummary-table-------------------------------------------------------- | ||
| library(cards) | ||
| library(gtsummary) | ||
| theme_gtsummary_compact() # reduce default padding and font size for a gt table | ||
|
|
||
| # build the ARD with the needed summary statistics using {cards} | ||
| ard <- | ||
| ard_stack( | ||
| adsl, | ||
| ard_continuous(variables = AGE), | ||
| ard_categorical(variables = c(AGEGR1, SEX, RACE)), | ||
| .by = ACTARM, # split results by treatment arm | ||
| .attributes = TRUE # optionally include column labels in the ARD | ||
| ) | ||
|
|
||
| # use the ARD to create a demographics table using {gtsummary} | ||
| tbl_ard_summary( | ||
| cards = ard, | ||
| by = ACTARM, | ||
| include = c(AGE, AGEGR1, SEX, RACE), | ||
| type = AGE ~ "continuous2", | ||
| statistic = AGE ~ c("{N}", "{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}") | ||
| ) |> | ||
| bold_labels() |> | ||
| modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") |> # add Ns to header | ||
| modify_footnote(everything() ~ NA) # remove default footnote | ||
|
|
||
| ## ----r gtsummary-ard---------------------------------------------------------- | ||
| # build demographics table directly from a data frame | ||
| tbl <- adsl |> tbl_summary(by = ACTARM, include = c(AGE, AGEGR1, SEX, RACE)) | ||
|
|
||
| # extract ARD from table object | ||
| gather_ard(tbl)[[1]] |> select(-gts_column) # removing column so ARD fits on page | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| --- | ||
| title: "Demographic Table" | ||
| order: 1 | ||
| --- | ||
|
|
||
| ```{r setup script, include=FALSE, purl=FALSE} | ||
| invisible_hook_purl <- function(before, options, ...) { | ||
| knitr::hook_purl(before, options, ...) | ||
| NULL | ||
| } | ||
| knitr::knit_hooks$set(purl = invisible_hook_purl) | ||
| knitr::opts_chunk$set(echo = TRUE) | ||
| ``` | ||
|
|
||
| ## Introduction | ||
|
|
||
| This guide will show you how pharmaverse packages, along with some from tidyverse, can be used to create a Demographic table, using the `{pharmaverseadam}` `ADSL` data as an input. | ||
|
|
||
| ### About CDISC Analysis Results Data (ARD) | ||
|
|
||
| Analysis Results Datasets (ARDs) are a core component of the emerging [CDISC Analysis Results Standard](https://www.cdisc.org/standards/foundational/analysis-results-standard). ARDs provide a standardized, machine-readable format for representing statistical analysis results, enabling: | ||
|
|
||
| - **Reproducibility**: ARDs capture the complete analysis metadata, making results reproducible and traceable | ||
| - **Automation**: Machine-readable formats allow automated generation of tables, listings, and graphs | ||
| - **Interoperability**: Standardized structures facilitate data exchange between systems and organizations | ||
| - **Regulatory Submissions**: ARDs support modern regulatory requirements for transparent and auditable analyses | ||
|
|
||
| ### About the {cards} Package | ||
|
|
||
| The [{cards}](https://insightsengineering.github.io/cards/) package is a powerful R package designed to create Analysis Results Datasets that conform to the CDISC ARD standard. Key features include: | ||
|
|
||
| - Creates ARDs with summary statistics for continuous and categorical variables | ||
| - Supports stratified analyses (e.g., by treatment arm) | ||
| - Integrates seamlessly with table-making packages like {gtsummary} | ||
| - Captures metadata such as variable labels and analysis context | ||
| - Provides a consistent data structure for downstream reporting | ||
|
|
||
| In the examples below, we illustrate how to create demographics tables using ARDs generated with {cards}, demonstrating the modern workflow for creating analysis results that are both human-readable and machine-actionable. | ||
|
|
||
| ## Data preprocessing | ||
|
|
||
| Now we will add some pre-processing to create some extra formatted variables ready for display in the table. | ||
|
|
||
| ```{r preproc} | ||
| #| message: false | ||
| library(dplyr) | ||
|
|
||
| # Create categorical variables, remove screen failures, and assign column labels | ||
| adsl <- pharmaverseadam::adsl |> | ||
| filter(!ACTARM %in% "Screen Failure") |> | ||
| mutate( | ||
| SEX = case_match(SEX, "M" ~ "MALE", "F" ~ "FEMALE"), | ||
| AGEGR1 = | ||
| case_when( | ||
| between(AGE, 18, 40) ~ "18-40", | ||
| between(AGE, 41, 64) ~ "41-64", | ||
| AGE > 64 ~ ">=65" | ||
| ) |> | ||
| factor(levels = c("18-40", "41-64", ">=65")) | ||
| ) |> | ||
| labelled::set_variable_labels( | ||
| AGE = "Age (yr)", | ||
| AGEGR1 = "Age group", | ||
| SEX = "Sex", | ||
| RACE = "Race" | ||
| ) | ||
| ``` | ||
|
|
||
| ## {gtsummary} & {cards} | ||
|
|
||
| In the example below, we will use the [{gtsummary}](https://www.danieldsjoberg.com/gtsummary/) and [{cards}](https://insightsengineering.github.io/cards/) packages to create a demographics tables. | ||
|
|
||
| - The {cards} package creates Analysis Results Datasets (ARDs, which are a part of the [CDISC Analysis Results Standard](https://www.cdisc.org/standards/foundational/analysis-results-standard)). | ||
| - The {gtsummary} utilizes ARDs to create tables. | ||
|
|
||
| #### ARD ➡ Table | ||
|
|
||
| In the example below, we first build an ARD with the needed summary statistics using {cards}. | ||
| Then, we use the ARD to build the demographics table with {gtsummary}. | ||
|
|
||
| ```{r gtsummary-table} | ||
| #| message: false | ||
| library(cards) | ||
| library(gtsummary) | ||
| theme_gtsummary_compact() # reduce default padding and font size for a gt table | ||
|
|
||
| # build the ARD with the needed summary statistics using {cards} | ||
| ard <- | ||
| ard_stack( | ||
| adsl, | ||
| ard_continuous(variables = AGE), | ||
| ard_categorical(variables = c(AGEGR1, SEX, RACE)), | ||
| .by = ACTARM, # split results by treatment arm | ||
| .attributes = TRUE # optionally include column labels in the ARD | ||
| ) | ||
|
|
||
| # use the ARD to create a demographics table using {gtsummary} | ||
| tbl_ard_summary( | ||
| cards = ard, | ||
| by = ACTARM, | ||
| include = c(AGE, AGEGR1, SEX, RACE), | ||
| type = AGE ~ "continuous2", | ||
| statistic = AGE ~ c("{N}", "{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}") | ||
| ) |> | ||
| bold_labels() |> | ||
| modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") |> # add Ns to header | ||
| modify_footnote(everything() ~ NA) # remove default footnote | ||
| ``` | ||
|
|
||
| #### Table ➡ ARD | ||
|
|
||
| One may also build the demographics in the classic way using `gtsummary::tbl_summary()` from a data frame, then extract the ARD from the table object. | ||
|
|
||
| ```{r gtsummary-ard} | ||
| # build demographics table directly from a data frame | ||
| tbl <- adsl |> tbl_summary(by = ACTARM, include = c(AGE, AGEGR1, SEX, RACE)) | ||
|
|
||
| # extract ARD from table object | ||
| gather_ard(tbl)[[1]] |> select(-gts_column) # removing column so ARD fits on page | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --- | ||
| title: "CARDs" | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,6 +185,7 @@ cachem | |
| cairo | ||
| cairoFT | ||
| callr | ||
| cards | ||
| categorizationvars | ||
| cdisc | ||
| CDISC | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more information about CDISC Analysis Results Data and {cards} package in the introduction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Add more information about CDISC Analysis Results Data and {cards} package in the introduction at beginning of CARDs section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comprehensive information about CDISC Analysis Results Data and the {cards} package in the introduction. The enhanced introduction now includes:
Commit: 2b25a94