Skip to content

Instantly share code, notes, and snippets.

@mmparker
Last active December 24, 2015 14:39
Show Gist options
  • Select an option

  • Save mmparker/6814524 to your computer and use it in GitHub Desktop.

Select an option

Save mmparker/6814524 to your computer and use it in GitHub Desktop.
Printing a list to a multicolumn HTML table with HTML inside the cells - two different methods I tried.
<head>
<style>
td {
text-align: left;
border: 1px solid #808080;
}
h4 {
background-color: #1F78B4;
color: #FFFFFF;
border: 1px solid #808080;
margin-bottom: 1px;
margin-top: 1px;
}
</style>
</head>
### The cells should be formatted like this (hand-coded) cell:
<table>
<tr><td><h4>Test Table</h4>Some text</td></tr>
</table>
### Set up an example list
```{r setup}
x <- list("<h4>Cell 1</h4>Some text",
"<h4>Cell 2</h4>Some other text",
"<h4>Cell 3</h4>Some new text",
"<h4>Cell 4</h4>Some text",
"<h4>Cell 5</h4>Some text",
"<h4>Cell 6</h4>Some text")
```
### Converting the list into a matrix, then printing with xtable
```{r matrix, echo = FALSE, results = 'asis'}
library(xtable)
x.mat <- matrix(x, ncol = 3)
print(xtable(x.mat),
type = "html",
include.rownames = FALSE,
include.colnames = FALSE)
```
### Manually constructing the table in HTML with a loop
```{r cat, echo = FALSE, results = 'asis'}
cat("<table><tr valign='top'>")
for(i in seq(from = 1, to = length(x), by = 2)) {
cat("<td>")
do.call(cat, x[seq(i, i + 1)])
cat("</td>")
}
cat("</tr></table>")
```
@mmparker
Copy link
Copy Markdown
Author

mmparker commented Oct 3, 2013

Special thanks to @yihui for this StackOverflow answer: http://stackoverflow.com/a/17739804/143319 (and the amazing knitr package)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment