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

MacQueen, Don macqueen1 at llnl.gov
Thu Nov 3 01:22:48 CET 2016


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.

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


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062



More information about the R-help mailing list