as_flextable function

Turns a crosstable object into a formatted flextable

Turns a crosstable object into a formatted flextable

## S3 method for class 'crosstable' as_flextable( x, keep_id = FALSE, by_header = NULL, autofit = TRUE, compact = FALSE, show_test_name = TRUE, fontsizes = list(body = 11, subheaders = 11, header = 11), padding_v = NULL, remove_header_keys = TRUE, header_show_n = FALSE, header_show_n_pattern = "{.col} (N={.n})", generic_labels = list(id = ".id", variable = "variable", value = "value", total = "Total", label = "label", test = "test", effect = "effect"), ... ) as_flextable(x, ...)

Arguments

  • x: the result of crosstable().
  • keep_id: whether to keep the .id column.
  • by_header: a string to override the header if x has only one by stratum.
  • autofit: whether to automatically adjust the table. Can also be a function.
  • compact: whether to compact the table. If TRUE, see ct_compact.crosstable() to see how to use keep_id.
  • show_test_name: in the test column, show the test name.
  • fontsizes: font sizes as a list of keys. Default to list(body=11, subheaders=11, header=11). If set through arguments instead of options, all 3 names should be specified.
  • padding_v: vertical padding (body).
  • remove_header_keys: if TRUE and x has several by strata, header will only display values.
  • header_show_n: numeric vector telling on which depth the group size should be indicated in the header. You can control the pattern using option crosstable_options. See crosstable_options() for details about it. See example for use case.
  • header_show_n_pattern: glue pattern used when header_show_n==TRUE. .col is the name of the column and .n the size of the group. Default to {.col} (N={.n}); you can also use {.col_key} and {.col_val} when by has multiple stratum. To control the "Total" column, enter this as a list with names "cell" and "total".
  • generic_labels: names of the crosstable default columns. Useful for translation for instance.
  • ...: unused.

Returns

a flextable.

Methods (by class)

  • as_flextable(crosstable): Turns a crosstable object into a formatted flextable.

Examples

crosstable_options(crosstable_fontsize_header=14, crosstable_fontsize_subheaders=10, crosstable_fontsize_body=8) crosstable(iris) %>% as_flextable() crosstable(mtcars2, -model, by=c(am, vs)) %>% as_flextable(header_show_n=1) crosstable(mtcars2, cols=c(mpg, cyl), by=am, effect=TRUE) %>% as_flextable(keep_id=TRUE, autofit=FALSE) crosstable(mtcars2, cols=c(mpg, cyl), by=am, effect=TRUE, total=TRUE) %>% as_flextable(compact=TRUE, header_show_n=TRUE, header_show_n_pattern=list(cell="{.col} (N={.n})", total="Total\n(N={.n})")) #Renaming (because why not?) crosstable(mtcars2, am, by=vs, total="both", test=TRUE, effect=TRUE) %>% dplyr::rename(ID=.id, math=variable, Tot=Total, lab=label, pval=test, fx=effect) %>% as_flextable(by_header = "Engine shape", generic_labels=list(id = "ID", variable = "math", total="Tot", label = "lab", test = "pval", effect="fx"))

See Also

crosstable(), flextable::flextable(), as_gt.crosstable()

Author(s)

Dan Chaltiel