Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit column width for paged rmarkdown tables

Let's say I have an Rmarkdown document. In that document, I create an data frame with two columns, and each of them is quite long. Also I have the "paged" output setting.

---
title: "Long Tables"
output:
  html_document:
    toc: true
    toc_depth: 3
    df_print: paged
---

```{r}
alphabet = paste(LETTERS, letters, collapse = "", sep = "")
data.frame(
    a = rep(alphabet, 10),
    b = rep(alphabet, 10)
)

When I knit this to HTML, it appears like this:

enter image description here

It is important that I can get both columns to fit on screen without the user having to click through to each column. Ideally there would be an rmarkdown setting to solve this. If not, is there a way to actually truncate the columns behind the scenes, but without actually showing the user the code that is doing the truncation? Because this will complicate the example that I am demonstrating in the document.

like image 393
Migwell Avatar asked May 09 '26 19:05

Migwell


1 Answers

Try this solution:

Your data:

```{r, echo=FALSE}
alphabet = paste(LETTERS, letters, collapse = "", sep = "")
df <- data.frame(
       a = rep(alphabet, 10),
       b = rep(alphabet, 10)
)
```

Your table:

```{r, echo=FALSE, warning=FALSE}
library(DT)
datatable(df, extensions = 'FixedColumns')
```

P.S. How to customize your table further - you can find a lot of interesting info there

enter image description here

like image 139
manro Avatar answered May 11 '26 09:05

manro