Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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

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


|         &nbsp;          |  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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top