-
-
Notifications
You must be signed in to change notification settings - Fork 11
update_ard_fmt_fn needs some love #535
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfddb66
update_ard_fmt_fn needs some love
kimjj93 847c41c
Merge remote-tracking branch 'origin/main' into kimjj93/issue449
kimjj93 e71e29b
Enhance documentation for ARD objects and list columns; update exampl…
kimjj93 55d7538
Merge branch 'main' into kimjj93/issue449
ddsjoberg 603bbf7
Merge branch 'main' into kimjj93/issue449
kimjj93 61301be
Corrected errors per GH Actions feedback.
kimjj93 40a6438
Merge branch 'main' into kimjj93/issue449
ddsjoberg 9b6f6e7
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
github-actions[bot] 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
| *.html | ||
| *.R | ||
|
|
||
| /.quarto/ | ||
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,140 @@ | ||
| --- | ||
| title: "Updating ARD Objects" | ||
| --- | ||
|
|
||
| ```{r, include = FALSE} | ||
| knitr::opts_chunk$set( | ||
| collapse = TRUE, | ||
| comment = "#>" | ||
| ) | ||
| ``` | ||
|
|
||
| ```{r setup, echo = FALSE, warning = FALSE, message = FALSE} | ||
| library(cards) | ||
| library(dplyr) | ||
| ``` | ||
|
|
||
| ## Introduction | ||
|
|
||
| ARD (Analysis Results Data) objects are data frames that contain statistical summaries. Because they are data frames, any value in the ARD can be updated using standard dplyr or base R functions. | ||
|
|
||
| ### Working with List Columns | ||
|
|
||
| ARD objects contain **list columns** such as `stat`, `fmt_fun`, `stat_label`, and others. List columns are columns where each cell can hold any R object—a number, a function, a vector, or even another data frame. If you're unfamiliar with list columns, they may look unusual when printed: | ||
|
|
||
| ```{r} | ||
| ard <- ard_summary(ADSL, variables = AGE) |> | ||
| select(variable, stat_name, stat, fmt_fun) |> | ||
| head(3) | ||
| print(ard) | ||
| ``` | ||
|
|
||
| While list columns can be updated just like any other column in a data frame, the syntax can be less intuitive. For example, to update the `fmt_fun` column for specific statistics, you might write: | ||
| ```{r} | ||
| ard |> | ||
| mutate( | ||
| fmt_fun = ifelse(stat_name %in% c("mean", "sd"), list(2L), fmt_fun) | ||
| ) | ||
| ``` | ||
|
|
||
| This requires wrapping values in `list()` and can become cumbersome for more complex updates. | ||
|
|
||
| ### Helper Functions | ||
|
|
||
| To simplify working with these list columns, {cards} provides helper functions: | ||
|
|
||
| - **`update_ard_fmt_fun()`**: Update formatting functions for specific statistics | ||
| - **`update_ard_stat_label()`**: Update statistic labels | ||
|
Melkiades marked this conversation as resolved.
|
||
|
|
||
| These functions handle the list column mechanics for you, making it easier to customize your ARD objects. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ### Updating Formatting Functions | ||
|
|
||
| By default, statistics may use simple formatting. You can update the formatting function for specific statistics: | ||
|
|
||
| ```{r} | ||
| # Create a basic ARD | ||
| ard <- ard_summary(ADSL, variables = AGE) | ||
|
|
||
| # Update formatting for mean and sd to show more decimal places | ||
| ard_updated <- ard |> | ||
| update_ard_fmt_fun( | ||
| stat_names = c("mean", "sd"), | ||
| fmt_fun = 2L # 2 decimal places | ||
| ) |> | ||
| apply_fmt_fun() | ||
|
|
||
| # View results | ||
| ard_updated |> | ||
| select(stat_name, stat, stat_fmt) | ||
| ``` | ||
|
|
||
| ### Updating Statistic Labels | ||
|
|
||
| Combine formatting updates with custom labels: | ||
|
|
||
| ```{r} | ||
| ard_summary(ADSL, variables = AGE) |> | ||
| update_ard_fmt_fun(stat_names = c("mean", "sd"), fmt_fun = 1L) |> | ||
| update_ard_stat_label( | ||
| stat_names = c("mean", "sd"), | ||
| stat_label = "Mean (SD)" | ||
| ) |> | ||
| apply_fmt_fun() | ||
| ``` | ||
|
|
||
| ## Selective Updates | ||
|
|
||
| ### Filtering by Variable | ||
|
|
||
| Update formatting for specific variables only: | ||
|
|
||
| ```{r} | ||
| ard_summary(ADSL, variables = c(AGE, BMIBL)) |> | ||
| update_ard_fmt_fun( | ||
| variables = AGE, # Only update AGE | ||
| stat_names = "mean", | ||
| fmt_fun = 3L | ||
| ) |> | ||
| apply_fmt_fun() | ||
| ``` | ||
|
|
||
| ### Filtering by Group | ||
|
|
||
| When working with stratified analyses, use the `filter` argument to target specific groups: | ||
|
|
||
| ```{r} | ||
| # Update formatting only for the Placebo arm | ||
| ard_summary( | ||
| ADSL, | ||
| by = ARM, | ||
| variables = AGE, | ||
| statistic = ~ continuous_summary_fns(c("N", "mean")) | ||
| ) |> | ||
| update_ard_fmt_fun( | ||
| stat_names = "mean", | ||
| fmt_fun = 3L, | ||
| filter = group1_level == "Placebo" | ||
| ) |> | ||
| apply_fmt_fun() | ||
| ``` | ||
|
|
||
| ## Custom Formatting Functions | ||
|
|
||
| Beyond integer aliases, you can pass custom functions: | ||
|
|
||
| ```{r} | ||
| # Custom formatter that adds parentheses | ||
| format_with_parens <- function(x) { | ||
| paste0("(", format(round(x, 1), nsmall = 1), ")") | ||
| } | ||
|
|
||
| ard_summary(ADSL, variables = AGE) |> | ||
| update_ard_fmt_fun( | ||
| stat_names = "sd", | ||
| fmt_fun = format_with_parens | ||
| ) |> | ||
| apply_fmt_fun() | ||
| ``` | ||
Oops, something went wrong.
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.
should this be in the main .gitignore?