Search code examples
pdfr-markdowngt

RMarkdown to PDF two gt() tables one next to the other


I'm developing reports in RMarkdown to PDF.

I'm generating 2 tables using the following code:

---
title: "R Notebook"
output: pdf_document
classoption: landscape
---

Blablabla

```{r include=FALSE}
library(tidyverse)
library(knitr)
library(gt)
```
\newpage

```{r}
table_1 <- filter(cars,speed<10) %>% gt() %>% as_latex()
table_2 <- filter(cars,speed<10) %>% gt() %>% as_latex()
table_1
table_2
```

And I would like to get the 2 tables side by side on the same page.

Anybody a clue on how to do this?

Thanks,

Michael


Solution

  • Use LaTeX. Replace .5 before \textwidth with how wide you want each minipage to be as a proportion of the total page width.

    ```{r, results="asis"}
    
    cat("\\begin{table}[h]
      \\centering
      \\begin{minipage}{.5\\textwidth}",
        table_1,
      "\\end{minipage}%
      \\begin{minipage}{.5\\textwidth}",
        table_2,
      "\\end{minipage}
    \\end{table}")
    ```
    

    enter image description here