user16024709
user16024709

Reputation: 133

Rotate table pdf output from Markdown

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.

Upvotes: 2

Views: 1233

Answers (2)

user16024709
user16024709

Reputation: 133

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

Upvotes: 1

manro
manro

Reputation: 3677

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

Upvotes: 1

Related Questions