1

I have a report with many summary tables of regression output and converted them into tables using pander in the Rmarkdown document. Even though all tables are less than one page, they sometimes break and continue on two pages, which is hard to read.

I created the .tex file from .Rmd and converted longtabs to booktabs as a way but couldn't compile the document. Also, since there are so many tables, adding \pagebreak or \newpage is not an optimal solution either.

Here is a minimal example of the .Rmd file. The code here is to be pasted in a .Rmd file. How can I make the table be on the same page?

---
title: "Put tables on the same page"
author: "author"
date: May 16, 2020
output:
  pdf_document:
    extra_dependencies: ["lipsum"]
    number_sections: yes
    toc: yes
geometry: margin=1in
fontsize: 11pt
---


```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE,
                      cache = TRUE,
                      #include = FALSE,
                      message = FALSE,
                      warning = FALSE,
                      error = TRUE,
                      fig.cap = " ",
                      number_sections = TRUE)
```
```{r}
library(tidyverse)
library(pander)
library(rmarkdown)
#panderOptions('keep.trailing.zeros', TRUE)
```


\lipsum[3-5]

```{r}
m5 = lm(log(Volume)~log(Girth)*log(Height)+Girth^2+Girth^3+Girth^4 + Height^2+Height^3+rnorm(31)+rpois(31, 1),data=trees)
pander(m5)
```

I don't know how to keep all the chunks intact in this code here. Sorry if that causes confusion. It is converting the markdown code to formatting.

% relevant lines for longtable package in the preamble of generated tex document and a sample table which breaks onto two pages but is short enough to be on one page.
\usepackage{longtable,booktabs}
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}


\begin{longtable}[]{@{}cccccl@{}}
\caption{Overall Model}\tabularnewline
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{(Intercept)}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-14.89\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.038\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-393.9\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var1}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.084\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.035\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-2.383\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.017\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
*\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var2}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.016\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
11.93\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var10}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.158\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.051\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
3.096\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.002\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var3}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.058\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.013\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
4.296\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var4}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.169\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.053\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-3.209\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var5}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.28\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.054\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
5.199\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var11}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.167\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.026\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.467\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var6}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.245\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.036\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.792\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\bottomrule
\end{longtable}
5
  • 1
    If you don't want pagebreaks within a table, don't use longtable. The only reason for using longtableis to allow a table to be split over multiple pages. – leandriis May 18 '20 at 13:25
  • You can't use the triple backquote markup if the code contains triple backquotes. I used the other method (four spaces in front of each line) to mark the code. – egreg May 18 '20 at 13:28
  • I am not using longtable myself. It is generated from Rmarkdown using pander. Simply replacing this by booktabs doesn't work. Is there something I can do in Rmarkdown to use some other package like booktabs? I included that in the dependencies for that, but didn't work. – Anusha May 18 '20 at 13:30
  • I would ideally want some solution which I can specify in the preamble or .sty file or markdown or pander, as there are many such tables and do not want to edit each of them manually. – Anusha May 18 '20 at 13:33
  • @Anusha: To be honest, the automatically generated table code you included in your question is very cluttered and would need some serious cleanup to produce a decent output. This is unfortunately the case with most table generators. I can therefore only encourage you to invest the time and clean up the code while adding some improvements at the same time. – leandriis May 18 '20 at 17:53
0

Here is an improved version of your table. I have remove all unnecessary code clutter and added the siunitx package to improve the alignment of the numbers.

\documentclass{article}
\usepackage{longtable,booktabs}
\usepackage{caption}
\usepackage{siunitx}
\begin{document}
\begin{table}
\centering
\caption{Overall Model}\label{key}
\begin{tabular}[]{@{}>{\bfseries}lS[table-format=-2.3]S[table-format=1.3]S[table-format=-3.3]S[table-format=1.3]l@{}}
\toprule
 & {Estimate} & {Std. Error} & {z value} & {Pr(\textgreater\textbar z\textbar)} \\
\midrule
(Intercept) & -14.89 & 0.038 & -393.9 & 0     & * * * \\
var1        & -0.084 & 0.035 & -2.383 & 0.017 & *     \\
var2        & 0.016  & 0.001 & 11.93  & 0     & * * * \\
var10       & 0.158  & 0.051 & 3.096  & 0.002 & * *   \\
var3        & 0.058  & 0.013 & 4.296  & 0     & * * * \\
var4        & -0.169 & 0.053 & -3.209 & 0.001 & * *   \\
var5        & 0.28   & 0.054 & 5.199  & 0     & * * * \\
var11       & 0.167  & 0.026 & 6.467  & 0     & * * * \\
var6        & 0.245  & 0.036 & 6.792  & 0     & * * * \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
1
  • Thank you for the reply. This is the reason I posted the question as I wrote the entire document in Rmarkdown and it compiles nicely except the the problem on table continuing on two pages. I added the latex generated code for the table only for reference , to help detect what options could be changed in Rmarkdown or latex document as a whole. If I have to include all tables separately after writing and doing the analysis in R notebook, it would in some way defeat the purpose of using Rmarkdown and knitr. – Anusha May 18 '20 at 18:24
0

I suggest that you use kable or xtable rather than pander. You will get a table formatted using standard LaTeX syntax and lets you invoke all the kable or xtable formatting capabilities. The following simple .Rmd file is adapted from here: Prettier Result Table With broom And knitr. It does the following:

  1. Fit a linear model to the cars data
  2. Tidies the model output
  3. Formats the model output as a LaTeX table.

Specifically regarding your question about not floating the table, the example sets the table float with [H], so that it does not float at all. This is done with the kable_styling function from kableExtra. If you have a longtable, just add that option to kable().

This is the output from knitting the .Rmd file:

enter image description here

This is the Rmd file:

---
title: "TeX.SE"
output: 
   pdf_document:
     keep_tex: true
header-includes:
   - \usepackage{caption}
   - \usepackage{booktabs}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.clean = FALSE)
```

## Fit the model

```{r cars}
lmfit <- lm(mpg ~ wt, mtcars)
lmfit
```

## Cleanup model output and format table

Use \texttt{broom}'s \texttt{tidy} function to create a data frame of the model output and output that dataframe using \texttt{knitr}'s \texttt{kable} function: 

```{r output}
library(broom)        # tidyr
library(magrittr)     # %>%

library(knitr)        # kable
library(kableExtra)   # kable_styling
tidy(lmfit) %>% kable(format="latex",booktabs=TRUE,
                      caption="My table caption",digits=2) %>% 
                kable_styling(latex_options = "HOLD_position")
```
\raggedright Add \texttt{longtable} to \texttt{kable} as required. For example: \par\texttt{kable(longtable=TRUE, format="latex", booktabs=TRUE, caption="My table caption")}

This is the LaTeX code for the table of regression parameters extracted from the .tex file created by compiling the .Rmd file.

\begin{table}[H]
\caption{\label{tab:output}My table caption}
\centering
\begin{tabular}[t]{lrrrr}
\toprule
term & estimate & std.error & statistic & p.value\\
\midrule
(Intercept) & 37.29 & 1.88 & 19.86 & 0\\
wt & -5.34 & 0.56 & -9.56 & 0\\
\bottomrule
\end{tabular}
\end{table}

As you can see, it is just regular LaTeX code with the [H] option applied for table placement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.