2

Folks, how do I align multiple tables side by side using the flextable R package in R markdown?

---
title: "flextables side by side"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(dplyr)
```
## Please, plot these tables side by side!
```{r pressure, echo=FALSE}
flextable(pressure[1:7,])%>% set_caption(caption = "Table 1")
flextable(cars[1:5,])%>% set_caption(caption = "Table 2")
```

There are examples for other packages:

Align multiple tables side by side

Rmarkdown side-by-side tables spacing

MsGISRocker
  • 588
  • 4
  • 21

2 Answers2

2

This is a solution for both HTML and Word output that do not require transforming tables as images.

---
title: "flextables side by side"
output:
  officedown::rdocx_document: default
  html_document: default
---

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

## two tables side by side

<!---BLOCK_MULTICOL_START--->

::::: {.row}

::: {.col-xs-12 .col-md-6}

```{r echo=FALSE}
flextable(pressure[1:7,])%>% 
  autofit() %>% 
  set_caption(caption = "Table 1")
```

`r officer::run_columnbreak()`

:::

::: {.col-xs-12 .col-md-6}

```{r echo=FALSE}
flextable(cars[1:5,])%>% 
  autofit() %>% 
  set_caption(caption = "Table 2")
```

:::

:::::

<!---BLOCK_MULTICOL_STOP{widths: [4,2], space: 0.1, sep: false}--->

blah blah blah

Word output:

Word output

HTML output:

HTML output

Note it is not possible today to get rid of the extra blank paragraph that can be seen in Word output in the second column; this paragraph contains the word instruction to insert a column break.

David Gohel
  • 9,180
  • 2
  • 16
  • 34
1

This works for me.

library(webshot)
flextable(pressure[1:7,])%>% set_caption(caption = "Table 1") %>% save_as_image("tmp1.png")
flextable(cars[1:5,])%>% set_caption(caption = "Table 2") %>% save_as_image("tmp2.png")
knitr::include_graphics(c("tmp1.png", "tmp2.png"))

enter image description here

Vons
  • 3,277
  • 2
  • 16
  • 19