Skip to contents

These objects are imported from other packages. Follow the links below to see their documentation.

flextable

as_flextable

Usage

# S3 method for 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 = FALSE,
  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"),
  ...
)

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 use flextable::autofit() on the table.

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 use {.col_key} and {.col_val} when by has multiple stratum.

generic_labels

names of the crosstable default columns. Useful for translation for instance.

...

unused.

Value

a flextable.

Methods (by class)

  • crosstable: Turns a crosstable object into a formatted flextable.

Author

Dan Chaltiel

Examples

#Crosstables
library(crosstable)
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
crosstable_options(crosstable_fontsize_header=14,
                   crosstable_fontsize_subheaders=10,
                   crosstable_fontsize_body=8)
crosstable(iris) %>% as_flextable()
#> a flextable object.
#> col_keys: `label`, `variable`, `value` 
#> header has 1 row(s) 
#> body has 19 row(s) 
#> original dataset sample: 
#>            .id        label   variable         value
#> 1 Sepal.Length Sepal.Length  Min / Max     4.3 / 7.9
#> 2 Sepal.Length Sepal.Length  Med [IQR] 5.8 [5.1;6.4]
#> 3 Sepal.Length Sepal.Length Mean (std)     5.8 (0.8)
#> 4 Sepal.Length Sepal.Length     N (NA)       150 (0)
#> 5  Sepal.Width  Sepal.Width  Min / Max     2.0 / 4.4
crosstable(mtcars2, -model, by=c(am, vs)) %>% as_flextable(header_show_n=1:2)
#> a flextable object.
#> col_keys: `label`, `variable`, `am=auto & vs=straight`, `am=manual & vs=straight`, `am=auto & vs=vshaped`, `am=manual & vs=vshaped` 
#> header has 2 row(s) 
#> body has 42 row(s) 
#> original dataset sample: 
#>   .id               label   variable am=auto & vs=straight
#> 1 mpg   Miles/(US) gallon  Min / Max           17.8 / 24.4
#> 2 mpg   Miles/(US) gallon  Med [IQR]      21.4 [18.6;22.1]
#> 3 mpg   Miles/(US) gallon Mean (std)            20.7 (2.5)
#> 4 mpg   Miles/(US) gallon     N (NA)                 7 (0)
#> 5 cyl Number of cylinders          4            3 (27.27%)
#>   am=manual & vs=straight am=auto & vs=vshaped am=manual & vs=vshaped
#> 1             21.4 / 33.9          10.4 / 19.2            15.0 / 26.0
#> 2        30.4 [25.1;31.4]     15.2 [14.1;16.6]       20.4 [16.8;21.0]
#> 3              28.4 (4.8)           15.1 (2.8)             19.8 (4.0)
#> 4                   7 (0)               12 (0)                  6 (0)
#> 5              7 (63.64%)               0 (0%)              1 (9.09%)
crosstable(mtcars2, cols=c(mpg, cyl), by=am, effect=TRUE) %>%
   as_flextable(keep_id=TRUE, autofit=FALSE)
#> a flextable object.
#> col_keys: `.id`, `label`, `variable`, `auto`, `manual`, `effect` 
#> header has 2 row(s) 
#> body has 7 row(s) 
#> original dataset sample: 
#>   .id               label   variable             auto           manual
#> 1 mpg   Miles/(US) gallon  Min / Max      10.4 / 24.4      15.0 / 33.9
#> 2 mpg   Miles/(US) gallon  Med [IQR] 17.3 [14.9;19.2] 22.8 [21.0;30.4]
#> 3 mpg   Miles/(US) gallon Mean (std)       17.1 (3.8)       24.4 (6.2)
#> 4 mpg   Miles/(US) gallon     N (NA)           19 (0)           13 (0)
#> 5 cyl Number of cylinders          4       3 (27.27%)       8 (72.73%)
#>                                                                                                     effect
#> 1                     Difference in means (t-test CI), ref='auto'\nmanual minus auto: 7.24 [3.64 to 10.85]
#> 2                     Difference in means (t-test CI), ref='auto'\nmanual minus auto: 7.24 [3.64 to 10.85]
#> 3                     Difference in means (t-test CI), ref='auto'\nmanual minus auto: 7.24 [3.64 to 10.85]
#> 4                     Difference in means (t-test CI), ref='auto'\nmanual minus auto: 7.24 [3.64 to 10.85]
#> 5 Odds ratio [95% Wald CI], ref='manual vs auto'\n6 vs 4: 0.28 [0.03 to 1.99]\n8 vs 4: 0.06 [0.01 to 0.39]
crosstable(mtcars2, cols=c(mpg, cyl), by=am, effect=TRUE) %>%
   as_flextable(compact=TRUE, header_show_n=TRUE)
#> a flextable object.
#> col_keys: `variable`, `auto`, `manual`, `effect` 
#> header has 2 row(s) 
#> body has 9 row(s) 
#> original dataset sample: 
#>            variable             auto           manual
#> 1 Miles/(US) gallon                                  
#> 2         Min / Max      10.4 / 24.4      15.0 / 33.9
#> 3         Med [IQR] 17.3 [14.9;19.2] 22.8 [21.0;30.4]
#> 4        Mean (std)       17.1 (3.8)       24.4 (6.2)
#> 5            N (NA)           19 (0)           13 (0)
#>                                                                                 effect
#> 1 Difference in means (t-test CI), ref='auto'\nmanual minus auto: 7.24 [3.64 to 10.85]
#> 2                                                                                     
#> 3                                                                                     
#> 4                                                                                     
#> 5                                                                                     

#Renaming (because why not?)
crosstable(mtcars2, -model, by=vs, total="both", test=TRUE, effect=TRUE) %>%
   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"))
#> Warning: Be aware that automatic global testing should only be done in an exploratory
#> context, as it would cause extensive alpha inflation otherwise.
#> This warning is displayed once every 8 hours.
#> Warning: Problems occured when calculating crosstable effects (glm-logit):
#>  "glm.fit: fitted probabilities numerically 0 or 1 occurred", "glm.fit:
#>   algorithm did not converge", and "collapsing to unique 'x' values"
#>  You might want to check for complete separation or extreme outliers.
#>  Applying `forcats::fct_rev()` to some columns might help too.
#> a flextable object.
#> col_keys: `lab`, `math`, `straight`, `vshaped`, `Tot`, `fx`, `pval` 
#> header has 2 row(s) 
#> body has 47 row(s) 
#> original dataset sample: 
#>    ID                 lab       math         straight          vshaped
#> 1 mpg   Miles/(US) gallon  Min / Max      17.8 / 33.9      10.4 / 26.0
#> 2 mpg   Miles/(US) gallon  Med [IQR] 22.8 [21.4;29.6] 15.7 [14.8;19.1]
#> 3 mpg   Miles/(US) gallon Mean (std)       24.6 (5.4)       16.6 (3.9)
#> 4 mpg   Miles/(US) gallon     N (NA)           14 (0)           18 (0)
#> 5 cyl Number of cylinders          4      10 (90.91%)        1 (9.09%)
#>                Tot
#> 1      10.4 / 33.9
#> 2 19.2 [15.4;22.8]
#> 3       20.1 (6.0)
#> 4           32 (0)
#> 5      11 (34.38%)
#>                                                                                                                        fx
#> 1                        Difference in means (t-test CI), ref='straight'\nvshaped minus straight: -7.94 [-11.27 to -4.61]
#> 2                        Difference in means (t-test CI), ref='straight'\nvshaped minus straight: -7.94 [-11.27 to -4.61]
#> 3                        Difference in means (t-test CI), ref='straight'\nvshaped minus straight: -7.94 [-11.27 to -4.61]
#> 4                        Difference in means (t-test CI), ref='straight'\nvshaped minus straight: -7.94 [-11.27 to -4.61]
#> 5 Odds ratio [95% Wald CI], ref='vshaped vs straight'\n6 vs 4: 7.50 [0.72 to 180.02]\n8 vs 4: 3143660128.53 [0.00 to  NA]
#>                                                      pval
#> 1                  p value: <0.0001 \n(Two Sample t-test)
#> 2                  p value: <0.0001 \n(Two Sample t-test)
#> 3                  p value: <0.0001 \n(Two Sample t-test)
#> 4                  p value: <0.0001 \n(Two Sample t-test)
#> 5 p value: <0.0001 \n(Fisher's Exact Test for Count Data)