[R] Prevent a table from crossing page boundary using rmarkdown/pandoc

Duncan Murdoch murdoch.duncan at gmail.com
Thu Nov 3 10:50:05 CET 2016


On 02/11/2016 8:22 PM, MacQueen, Don wrote:
> I'm using rmarkdown::render, which executes pandoc, to create a fairly
> simple document, and I would like to prevent tables from breaking across
> pages. I've been doing some web-searching but haven't found a solution.

If you make it into a float, LaTeX will try not to split it.  You can do 
that by specifying the caption arg to kable().

I don't know if you can specify where the float appears or if you're 
stuck with the default.

Duncan Murdoch

>
> In my report's context, the table is so short that breaking it across
> pages looks shabby, whereas leaving some empty space at the bottom of the
> page is acceptable.
>
>
>
> ## this R script creates a reproducible example
> ## on my installation
> library(rmarkdown)
> library(knitr)
> sink('tmp.Rmd')
>
> cat('---
> title: A Title
> output:
>   html_document:
>     toc: yes
>     number_sections: yes
>   word_document:
>     reference_docx: report-template.docx
>   pdf_document:
>     number_sections: yes
>     toc: yes
> ---\n\n')
>
> for (i in 1:65) {
>   cat('ln',i,'\n\n')
> }
>
>
> mytbl <- data.frame(A=1:5, B=1:5)
>
> ## then, within a chunk:
>
> print(kable(mytbl))
>
> for (i in 66:70) {
>   cat('ln',i,'\n\n')
> }
>
> sink()
>
> render('tmp.Rmd','pdf_document')
>
> ## open tmp.pdf and see that the table breaks
> ## between the first and second pages
> ## possibly, other latex installations will be different
> ## (e.g., I have default letter-size paper, A4 will
> ## be different. Change the 1:65 until the table starts
> ## two or three lines from the bottom of the page.)
>
>
> An alternative to kable() that has a suitable arg, would be easiest, I
> suppose. I've looked at pander::pandoc.table without success.
>
> Naturally, I'd like the solution to work for word_document as well, but
> I'd be more than grateful for a pdf solution. It's irrelevant for html
> output.
>
> If it matters, am working in R at a shell command line, not within RStudio.
>
> Thanks in advance.
> -Don
>
>



More information about the R-help mailing list