Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export HTML table in R and have control over line borders?

Tags:

r

Are there any functions in R that permit the exporting of HTML tables as part of an R Markdown or related weaved document and that permit detailed control over table line borders?

E.g., Imagine a matrix like this:

x <- matrix(c("", "M", "F", "Good", 
"23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)

what command would output a proper HTML table with features like this:

       --------
       M     F
---------------
 Good  23    17
---------------
 Bad   23    4
---------------
like image 500
Jeromy Anglim Avatar asked Dec 28 '22 00:12

Jeromy Anglim


1 Answers

You might try my really young package under heavy development named to pander which tries to print R objects in pandoc markdown format.

Lazy example:

> x <- matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)
> pandoc(x)

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+

I am just working on some functions resulting in other table syntax like "simple table" or "multiline table" (see Pandoc's readme).


P. S.: you could also export this table easily to HTML (besides other formats like docx, odt etc.) with the (not yet documented) Pandoc reference class like:

> myReport <- Pandoc$new()
> myReport$add(x)
> myReport

Anonymous's report
==================
 written by *Anonymous* at *Sun May 27 21:04:22 2012*

  This report holds 1 block(s). 

---

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+


---

Proc. time:  0.009 seconds. 

> myReport$format <- 'html'
> myReport$export()

Exported to */tmp/pander-4e9c12ff63a6.[md|html]* under 0.031 seconds.

P.S. second: you could also brew (like sweave) a text document with Pandoc.brew which would auto-transform your <%=...%> tags from internal R object to Pandoc markdown format. Short example (of course this would work with file input too, now I just brew an R character vector):

> t <- '# Title
+ 
+ A nice matrix:
+ 
+ <%=matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)%>
+ 
+ Bye-bye!'
> 
> Pandoc.brew(text=t)
# Title

A nice matrix:

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+

Bye-bye!
like image 157
daroczig Avatar answered Jan 31 '23 01:01

daroczig