Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating Markdown tables in R with KnitR

I am just starting to learn about KnitR and the use of Markdown in generating R documents and reports. This looks to be perfect for a lot of the day to day reporting that I have to do with my job. However, one thing that I'm not seeing is an easy way to print data frames and tables using Markdown formatting (sort of like xtable, but with Markdown instead of LaTeX or HTML). I know that I can just embed the HTML output from xtable, but I was wondering if there were any Markdown-based solutions?

like image 382
TARehman Avatar asked Mar 18 '13 22:03

TARehman


People also ask

What is the function of knitr in creating markdown document?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What is knitr :: Kable?

10.1 The function knitr::kable() The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

Can you insert a table into R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.


2 Answers

Now knitr (since version 1.3) package include the kable function for a creation tables:

> library(knitr) > kable(head(iris[,1:3]), format = "markdown") |  Sepal.Length|  Sepal.Width|  Petal.Length| |-------------:|------------:|-------------:| |           5,1|          3,5|           1,4| |           4,9|          3,0|           1,4| |           4,7|          3,2|           1,3| |           4,6|          3,1|           1,5| |           5,0|          3,6|           1,4| |           5,4|          3,9|           1,7| 

UPDATED: if you get raw markdown in a document try setup results = "asis" chunk option.

like image 196
Artem Klevtsov Avatar answered Oct 06 '22 01:10

Artem Klevtsov


Two packages that will do this are pander

library(devtools) install_github('pander', 'Rapporter') 

Or ascii

pander is a slightly different approach to report construction, (but can be useful for this feature).

ascii will allow you to print with type = 'pandoc (or various other markdown flavours)

library(ascii) print(ascii(head(iris[,1:3])), type = 'pandoc')        **Sepal.Length**   **Sepal.Width**   **Petal.Length**   --- ------------------ ----------------- ------------------ 1   5.10               3.50              1.40               2   4.90               3.00              1.40               3   4.70               3.20              1.30               4   4.60               3.10              1.50               5   5.00               3.60              1.40               6   5.40               3.90              1.70               --- ------------------ ----------------- ------------------ 

Note that in both these cases, it is directed towards using pandoc to convert from markdown to your desired document type, however using style='rmarkdown' will create tables that are compatible with this markdown package and inbuilt conversion in rstudio.

like image 23
mnel Avatar answered Oct 06 '22 02:10

mnel