3

I'd like to describe my data table in a R Markdown file using

xtable(data, type='html')

But none of the packages I looked so far seem to be compatible with xtable in html setting, f.i. Hmisc::describe,reporttools::tableNominal.

Does anyone have a solution for this?

Example: Something like Variables Overview with xtable in R but working in Markdown/html.

Community
  • 1
  • 1
Joanne Demmler
  • 1,406
  • 11
  • 31

2 Answers2

4

try pander package. specifically pandoc.table function from that package

> pandoc.table(head(mtcars), split.tables=Inf, style='rmarkdown')


|                    |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt   |  qsec  |  vs  |  am  |  gear  |  carb  |
|:-----------------------:|:-----:|:-----:|:------:|:----:|:------:|:-----:|:------:|:----:|:----:|:------:|:------:|
|      **Mazda RX4**      |  21   |   6   |  160   | 110  |  3.9   | 2.62  | 16.46  |  0   |  1   |   4    |   4    |
|    **Mazda RX4 Wag**    |  21   |   6   |  160   | 110  |  3.9   | 2.875 | 17.02  |  0   |  1   |   4    |   4    |
|     **Datsun 710**      | 22.8  |   4   |  108   |  93  |  3.85  | 2.32  | 18.61  |  1   |  1   |   4    |   1    |
|   **Hornet 4 Drive**    | 21.4  |   6   |  258   | 110  |  3.08  | 3.215 | 19.44  |  1   |  0   |   3    |   1    |
|  **Hornet Sportabout**  | 18.7  |   8   |  360   | 175  |  3.15  | 3.44  | 17.02  |  0   |  0   |   3    |   2    |
|       **Valiant**       | 18.1  |   6   |  225   | 105  |  2.76  | 3.46  | 20.22  |  1   |  0   |   3    |   1    |

That markdown table should render as follows

enter image description here

CHP
  • 16,981
  • 4
  • 38
  • 57
  • Thanks @geektrader, I will try it out. I assumed that I needed to export it using `pandoc`, that's why I hadn't tried it yet. – Joanne Demmler Oct 21 '13 at 11:44
  • @geektrader - Thanks for promoting `pander` :) Just a small addition that could improve the usability of the package: one can set the `style` of all tables and if splitting of tables is required or not with [`panderOptions`](http://rapporter.github.io/pander/#pander-options) and then simply call `pander` on any R object. – daroczig Oct 21 '13 at 12:25
  • Darn, I'm working within a secure environment, which is too old to support the `pander` package [Revolution R Community, R 2.14.2] – Joanne Demmler Oct 21 '13 at 13:08
  • Of course this still assumes that the table you want to plot is in some sort of table format, which is NOT true for `describe` output – Joanne Demmler Oct 21 '13 at 13:22
4

Ok, I've found one option that does work well with R markdown and that is using the psych::describe command. This has the advantage that the final table is a data.frame object that can then be further manipulated.

with xtable

library(psych)
library(xtable)
table.desc <- describe(mytable)
print(xtable(table.desc), type="html")

or using Gmisc

library(psych)
table.desc <- describe(mytable)
table.prep <- as.matrix(table.desc)
library(Gmisc)
htmlTable(table.prep)

Please note that in this example you do want to include the rownames, as they are part of the describe output. Also Gmisc inherits the Hmisc::describe command and has thus to be loaded AFTER creating the stats table.

Joanne Demmler
  • 1,406
  • 11
  • 31