rr-markdownmarkdown

How to avoid columns of a table to be split across two rows in rmarkdown


I am trying to create a table and knit to PDF with an rmarkdown file:

---
title: "Untitled"
author: "L"
date: "2025-03-13"
output: pdf_document
---

```{r}

set.seed(84)
dt <- data.frame(month = rep(c(1:12), 10),
           value = sample(c("A", "B", "C"), 120, replace = TRUE))

addmargins(round(prop.table(table(dt$value, dt$month), 1)*100,2))

the knitted pdf file looks like this:

enter image description here

I do not like that the columns of the table are not on a single row;

I have tried many different options, like adding \tiny before creating the table, or by changing the font or the margins; however, I was not able to get the table on one row only, as the table was split even if a lot of space was left on the other side:

enter image description here

How can I force the table to be in one row only and not be split (even if this breaks the page or margins)?


Solution

  • Set width in options(). We can use the 'setup' code chunk right after the YAML for this.

    ---
    title: "Untitled"
    author: "L"
    date: "2025-03-13"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    options(width=100)
    ```
    
    ```{r}
    set.seed(84)
    dt = data.frame(month = rep(c(1:12), 10), 
                    value = sample(c('A', 'B', 'C'), 120, replace=TRUE))
    addmargins(round(prop.table(table(dt$value, dt$month), 1) * 100, 2))
    ```
    

    Could not find a duplicate yet; just this. Cannot believe. If someone links the dupe, I will delete.