Is there a way to easily export R tables to a simple HTML page?
5 Answers
The xtable
function in the xtable
package can export R tables to HTML tables. This blog entry describes how you can create HTML pages from Sweave documents.

- 6,903
- 1
- 29
- 31
-
10`print(xtable(tb), type = "html")`, to be precise. Thanks for the link! – aL3xa Aug 09 '10 at 08:49
-
@DavidB Based on the others' comments, you can use this code `print(xtable(tb), type = "html",file = 'myfile.html')` whhere you can find the exported file in your work directory. – Mohamed Rahouma Aug 03 '23 at 04:46
It might be worth mentioning that there has been a new package specifically designed to convert (and style with css) data.frames (or tables) into HTML tables in an easy and intuitive way. It is called tableHTML
. You can see a simple example below:
library(tableHTML)
#create an html table
tableHTML(mtcars)
#and to export in a file
write_tableHTML(tableHTML(mtcars), file = 'myfile.html')
You can see a detailed tutorial here as well.

- 37,047
- 12
- 77
- 87
The grammar of tables package gt is also an option.
Here's the example from the docs for generating a HTML table:
library(gt)
tab_html <-
gtcars %>%
dplyr::select(mfr, model, msrp) %>%
dplyr::slice(1:5) %>%
gt() %>%
tab_header(
title = md("Data listing from **gtcars**"),
subtitle = md("`gtcars` is an R dataset")
) %>%
as_raw_html()

- 9,802
- 2
- 74
- 62
In an issue for the DT package, someone posted how to use the DT package to get html in tables. I've pasted the relevant example code, modifying it to reference all columns with: targets = "_all"
.
library(DT)
render <- c(
"function(data, type, row){",
" if(type === 'sort'){",
" var parser = new DOMParser();",
" var doc = parser.parseFromString(data, 'text/html');",
" data = doc.querySelector('a').innerText;",
" }",
" return data;",
"}"
)
dat <- data.frame(
a = c("AAA", "BBB", "CCC"),
b = c(
'<a href="#" id = "Z" onclick = "Ztest">aaaaa</a>',
'<a href="#" id = "A" onclick = "Atest">bbbbb</a>',
'<a href="#" id = "J" onclick = "Jtest">jjjjj</a>'
)
)
datatable(
dat,
escape = FALSE,
options = list(
columnDefs = list(
list(targets = "_all", render = JS(render))
)
)
)
I hope this helps.

- 182
- 1
- 8