How can a landscape table be plotted in R Markdown (PDF output) without causing a page break to be inserted?
There is the function landscape from the kableExtra package, but this forces a page break to be inserted.
Example:
The normal behaviour for tables in R Markdown is that the will float to minimise the breaking up of text.
---
output: pdf_document
---
Some Text
```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", caption = "A table")
```
More Text
Landscape:
---
output: pdf_document
---
Some Text
```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", booktabs = T, caption = "A table") %>%
landscape()
```
More Text
You can use the LaTeX package realboxes to do what you want
---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
- \usepackage[graphicx]{realboxes}
- \usepackage{booktabs}
---
Some text
\Rotatebox{90}{
```{r, echo=FALSE, warning=FALSE}
knitr::kable(mtcars, "latex", booktabs = TRUE)
```
}
More text
This produces a single page pdf with the table presented in landscape. The problem with this approach is that it does not seem to work with a caption.
Edit You can use the caption latex package to add the caption
---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
- \usepackage[graphicx]{realboxes}
- \usepackage{booktabs}
- \usepackage{caption}
---
Some text
\begingroup
\captionsetup{type=table}
\caption{A table}
\Rotatebox{90}{
```{r, echo=FALSE, warning=FALSE}
knitr::kable(mtcars, "latex", booktabs = TRUE)
```
}
\endgroup
More text
This produces the table in landscape with caption
Related
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.
I am actually looking for a solution which provides me the output in this way -
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.
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}
```
Further you can modify it for your tasks ;)
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")
}
```
Folks, how do I align multiple tables side by side using the flextable R package in R markdown?
---
title: "flextables side by side"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(dplyr)
```
## Please, plot these tables side by side!
```{r pressure, echo=FALSE}
flextable(pressure[1:7,])%>% set_caption(caption = "Table 1")
flextable(cars[1:5,])%>% set_caption(caption = "Table 2")
```
There are examples for other packages:
Align multiple tables side by side
Rmarkdown side-by-side tables spacing
This is a solution for both HTML and Word output that do not require transforming tables as images.
---
title: "flextables side by side"
output:
officedown::rdocx_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(dplyr)
library(officedown)
```
## two tables side by side
<!---BLOCK_MULTICOL_START--->
::::: {.row}
::: {.col-xs-12 .col-md-6}
```{r echo=FALSE}
flextable(pressure[1:7,])%>%
autofit() %>%
set_caption(caption = "Table 1")
```
`r officer::run_columnbreak()`
:::
::: {.col-xs-12 .col-md-6}
```{r echo=FALSE}
flextable(cars[1:5,])%>%
autofit() %>%
set_caption(caption = "Table 2")
```
:::
:::::
<!---BLOCK_MULTICOL_STOP{widths: [4,2], space: 0.1, sep: false}--->
blah blah blah
Word output:
HTML output:
Note it is not possible today to get rid of the extra blank paragraph that can be seen in Word output in the second column; this paragraph contains the word instruction to insert a column break.
This works for me.
library(webshot)
flextable(pressure[1:7,])%>% set_caption(caption = "Table 1") %>% save_as_image("tmp1.png")
flextable(cars[1:5,])%>% set_caption(caption = "Table 2") %>% save_as_image("tmp2.png")
knitr::include_graphics(c("tmp1.png", "tmp2.png"))
How can one create a full width figure when using the twocolumn class option in knitr / R / RMarkdown / LaTex?
Based on the Knitr documentation, I've tried two approaches. Nothing short of editing the .tex file has worked for me so far.
This:
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-2]
```{r fig.env = "figure*", fig.cap = "Test"}
plot(runif(10))
```
\lipsum[3-5]
```{r fig.fullwidth = T}
plot(runif(10))
```
Results in this:
Yihui has fixed this in the development version of knitr. Yihui's response:
The option fig.env = 'figure*' should be respected now (in the dev version of knitr). But the plot will float to a new page. I guess that is a LaTeX issue orthogonal to knitr. Thanks!
Don't forget that you must include a caption for this to work.
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-3]
```{r fig.env = "figure*"}
plot(runif(10))
```
\lipsum[2]
```{r fig.env = "figure*", fig.cap = ""}
plot(runif(10))
```
\lipsum[2]
I am using the following template
---
title: "Nice try buddy"
author: "SpaceMan"
date: "13 December 2057"
output:
bookdown::pdf_document2
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
---
---
references:
- id: fenner2012a
title: One-click science marketing
container-title: Nature Materials
volume: 11
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title
\begin{equation}
f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k} \label{eq:binom}
\end{equation}
You may refer to it using `\#ref(eq:binom)`, e.g., see Equation \#ref(eq:binom).
and not a nice citation! #fenner2012a
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mytable
```
## References
where mytable is stored in R session and is generated with
mytable <- head(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
Now, this is supposed to work, but when I knit the document using
rmarkdown::render('C:\\Users\\john\\Documents\\bbv.Rmd')
the cross-reference for the table is not there! I only see ??
and the table has this weird #tab thing - how to get rid of it ?
the TOC is here even though I did not ask for it
Any ideas how to fix these issues?
Thanks!
EDIT: the weird #tab thing disappeared after a reboot.
The problem is that you are working against the intentions of kable by using it outside of an R chunk:
The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label.
https://bookdown.org/yihui/bookdown/tables.html
So the following workaround is definitely on the hacky side. Using a file foo.Rmd with
---
output:
bookdown::pdf_document2:
toc: no
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mytable
```
You can also embed tables, for example: \#ref(tab:tw2)
```{r tw2, echo=FALSE}
mytable2
```
Referencing images is easier: \#ref(fig:plt)
```{r plt, echo=FALSE, fig.cap = 'hello', fig.height=3}
myplot
```
one can process this file with a second file foo.R:
library(knitr)
library(kableExtra)
# add the label to the options that would normally be populated from the chunk options
opts_current$append(list(label = "tw"))
mytable <- head(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
opts_current$restore()
opts_current$append(list(label = "tw2"))
mytable2 <- tail(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
opts_current$restore()
myplot <- ggplot(cars, aes(x = dist, y = speed)) + geom_point()
rmarkdown::render("foo.Rmd")
In principle, you can do these commands also just at the R prompt, but I try to not use the prompt directly. BTW, I do not get the (#tab) output with your code.
However, I think it makes more sense to not work against the workings of kable. I can understand that it can make sense to separate the data manipulation fro the presentation. However, creating the table is presentation from my point of view. So instead of creating the table externally I would just create the data externally. To make this concrete, let's use a file bar.Rmd:
---
output:
bookdown::pdf_document2:
toc: no
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mydata %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
```
together with a file bar.R:
# insert data processing here
mydata <- head(cars)
rmarkdown::render("bar.Rmd")
This gives me the same output and the data processing is (initially!) separated from the presentation.
I am building a beamer presentation in rmarkdown and I have a long table that I create using kable and kableExtra, and splitting between two slides. I have been able to split the table fine, but I am having trouble getting the header to repeat. The solutions I have found on the Tex stackexchange pages suggest using different packages, or manually splitting the table in the Tex file. I don't think the additional package suggested, xtab, is compatible with kableExtra. Here is and example of what I am working on.
---
title: Title
author: James
classoption: table
output:
beamer_presentation:
keep_tex: true
includes:
in_header: ./R presentaion/header.tex
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
library(magrittr)
options(knitr.kable.NA = '')
```
```{r, echo=FALSE}
long_dt <- rbind(mtcars, mtcars)
```
#Slide {.allowframebreaks}
```{r, echo=FALSE, results='asis'}
kable(mtcars, format = "latex", longtable = TRUE, booktabs = TRUE) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("striped", "repeat_header"), font_size = 6)
```
This is what the template file looks like.
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}
\usepackage{xtab}
\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}
\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}