Rotate a table from R markdown in pdf

本秂侑毒 提交于 2019-12-03 23:17:06

The out.extra='angle=90' only works on Figures, and unfortunately not tables. Here are several potential approaches:

KableExtra (Rotate Page)

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 (Scale Width)

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")

For more examples of the kableExtra package, check out the package here: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

Stargazer (Rotate Table)

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")
```

This requires \usepackage{dcolumn} in LaTeX preamble

Read more about customising your LaTex preamble here: https://tex.stackexchange.com/questions/171711/how-to-include-latex-package-in-r-markdown

You can add some LATEX code in your Rmd file :

\usepackage{lscape}
\usepackage{pdfpages}

Some text on a portrait page.

\newpage
\blandscape

## Title, lorem ipsum

```{r, results = "asis"}
kable(iris[1:5,], caption = "Lorem again")
```
Lorem ipsum...

\elandscape

Some other text on a portrait page.

Can you just use t()?

library(xtable)
xtable(t(iris[1:5,]))

If your table is still to long, split it up into multiple tables. e.g.:

splits = floor(seq(1, ncol(iris), length=5))
for(i in 2:length(splits)){
 mini.tab = iris[ , splits[i-1]:splits[i]]
 xtable(mini.tab)
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!