Using table caption on R markdown file using knitr to use in pandoc to convert to pdf

Jd Baba picture Jd Baba · Mar 6, 2013 · Viewed 17.4k times · Source

I am wondering if it is possible to use the table captions like figure captions using knitr in .Rmd file ?

I saw options for figure caption but I couldn't see the option for the table caption. I also want to remove the message such as "% latex table generated in R 2.15.2 by xtable 1.7-0 package % Wed Mar 06 15:02:11 2013" .

I used X table to create the table: The sample code I used is as follows:

```{r table2, results='asis', message=FALSE} 
library(xtable) 
print(xtable(head(iris))) 
``` 

The table I got after processing through pandoc is as follows:

enter image description here

I tried to use message=FALSE in Rmd file to get rid of the message shown above. I also want to know if it is possible to automatically add the caption for table in Rmd ?

By caption I mean something like below (this is for the figure) and the figure number is automatically updated.

This output is a snapshot from the pdf generated by pdf using the markdown file created by knitr.

enter image description here

Thank you.

Answer

daroczig picture daroczig · Mar 7, 2013

If you do not insist on using a LaTeX/HTML-only solution with the otherwise awesome xtable package, you might achieve the same with Pandoc's markdown. One option is to add the caption manually below the table, or use my R Pandoc writer package:

> library(pander)                         # load pkg
> panderOptions('table.split.table', Inf) # not to split table
> set.caption('Hello Fisher!')            # add caption
> pander(head(iris))                      # show (almost) any R object in markdown
-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
     5.1            3.5           1.4            0.2       setosa  

     4.9            3.0           1.4            0.2       setosa  

     4.7            3.2           1.3            0.2       setosa  

     4.6            3.1           1.5            0.2       setosa  

     5.0            3.6           1.4            0.2       setosa  

     5.4            3.9           1.7            0.4       setosa  
-------------------------------------------------------------------

Table: Hello Fisher!

Then use Pandoc to convert this markdown file to HTML, LaTeX, docx, odt or any other popular document formats.