Packages such as gt and flextable produce highly flexible and beautiful HTML tables, but using features such as math in them will not result in a properly rendered Word .docx file. Using R functions that render HTML that is simple but flexible results in well-rendered Word and HTML. Here is an example using the Hmisc package summaryM function and its print method, which uses the htmlTable package by Max Gordon, which you can use in this context to produce a wide variety of nice tables.
Quarto allows you to include Markdown inside the html as described here. I define a little function mk to facitate that.
abc represent the lower quartile a, the median b, and the upper quartile c for continuous variables. x ± s represents X ± 1 SD. N is the number of non-missing values.
Tests used: 1Kruskal-Wallis test; 2Pearson test .
The HTML file is perfect. pandoc (used by Quarto) rendered the .docx table quite well, just not respecting font size changes and the middle.bold argument to put the median in a larger, bold, font.
The math rendered from summaryM output uses HTML Greek characters, subscripts, superscripts, and font size changes. Better output would have been achieved by using the mk() approach except for font size.
Here’s an example from an htmlTable package vignette.
A table with column spanners, row groups, and zebra striping
Column spanners
Cgroup 1
Cgroup 2†
1st hdr
2nd hdr
3rd hdr
4th hdr
5th hdr
6th hdr
Spanner A
Group A
1st row
1:1
1:2
1:3
1:4
1:5
1:6
2nd row
2:1
2:2
2:3
2:4
2:5
2:6
Spanner B
Group B
More
3rd row
3:1
3:2
3:3
3:4
3:5
3:6
4th row
4:1
4:2
4:3
4:4
4:5
4:6
5th row
5:1
5:2
5:3
5:4
5:5
5:6
6th row
6:1
6:2
6:3
6:4
6:5
6:6
7th row
7:1
7:2
7:3
7:4
7:5
7:6
8th row
8:1
8:2
8:3
8:4
8:5
8:6
† A table footer commment
The Word result isn’t perfect, but not bad.
Other R Packages for Table Making
The kableExtra package will not work for docx output. Try gt using an example from here. Sometimes adding prefer-html: true in the yaml header will make simple html tables render properly in Word.
require(gt)d <- airquality[1:10,]d$Year <-1973gt(d) |>tab_header(title ="New York Air Quality Measurements",subtitle ="Daily measurements in New York City (May 1-10, 1973)" ) |>tab_spanner(label ="Time",columns =c(Year, Month, Day) ) |>tab_spanner(label ="Measurement",columns =c(Ozone, Solar.R, Wind, Temp) ) |>cols_move_to_start(columns =c(Year, Month, Day) ) |>cols_label(Ozone =md("Ozone,<br>_ppbV_"),Solar.R =md("Solar R.,<br>$$\\text{cal}/m^2$$"),Wind =md("Wind,<br>mph"),Temp =md("Temp,<br>$$^{\\circ}F$$") )
New York Air Quality Measurements
Daily measurements in New York City (May 1-10, 1973)
Time
Measurement
Year
Month
Day
Ozone, ppbV
Solar R.,
$$\text{cal}/m^2$$
Wind,
mph
Temp,
$$^{\circ}F$$
1973
5
1
41
190
7.4
67
1973
5
2
36
118
8.0
72
1973
5
3
12
149
12.6
74
1973
5
4
18
313
11.5
62
1973
5
5
NA
NA
14.3
56
1973
5
6
28
NA
14.9
66
1973
5
7
23
299
8.6
65
1973
5
8
19
99
13.8
59
1973
5
9
8
19
20.1
61
1973
5
10
NA
194
8.6
69
Math expressions rendered fine in HTML but not in Word.
rms Package Model Output Example
Here I fit an ordinal regression model on a continuous response variable using the orm function in rms, the use the orm print method. With options(prTYpe='html') in effect the results are rendered in HTML.
The Word result is excellent except for loss of the long overbar over \(|\Pr(Y \geq \text{median}) - \frac{1}{2}|\) and the coefficient table header being repeated.