2

I want to rotate table output by 90 degrees on pdf. I am using markdown to generate a report and kable to display the tables in a loop. If possible, I would like to continue using kable since there are lot of other things which are dependent on it that I haven't included in this MWE.

This is a simple example using iris dataset. I tried using landscape function from this post Rotate a table from R markdown in pdf

---
output: pdf_document
header-includes:
  \usepackage{lscape}
  \usepackage{pdfpages}
---

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

Report page - 

```{r results='asis'}  
library(knitr)
library(kableExtra)
for (i in 1:3) {
  print(landscape(kable_styling(
    kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE, 
   longtable = TRUE, row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```

But this only rotates the page number keeping the table as it is.

enter image description here

I am actually looking for a solution which provides me the output in this way -

enter image description here

To clarify, all the pages with table data in it (3 for this example) should be rotated whereas rest of them should remain as it is. Also, I need longtable = TRUE in kable since in my actual example I am printing lot of rows.

user16024709
  • 151
  • 1
  • 12
  • 1
    Does this answer your question? [Rotate a table from R markdown in pdf](https://stackoverflow.com/questions/21840878/rotate-a-table-from-r-markdown-in-pdf) – lcgodoy Oct 24 '21 at 04:30
  • @lcgodoy Not really. That is the same link which I shared in my post and I showed that it is not what I want. Am I missing something ? – user16024709 Oct 25 '21 at 02:03
  • Have you taken a look [at this answer](https://stackoverflow.com/a/47136674/9220758)? You need to add some `latex` code into the Rmd file. – lcgodoy Oct 25 '21 at 15:07

2 Answers2

1

Use package rotating

I added a simple example for you.

---
title: "test"
header-includes: \usepackage[figuresright]{rotating}
#or \usepackage[figuresleft]{rotating}
output:
  pdf_document:
    latex_engine: xelatex
---

    ```{r setup, include = FALSE}
    library(flextable)
    ft <- flextable(head(mtcars))
    ```

    \begin{sidewaysfigure}
    `r ft`
    \end{sidewaysfigure}
    ```

enter image description here

Further you can modify it for your tasks ;)

manro
  • 3,529
  • 2
  • 9
  • 22
  • Thank you for taking time to answer @manro. In my case the issue is that I am printing long tables (around 1000 rows) which prints for multiple pages. When I use this solution I get error `! Dimension too large.`. – user16024709 Oct 26 '21 at 09:34
  • @user16024709 I think, that you should split your long tables to small – manro Oct 26 '21 at 10:24
  • @user16024709 Also, try to ask there: https://tex.stackexchange.com/ An answer is interesting for me too. – manro Oct 26 '21 at 10:32
1

I found another way using rotatebox.

---
output: pdf_document
header-includes:
  \usepackage{lscape}
  \usepackage{pdfpages}
  \usepackage{graphicx}
  \usepackage[figuresright]{rotating}
---

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

Report page - 


```{r results='asis', warning=FALSE, message=FALSE}  
library(knitr)
library(kableExtra)
for (i in 1:3) {
  cat('\\rotatebox{90}{')
  print(kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
          row.names = FALSE))
  cat('}')
  cat("\n\\newpage\n")
}
```
user16024709
  • 151
  • 1
  • 12