简体   繁体   English

在两列布局 r-markdown 文档中插入优雅的表格

[英]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 .我正在研究 R Markdown 文档,该文档可以从 Shiny 应用程序下载为Z43710Z45BA41917210EE0 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.我已经用一个布局构建了我的 1 页文档,该布局在开头显示两列,然后再显示一列直到结尾。 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:主要问题是,在两列之一中,我无法插入生成的表,例如,通过knitr package 的kable() function,因为我收到以下错误:

Package longtable Error: longtable not in 1-column mode Package longtable 错误:longtable 不在 1 列模式下

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.请注意,如果您删除kable() function 脚本可以编译,但表格是标准的“R”格式。

Thanks!谢谢!

I found a solution by using the functionalities of the grid , gridExtra and gtable package.我通过使用gridgridExtragtable 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. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM