Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 655
Jd Baba Avatar asked Mar 06 '13 21:03

Jd Baba


People also ask

How do I save an R markdown as a PDF?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

Can pandoc convert PDF to Markdown?

You can use the program pandoc on the SCF Linux and Mac machines (via the terminal window) to convert from formats such as HTML, LaTeX and Markdown to formats such as HTML, LaTeX, Word, OpenOffice, and PDF, among others.

Does Rmarkdown use pandoc?

A recent version of Pandoc (>= 1.12. 3) is required to use the rmarkdown package. RStudio also automatically includes this so you do not need to download Pandoc if you plan to use rmarkdown from the RStudio IDE.

How do you insert a table in 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”.


Video Answer


2 Answers

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.

like image 165
daroczig Avatar answered Sep 22 '22 06:09

daroczig


You can insert tables with automatically numbered captions in markdown for processing with pandoc using straight knitr code. Insert this code snippet at the top of your .rmd file:

```{r setup, echo=FALSE}
tn = local({
  i = 0
  function(x) {
    i <<- i + 1
    paste('\n\n:Table ', i, ': ', x, sep = '')
    # The : before Table tells pandoc to wrap your caption in <caption></caption>
  }
})
knit_hooks$set(tab.cap = function(before, options, envir) {
  if(!before)
    tn(options$tab.cap)
})
default_output_hook = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  if (is.null(options$tab.cap) == F)  
    x
  else
    default_output_hook(x,options)
})
```

To insert a numbered table caption:

```{r myirischunk, tab.cap="This is the head of the Iris table"}
kable(head(iris))
```

By overriding the output hook and using tab.cap you don't need to clutter your chunk options with results='asis'.

Thanks Knitr!

PS: If you want to convert to latex/pdf you would probably want latex to number the tables for you. In that case you could change tn(options$tab.cap) to paste('\n\n:', options$tab.cap, sep='') - but I haven't tested this.

like image 31
DeanK Avatar answered Sep 19 '22 06:09

DeanK