Auto-format tables for printing in MS-Word documents

docx_tab(
  x,
  lang = c("eng", "ger")[1],
  pvalform = "p.value",
  asft = TRUE,
  digits = "round_smart",
  fit_mode = c("smart", "page", "none")[1],
  page_width = "A4",
  landscape = FALSE,
  verbose = FALSE,
  add_abbrev_footnote = TRUE,
  ft_padding = 2,
  ft_fontsize = 9,
  ft_border_width = 0.1,
  ...
)

Arguments

x

Table to be formatted

lang

Language for column names. Either "eng" (default) or "ger".

pvalform

Names of columns that are formatted via BioMathR::format_p(). Can be set to NULL. The default is "p.value", but note that this function first unifies multiple column names such as "Pr(>F)" or "P(>|Chi|)" into "p.value".

asft

If TRUE (default), output is formatted as flextable. If FALSE, returns a tibble with formatted values.

digits

Number of digits all numeric columns are rounded to. The default is "round_smart" which applies BioMathR::round_smart() to each numeric column individually. Can also be a numeric value.

fit_mode

How to fit the flextable to page width. Options:

  • "smart" (default): Apply smart_fit() to optimize column widths

  • "page": Stretch to full page width while maintaining smart_fit proportions

  • "none": Use flextable's default autofit, no additional fitting

page_width

Page width for fit_mode. Can be "A4" (default), "letter", "legal", "executive", or a numeric value in cm. Ignored if fit_mode = "none".

landscape

Logical. If TRUE, use landscape orientation for standard page sizes. Default is FALSE. Ignored if page_width is numeric or fit_mode = "none".

verbose

If TRUE, prints detailed information about transformations. Useful for debugging. Default is FALSE.

add_abbrev_footnote

If TRUE (default), adds footnotes explaining abbreviated column names (e.g., "df" = "Degrees of freedom").

ft_padding

Padding for flextable cells (in points). Default is 2.

ft_fontsize

Font size for flextable (in points). Default is 9.

ft_border_width

Border width for flextable (in points). Default is 0.1.

...

Other arguments passed to BioMathR::round_smart()

Examples

library(BioMathR)

# Basic usage with ANOVA table
anova <- anova(lm(weight ~ group, data = PlantGrowth))
docx_tab(anova, lang = "eng")

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

docx_tab(anova, lang = "ger")

Term

FG1

SQ2

MQ3

F-Wert

p-Wert

group

2

3,8

1,9

4,8

0,016*

Residuals

27

10,5

0,4

1Freiheitsgrade

2Summe der Quadrate

3Mittelquadrate

# Return tibble instead of flextable docx_tab(anova, asft = FALSE) #> # A tibble: 2 × 6 #> Term df SS MS `F-value` `p-value` #> <chr> <int> <dbl> <dbl> <dbl> <chr> #> 1 group 2 3.8 1.9 4.8 "0.016*" #> 2 Residuals 27 10.5 0.4 NA "" # Different rounding options before <- data.frame( V1 = c(123456, 1234), V2 = c(-123, -0.12345), V3 = c(1.0012345, 0.1), V4 = c(1.1, 0.0012345), V5 = c(1.000000012345, 0), V6 = c(NA, -5.0018), V7 = c(NA_real_, NA_real_) ) docx_tab(before) # smart rounding

V1

V2

V3

V4

V5

V6

V7

123,456

-123.0

1.001

1.100

1

1,234

-0.1

0.100

0.001

0

-5.002

docx_tab(before, digits = 2) # fixed 2 digits

V1

V2

V3

V4

V5

V6

V7

123,456

-123.00

1.0

1.1

1

1,234

-0.12

0.1

0.0

0

-5

# Different fit modes docx_tab(anova, fit_mode = "smart") # default: optimized widths

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

docx_tab(anova, fit_mode = "page") # full page width

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

docx_tab(anova, fit_mode = "none") # no fitting

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

# Customize flextable appearance docx_tab(anova, ft_fontsize = 11, ft_padding = 3)

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

# Verbose output to see transformations docx_tab(anova, verbose = TRUE) #> [docx_tab] Starting docx_tab... #> [docx_tab] Input dimensions: 2 rows, 5 columns #> [docx_tab] Parameters: lang = eng | digits = round_smart | fit_mode = smart #> [docx_tab] #> --- Table preparation --- #> [docx_tab] Converted to tibble #> [docx_tab] #> --- Column name unification --- #> [docx_tab] Unified names: term, df, sumsq, meansq, F.value, p.value #> [docx_tab] #> --- Column formatting --- #> [docx_tab] Formatting p-values: columns starting with p.value #> [docx_tab] Applying round_smart() to numeric columns #> [docx_tab] #> --- Column renaming --- #> [docx_tab] Translating column names to eng #> [docx_tab] Final column names: Term, df, SS, MS, F-value, p-value #> [docx_tab] #> --- Creating flextable --- #> [docx_tab] Creating flextable with 2 rows, 6 columns #> [docx_tab] Formatting: padding = 2 | fontsize = 9 | border = 0.1 #> [docx_tab] Applying autofit for initial column widths #> [docx_tab] #> --- Adding abbreviation footnotes --- #> [docx_tab] Found 3 abbreviated columns: df, SS, MS #> [docx_tab] Using footnote font size: 7 pt (table font: 9 pt) #> [docx_tab] Adding footnote 1: 'df' = 'degrees of freedom' #> [docx_tab] Adding footnote 2: 'SS' = 'sum of squares' #> [docx_tab] Adding footnote 3: 'MS' = 'mean squares' #> [docx_tab] Added 3 abbreviation footnotes with 7 pt font #> [docx_tab] #> --- Applying fit_mode: smart --- #> [docx_tab] Applying smart_fit() with page_width = A4 , landscape = FALSE #> [smart_fit] Starting smart_fit for flextable... #> [smart_fit] Data frame has 2 rows and 6 columns #> [smart_fit] Using A4 portrait - Width: 21 cm, Margin: 4.8 cm, Available: 16.2 cm #> [smart_fit] #> --- Calculating width requirements --- #> [smart_fit] #> Column width requirements (in cm, with autofit buffer): #> [smart_fit] Term | header: 1.22 | body: 1.84 #> [smart_fit] df | header: 0.83 | body: 0.80 #> [smart_fit] SS | header: 0.95 | body: 1.06 #> [smart_fit] MS | header: 1.01 | body: 0.88 #> [smart_fit] F-value | header: 1.55 | body: 0.88 #> [smart_fit] p-value | header: 1.55 | body: 1.36 #> [smart_fit] #> --- Determining final column widths --- #> [smart_fit] Ideal total width (no line breaks): 7.84 cm #> [smart_fit] Page width available: 16.20 cm #> [smart_fit] #> [OK] All content fits without line breaks! #> [smart_fit] Using ideal widths (table will be 7.84 cm wide) #> [smart_fit] Unused space: 8.36 cm #> [smart_fit] #> --- Final validation --- #> [smart_fit] Final table width: 7.84 cm (limit: 16.20 cm) #> [smart_fit] [OK] Table fits with 8.36 cm unused (intentional - minimizes line breaks) #> [smart_fit] #> Final column widths: #> [smart_fit] Term | 1.84 cm #> [smart_fit] df | 0.83 cm #> [smart_fit] SS | 1.06 cm #> [smart_fit] MS | 1.01 cm #> [smart_fit] F-value | 1.55 cm #> [smart_fit] p-value | 1.55 cm #> [smart_fit] #> Total: 7.84 cm / 16.20 cm available #> #> [docx_tab] #> Docx_tab complete! #>

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

# Fit to different page sizes docx_tab(anova, page_width = "letter", landscape = TRUE)

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

# Control abbreviation footnotes docx_tab(anova, add_abbrev_footnote = TRUE) # default: adds footnotes

Term

df1

SS2

MS3

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4

1degrees of freedom

2sum of squares

3mean squares

docx_tab(anova, add_abbrev_footnote = FALSE) # no footnotes

Term

df

SS

MS

F-value

p-value

group

2

3.8

1.9

4.8

0.016*

Residuals

27

10.5

0.4