Knitr & Rmarkdown Docx Tables

Formatting tables in R Markdown to export to MS Word document

expss uses htmlTable package for table rendering. Unfortunately, htmlTable doesn't support word output.
However, you can use split_table_to_df and kable functions. They give you table-like output in the Microsoft Word. See example:

library(expss)
library(knitr)
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (1000 lbs)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)

cro(mtcars$am, mtcars$vs) %>%
split_table_to_df() %>%
kable()

R markdown does not covert .tex table to word document, while it works fine in pdf. How to make both result consistent?

The idea behind raw snippets is that it allows to include some extra content, that does not apply to other formats. So normally, R Markdown won't touch these snippets but pass them through, but only when the output format matches. That's why the table shows up in PDF, but not Docx.

R Markdown is very flexible, so we can modify its behavior. The best approach here is to use a Lua filter to parse the LaTeX code when going to a format that does not support LaTeX snippets.

Save the following code to a file parse-latex.lua, and put it in the same directory as your .Rmd file.

if FORMAT == 'latex' then
return {}
end

function RawBlock (raw)
if raw.format:match 'tex' then
print(raw.text)
return pandoc.read(raw.text, 'latex').blocks
end
end

Then instruct R Markdown to invoke the filter by changing your YAML header to

---
title: "word_test"
output:
word_document:
pandoc_args:
- '--lua-filter=parse-latex.lua'
date: '2022-08-09'
---

There is one remaining problem though: pandoc is not good at knowing when the parser should be in math mode, so the \ifmmode in your \sym definition doesn't work the way it does in LaTeX. Replace it with this instead:

\def\sym#1{\textsuperscript{#1}}

Your tables should now show up even in docx output.


I've put the filter into a repo at tarleb/parse-latex; see there for more details and Quarto usage instructions.

rmarkdown: kable, xtable or tab_df tables in Word doc

I experimented with flextable quite a bit the last few days and it might be the best option when you have to work with Word:

---
output:
word_document: default
---

```{r setup, include=FALSE}
data("mtcars")
library(tidyverse)
library(flextable)
```

```{r, results='asis'}
mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp),
wt = mean(wt),
n = n()
) %>%
flextable() %>%
align(part = "all") %>% # left align
set_caption(caption = "Table 1: Example") %>%
font(fontname = "Calibri (Body)", part = "all") %>%
fontsize(size = 10, part = "body") %>%
# add footer if you want
# add_footer_row(values = "* p < 0.05. ** p < 0.01. *** p < 0.001.",
# colwidths = 4) %>%
theme_booktabs() %>% # default theme
autofit()

```

Sample Image

RMarkdown - word output - hot to display tables correctly

This and this seem to suggest using another R package for getting tables in MSWord.



Related Topics



Leave a reply



Submit