Getting nice-looking tables wider than a page when rendering to R Markdown HTML

I need to create a few tables in an R Markdown HTML report, for dataframes which are wider than a page. kable (which is my to-go solution for tables in R Markdown - I'm a simple guy) doesn't do a great job: the table just continues and "disappears" through the right-hand side of the page. pander (which on my system is available as an import from skimr) looks better:

---
title: "Untitled"
author: "anon"
date: "`r Sys.Date()`"
output: html_document
# output: html_vignette

---

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

test

```{r }
set.seed(1)
df <- matrix(rnorm(120), 3, 40)
index <- sample(1:120,30)
df[index] <- NA
df <- data.frame(df)
pander(df %>% summarize_all(function(x) sum(is.na(x))/length(x) * 100))

```

but I was looking for something with a little bit more "oomph". Specifically, I'd like the headers row to be colored in light grey. I get the desired color if I change the output: html_document line in the YAML header to output: html_vignette, however I also get ugly-looking irregular line breaks, unlike for output: html_document where all lines had the same length:

Any suggestions? Ideally, the package/solution should be simple - I don't need very fancy effects, just a constant width (like in output: html_document ) but a colored header. Also, since my numbers are "percentage of missingness", as you can see, it would be great if I could either add a title to the table, or add % symbols after each entry.

1 Like

I'm a fan of the formattable package, which can be used for tables that are very simply formatted and much more complex formatting. Check out the vignette

1 Like

There are a few threads here that cover a bunch of the nice (and plentiful) R Markdown table-formatting packages here on the site you might want to take a look at:

1 Like

Sounds good! I will check and let you know

Thanks for the links! I had found the first thread, but not the second. I decided to start a new one anyway, because my question focused specifically on simple packages (so, no xtable) with just a few frills (line breaks, colored header, percentage signs and maybe titles). But I really like your post on the comparison among different packages! I will read through the threads more carefully. From a cursory look, it seems like huxtable is the most flexible one, but formattable (also suggested by @jasonparker here), seems to be extremely simple to use, and has a great HTML output.

1 Like