Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

knitr - Rotate a table from R markdown in 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.

Is it possible to rotate or shrink the table? Ideally this would be done without having to switch to LaTeX formatting.

My Attempts:

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.

The code I'm using to knit is:

Rscript -e "library(knitr); knit('table.Rmd', 'table.md')"

And to convert to pdf:

pandoc table.md -o table.pdf
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...