How to Format Kable Table When Knit from .Rmd to Word (With Bookdown)

How to format kable table when knit from .rmd to Word (with bookdown)

Pandoc version ≥2

Yes, this can be done with Pandoc (see David's answer)

Original answer follows:

Pandoc version <2

The conversion to word is made via pandoc. Currently pandoc only creates four type of tables,

  • simple tables
  • multiline_tables
  • grid_tables
  • pipe_tables

Some of those supported formats are demontrated in pander and in the pandoc manual p 35-39.

So you cannot create the stripped table currently with pandoc.

You also have a good summary of how you can use tables in rmarkdown.rstudio.

RMarkdown - word output - hot to display tables correctly

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

Getting simple table to a word list with Kable

You can try this:

   ```{r, echo = FALSE}

library(kableExtra)
library(data.table)

table3 <- data.frame("uno, dos, tres, cuatro, cinco")

table_t3 <- transpose(table3)
colnames(table_t3) <- NULL

kable(table_t3, format = "latex", align ="|c|", booktabs = T) %>%
kable_styling(latex_options =c("striped", "hold_position"))

```

Sample Image


For the long list:
add the full_width = TRUE to the kable_styling

kable_styling(latex_options =c("striped", "hold_position"), full_width = TRUE)

But borders don't work in this case.

Sample Image

How to do specific table in bookdown with kableExtra

Maybe this could help you:

---
title: "Untitled"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```

```{r}
text_tbl <- data.frame(
Items = c(" $<-$ ", " $$\\%$$in$$\\%$$ "),
Features = c(
" Here it's an example: $x <- 3$",
" Here it's an example: $c(1, 1, 1, 2, 5, 8, 10) $$\\%$$in$$\\%$$ 1$ "
)
)
kbl(text_tbl, booktabs = T, escape = F) %>%
kable_styling(full_width = F) %>%
column_spec(1, bold = T, color = "red") %>%
column_spec(2, width = "30em")
```

1 - escape = F was added to the function kbl

2 - To insert %, you need this: $$\\%$$

Display correlation matrix in word document using knitr

The simple way would be:

---
title: "Correlation table"
author: "mk"
date: "17/01/2022"
output: word_document
---
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
df <- cbind(table1, table2)
knitr::kable(df)

you can play with styles in different ways:

  • kable: https://bookdown.org/yihui/rmarkdown-cookbook/kable.html
  • flextable: How to format kable table when knit from .rmd to Word (with bookdown)
  • officeverse set: https://ardata-fr.github.io/officeverse/officer-for-word.html#tables

Regards, Grzegorz

R Markdown - knitting to bookdown::word_document2 fails because of officer::ftext used in inline code

The .Rmd file started knitting properly to word after I installed and loaded package officedown version 0.1.0. I initially tried with version 0.2.0, but then if I used non-alphanumeric characters as text input in officer::ftext function (eg. '%', '[') in the word output it appeared as an xml structure instead of formatted text.

Credit goes to David Gohel, developer of the officer packages who pointed me to the officedown package, see discussion on his github page:
https://github.com/davidgohel/officer/issues/402

Rmarkdown setting the position of kable

You can replace "hold_position" from claudius answer with "HOLD_position":

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
kable_styling(latex_options = "HOLD_position")
```

As mentionned in the kableExtra package:

if you find hold_position is not powerful enough to literally PIN your table in the exact position, you may want to use HOLD_position, which is a more powerful version of this feature. For those who are familiar with TeX, hold_position uses[!h] and HOLD_position uses [H] and the float package.

ref: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

You may also want to control figure position by adding fig.pos='H' to the figure chunk header.

How to generate automatic numbering of table titles in word using knit in Rmarkdown?

To do this you will need to use the bookdown extensions to markdown. The following is an example rmarkdown (.Rmd) file which does want you want:

---
output:
bookdown::word_document2
---

See Table \@ref(tab:myfirsttable).

```{r myfirsttable, echo = FALSE}
knitr::kable(head(cars, 3), caption = "First three rows of cars dataset")
```

See Table \@ref(tab:mysecondtable).

```{r mysecondtable, echo = FALSE}
knitr::kable(head(iris, 3), caption = "First three rows of iris dataset")
```

Once generated the word document looks like:

Sample Image

For more information see:

https://bookdown.org/yihui/bookdown/a-single-document.html
https://bookdown.org/yihui/bookdown/tables.html



Related Topics



Leave a reply



Submit