View source: R/colby_constructors.R
split_cols_by | R Documentation |
Will generate children for each subset of a categorical variable
split_cols_by(
lyt,
var,
labels_var = var,
split_label = var,
split_fun = NULL,
format = NULL,
nested = TRUE,
child_labels = c("default", "visible", "hidden"),
extra_args = list(),
ref_group = NULL
)
lyt |
layout object pre-data used for tabulation |
var |
string, variable name |
labels_var |
string, name of variable containing labels to be displayed
for the values of |
split_label |
string. Label string to be associated with the table generated by the split. Not to be confused with labels assigned to each child (which are based on the data and type of split during tabulation). |
split_fun |
function/NULL. custom splitting function See
|
format |
|
nested |
boolean. Should this layout instruction be applied within the
existing layout structure if possible ( |
child_labels |
string. One of |
extra_args |
list. Extra arguments to be passed to the tabulation function. Element position in the list corresponds to the children of this split. Named elements in the child-specific lists are ignored if they do not match a formal argument of the tabulation function. |
ref_group |
character(1) or |
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 contract for generating 'splits' of the incoming data 'based on' the split object.
Split functions are functions that accept:
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 likely be ignored
Any pre-calculated value labels. Same as above for
values
If TRUE
, resulting splits that are empty should be
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.frames 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.
Gabriel Becker
lyt <- basic_table() %>%
split_cols_by("ARM") %>%
analyze(c("AGE", "BMRKR2"))
tbl <- build_table(lyt, ex_adsl)
tbl
# Let's look at the splits in more detail
lyt1 <- basic_table() %>% split_cols_by("ARM")
lyt1
# add an analysis (summary)
lyt2 <- lyt1 %>%
analyze(c("AGE", "COUNTRY"), afun = list_wrap_x(summary) ,
format = "xx.xx")
lyt2
tbl2 <- build_table(lyt2, DM)
tbl2
# By default sequentially adding layouts results in nesting
library(dplyr)
DM_MF <- DM %>% filter(SEX %in% c("M", "F")) %>%
mutate(SEX = droplevels(SEX))
lyt3 <- basic_table() %>% split_cols_by("ARM") %>%
split_cols_by("SEX") %>%
analyze(c("AGE", "COUNTRY"), afun = list_wrap_x(summary),
format = "xx.xx")
lyt3
tbl3 <- build_table(lyt3, DM_MF)
tbl3
# nested=TRUE vs not
lyt4 <- basic_table() %>% split_cols_by("ARM") %>%
split_rows_by("SEX", split_fun = drop_split_levels) %>%
split_rows_by("RACE", split_fun = drop_split_levels) %>%
analyze("AGE")
lyt4
tbl4 <- build_table(lyt4, DM)
tbl4
lyt5 <- basic_table() %>% split_cols_by("ARM") %>%
split_rows_by("SEX", split_fun= drop_split_levels) %>%
analyze("AGE") %>%
split_rows_by("RACE", nested=FALSE, split_fun = drop_split_levels) %>%
analyze("AGE")
lyt5
tbl5 <- build_table(lyt5, DM)
tbl5
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.