Home > Enterprise >  Rotate table pdf output from Markdown
Rotate table pdf output from Markdown

Time:10-28

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 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.

CodePudding user response:

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

CodePudding user response:

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