rlayoutshinymarkdownlongtable

Insert elegant table inside a two-column layout r-markdown document


I am working on an R Markdown document that can be downloaded from a Shiny App as a pdf. I have structured my 1-page document with a layout that presents two columns at the beginning and then again one column until the end. The main issue is that in one of the two columns I can't insert a table generated, for example, through the kable() function of the knitr package, because I get the following error:

Package longtable Error: longtable not in 1-column mode

Below you can find some reproducible code:

---
output: pdf_document
header-includes:
  - \usepackage{multicol}
  - \newcommand{\btwocol}{\begin{multicols}{2}}
  - \newcommand{\etwocol}{\end{multicols}}
---

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

library(ggplot2)
library(knitr)
```

\btwocol

#### Column one
```{r}
ggplot(mtcars,
       aes(x = mpg,
           y = cyl)) +
  geom_point()
```

\columnbreak

#### Column two
```{r}
kable(mtcars[1:10, 1:2], format = "markdown")
```

\etwocol

Note that if you remove the kable() function the script can be compiled but the table is in the standard "R" format.

Thanks!


Solution

  • I found a solution by using the functionalities of the grid, gridExtra and gtable package. I created and customised the table and its caption directly inside a chunk of the R Markdown file, and with the grid.draw() function I created a graphical object of that table that can now be inserted inside a two-columns layout.