Search code examples
rpdflatexr-markdown

How can I control the location of table1 (table1 package) tables in RMarkdown pdf document?


I would like to intersperse tables created using the R table1 package with text and headers in an RMarkdown pdf document. Instead of placing the headers/text and tables in the correct order, the text all appears at the beginning of the document, followed by all of the tables. How can I correct this?

Here's a small RMarkdown example that creates this error:

---
title: "Example"
author: ""
date: "`r Sys.Date()`"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(table1)
```

## R Markdown

This document will illustrate the issue I'm encoutering with table1 in RMarkdown.

```{r createdata, echo=FALSE}
# create two table1 summary tables using mtcars dataset (built-in R dataset)
df <- mtcars

df$cyl <- factor(df$cyl)
df$gear <- factor(df$gear)
label(df$mpg) <- "Mileage"
label(df$cyl) <- "Cylinders"
label(df$hp) <- "Horsepower"
label(df$gear) <- "Number of Forward Gears"
label(df$wt) <- "Weight"

units(df$mpg) <- "miles/(US)gallon"
units(df$wt) <- "1000 pounds"

caption1 <- "Motor Trend Road Test Stats, grouped by number of forward gears"

summary1 <- table1(~ mpg + hp + wt | gear, data=df,
    overall=c(right="Total"), caption=caption1)

caption2  <- "Motor Trend Road Test Stats, grouped by number of cylinders"
summary2 <- table1(~ mpg + hp + wt + gear | cyl, data=df,
    overall=c(right="Total"), caption=caption2)

```

## Heading info here

Additional text 

```{r firsttable, echo=FALSE}
summary1
```

## Another heading 

More desciption

```{r secondtable, echo=FALSE}
summary2
```

The resulting document looks like this:

enter image description here

but I would like for it to look like this: enter image description here


Solution

  • Use table1::t1kable() to convert the table1 table into a kable table and then use kableExtra::kable_styling(latex_options = "HOLD_position") to keep your table in the exact position.

    ---
    title: "Example"
    author: ""
    date: "`r Sys.Date()`"
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(table1)
    library(kableExtra)
    ```
    
    ## R Markdown
    
    ```{r createdata, echo=FALSE}
    # create two table1 summary tables using mtcars dataset (built-in R dataset)
    df <- mtcars
    
    df$cyl <- factor(df$cyl)
    df$gear <- factor(df$gear)
    label(df$mpg) <- "Mileage"
    label(df$cyl) <- "Cylinders"
    label(df$hp) <- "Horsepower"
    label(df$gear) <- "Number of Forward Gears"
    label(df$wt) <- "Weight"
    
    units(df$mpg) <- "miles/(US)gallon"
    units(df$wt) <- "1000 pounds"
    
    caption1 <- "Motor Trend Road Test Stats, grouped by number of forward gears"
    
    summary1 <- table1(~ mpg + hp + wt | gear, data=df,
        overall=c(right="Total"), caption=caption1)
    
    caption2  <- "Motor Trend Road Test Stats, grouped by number of cylinders"
    summary2 <- table1(~ mpg + hp + wt + gear | cyl, data=df,
        overall=c(right="Total"), caption=caption2)
    
    ```
    
    ## Heading info here
    
    ```{r firsttable, echo=FALSE}
    t1kable(summary1) |>
      kable_styling(latex_options = "HOLD_position")
    ```
    
    ## Another heading 
    
    ```{r secondtable, echo=FALSE}
    t1kable(summary2)|>
      kable_styling(latex_options = "HOLD_position")
    ```
    
    

    table1 tables in a fixed position


    And I will recommend exploring the table1 package vignettes for pdf and docx documents.