从pdf的R markdown旋转表格 [英] Rotate a table from R markdown in pdf
问题描述
我正在用R Markdown编写,并且具有很大的列联表.我正在使用pandoc将R markdown文档转换为PDF.
I'm writing in R Markdown and have a contingency table that is quite wide. I am converting the R markdown document to a PDF using pandoc.
是否可以旋转或收缩桌子?理想情况下,无需切换为LaTeX格式即可完成此操作.
Is it possible to rotate or shrink the table? Ideally this would be done without having to switch to LaTeX formatting.
我的尝试:
我一直在滥用knitr中的Figure选项来尝试这种操作,但是无论我使用kable还是xtable,我都没有任何运气.我尝试过的一些排列包括:
I've been abusing the figure options in knitr to attempt this, but whether I use kable or xtable, I haven't had any luck. Some permutations I have tried include:
```{r out.extra='angle=90', results='asis'}
library(knitr)
kable(iris[1:5,])
```
``{r size='footnotesize', results='asis'}
library(knitr)
kable(iris[1:5,])
```
```{r out.extra='angle=90', results='asis'}
library(xtable)
xtable(iris[1:5,])
```
```{r size='footnotesize', results='asis'}
library(xtable)
xtable(iris[1:5,])
```
所有这些都很好地显示了表格,但不要旋转表格.
All of these show the table nicely, but do not rotate it.
我用来编织的代码是:
Rscript -e "library(knitr); knit('table.Rmd', 'table.md')"
并转换为pdf:
pandoc table.md -o table.pdf
推荐答案
out.extra='angle=90'
仅适用于图形,但不适用于表格.以下是几种可能的方法:
The out.extra='angle=90'
only works on Figures, and unfortunately not tables. Here are several potential approaches:
KableExtra(旋转页面)
您可以使用有用的附加程序包kableExtra
轻松旋转表.具体来说,landscape()
函数会将表格放在单个横向页面上.对于无法放置的宽表很有用
可以打印在纵向页面上.
You can easily rotate tables using the useful addon package kableExtra
. Specifically, the landscape()
function will put the table on an single landscape page. It’s useful for wide tables that can’t
be printed on a portrait page.
library(kableExtra)
kable(iris[1:5,],
format = "latex", booktabs = TRUE) %>%
kableExtra::landscape()
这些功能的局限性在于它会强制生成新页面,因此根据表的大小,它可能会留出一些空白.
The limitation of these function is that it does force a new page, so depending on the size of your table it could leave a bit of blank space.
KableExtra(比例宽度)
您可以使用功能kable_styling(latex_options = "scale_down")
缩放表格的宽度.这将迫使表格达到页面的宽度.
You can scale the width of the table using the function kable_styling(latex_options = "scale_down")
. This will force the table to the width of the page.
kable(iris[1:5,],
format = "latex", booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
有关kableExtra软件包的更多示例,请在此处查看该软件包: https://haozhu233. github.io/kableExtra/awesome_table_in_pdf.pdf
观星台(旋转桌)
还可以使用其他选项,但是这些选项在很大程度上需要安装其他LaTeX软件包.例如,stargazer
程序包可以使用float.env
参数以横向打印表格:
Other options are available, but these largely require the installation of additional LaTeX packages. For example, the stargazer
package can print tables in landscape using the float.env
argument:
```{r, results="asis"}
stargazer(iris[1:5,],
float.env = "sidewaystable")
```
这需要LaTeX序言中的\usepackage{dcolumn}
This requires \usepackage{dcolumn}
in LaTeX preamble
在此处了解有关自定义LaTex序言的更多信息: https: //tex.stackexchange.com/questions/171711/how-to-include-latex-package-in-r-markdown
这篇关于从pdf的R markdown旋转表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!