View source: R/colby_constructors.R
split_rows_by | R Documentation |
Add rows according to levels of a variable
split_rows_by(
lyt,
var,
labels_var = var,
split_label = var,
split_fun = NULL,
format = NULL,
na_str = NA_character_,
nested = TRUE,
child_labels = c("default", "visible", "hidden"),
label_pos = "hidden",
indent_mod = 0L,
page_by = FALSE,
page_prefix = split_label,
section_div = NA_character_
)
lyt |
( |
var |
( |
labels_var |
( |
split_label |
( |
split_fun |
( |
format |
( |
na_str |
( |
nested |
( |
child_labels |
( |
label_pos |
( |
indent_mod |
( |
page_by |
( |
page_prefix |
( |
section_div |
( |
A PreDataTableLayouts
object suitable for passing to further layouting functions, and to build_table()
.
User-defined custom split functions can perform any type of computation on the incoming data provided that they meet the requirements for generating "splits" of the incoming data based on the split object.
Split functions are functions that accept:
a data.frame
of incoming data to be split.
a Split object. This is largely an internal detail custom functions will not need to worry about,
but obj_name(spl)
, for example, will give the name of the split as it will appear in paths in the resulting
table.
any pre-calculated values. If given non-NULL
values, the values returned should match these.
Should be NULL
in most cases and can usually be ignored.
any pre-calculated value labels. Same as above for values
.
if TRUE
, resulting splits that are empty are removed.
a data.frame
describing previously performed splits which collectively
arrived at df
.
The function must then output a named list
with the following elements:
the vector of all values corresponding to the splits of df
.
a list of data.frame
s representing the groupings of the actual observations from df
.
a character vector giving a string label for each value listed in the values
element above.
if present, extra arguments are to be passed to summary and analysis functions
whenever they are executed on the corresponding element of datasplit
or a subset thereof.
One way to generate custom splitting functions is to wrap existing split functions and modify either the incoming data before they are called or their outputs.
If var
is a factor with empty unobserved levels and labels_var
is specified, it must also be a factor
with the same number of levels as var
. Currently the error that occurs when this is not the case is not very
informative, but that will change in the future.
Gabriel Becker
lyt <- basic_table() %>%
split_cols_by("ARM") %>%
split_rows_by("RACE", split_fun = drop_split_levels) %>%
analyze("AGE", mean, var_labels = "Age", format = "xx.xx")
tbl <- build_table(lyt, DM)
tbl
lyt2 <- basic_table() %>%
split_cols_by("ARM") %>%
split_rows_by("RACE") %>%
analyze("AGE", mean, var_labels = "Age", format = "xx.xx")
tbl2 <- build_table(lyt2, DM)
tbl2
lyt3 <- basic_table() %>%
split_cols_by("ARM") %>%
split_cols_by("SEX") %>%
summarize_row_groups(label_fstr = "Overall (N)") %>%
split_rows_by("RACE",
split_label = "Ethnicity", labels_var = "ethn_lab",
split_fun = drop_split_levels
) %>%
summarize_row_groups("RACE", label_fstr = "%s (n)") %>%
analyze("AGE", var_labels = "Age", afun = mean, format = "xx.xx")
lyt3
library(dplyr)
DM2 <- DM %>%
filter(SEX %in% c("M", "F")) %>%
mutate(
SEX = droplevels(SEX),
gender_lab = c(
"F" = "Female", "M" = "Male",
"U" = "Unknown",
"UNDIFFERENTIATED" = "Undifferentiated"
)[SEX],
ethn_lab = c(
"ASIAN" = "Asian",
"BLACK OR AFRICAN AMERICAN" = "Black or African American",
"WHITE" = "White",
"AMERICAN INDIAN OR ALASKA NATIVE" = "American Indian or Alaska Native",
"MULTIPLE" = "Multiple",
"NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER" =
"Native Hawaiian or Other Pacific Islander",
"OTHER" = "Other", "UNKNOWN" = "Unknown"
)[RACE]
)
tbl3 <- build_table(lyt3, DM2)
tbl3
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.