Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent table being split in Rmarkdown

Tags:

r

r-markdown

Is there a way to put my table into one page? I have a table which is broken between two pages. Here is a reproducible example:

---
output: pdf_document
---

`r rep("Text", 400)`

```{r}
# This will create a page break
knitr::kable(mtcars, caption = "A split table")
```

This produces a 2 page pdf with a broken table as follows::

enter image description here

like image 845
Bustergun Avatar asked Feb 26 '18 08:02

Bustergun


People also ask

How do you break a line in Markdown in R?

To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return. Let’s look at an example. Here, we did not specify two trailing spaces between the two sentences in the first (top) group.

Why can't I reference a plot or table in Markdown?

You need to make sure that you actually print the table or plot. If you create the plot and save it, but do not print it in the document, then you will not be able to reference the plot or table. Go to this repo njtierney/rmd-errors, and give debugging some of these common rmarkdown errors a go.

What are the R Markdown shortcuts?

Here are some of the essential R Markdown shortcuts: Insert a new code chuck with Command + Option + I on a Mac, or Ctrl + Alt + I on Linux and Windows. Output your document in the format specified in your YAML header with Command + Shift + K on a Mac, or Ctrl + Shift + K on Linux and Windows. The “k” is short for “knit”!

How do I prevent floating figures in R Markdown?

The option value !H will prevent any floating within the document. We can set the default behavior for the document so that all chunks have this setting by including the following line in the first code chunk in your R Markdown document: In general, we do not recommend that users force LaTeX to stop floating figures.


1 Answers

There are two easy ways to do this:

1. Add a page break

If you are only using the PDF output, you can actually integrate LaTeX commands directly into the report. Therefore the \newpage command will force a pagebreak as follows:

---
output: pdf_document
---

`r rep("Text", 400)`

\newpage
```{r}
knitr::kable(mtcars, caption = "A fixed table")
```

enter image description here

2. Use Page Floats:

As RMarkdown uses LaTeX to build the PDF, you can take advantage of the page floats feature. This can take some getting used to, but rather than locking the figure or table in a set position, LaTeX will try and place the figure in a position which it deems "best". With tables, it will try and remove any page breaks.

Here is an example. The table will float to the second page. Make sure to update the YAML to include the header-includes: argument as well:

---
output: pdf_document
header-includes:
   - \usepackage{booktabs}
---

`r rep("Text", 400)`

```{r}
knitr::kable(mtcars, format = "latex",
             caption = "A caption",
             booktabs = TRUE,
             longtable = FALSE)
```

`r rep("Text", 200)`

If you look at the output, you will see that the second chunk of text continues on the same page as the first, and that the table is centered on the second page. This is all done automatically by LaTeX and can avoid pain down the road if you add more text before the table which might have previously caused the table to be broken again.

enter image description here

Hope that helps.

like image 61
Michael Harper Avatar answered Sep 28 '22 16:09

Michael Harper