14
votes

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.

4
I think that much of the answers you seek are through using ?xtable and ?print.xtable. Look at type and captionTyler Rinker
for the LaTeX comment, the issue has been solved in xtable; see github.com/yihui/knitr-book/issues/3 (the new version of xtable is on CRAN now)Yihui Xie
@ Yihui: I upgraded my xtable from CRAN and now I have latest version and I am still getting the same output. I used the code <pre>{r table2, results='asis', message=FALSE,echo=FALSE} library(xtable) print(xtable(head(iris))) </pre>. Did I do anything wrong ?Jd Baba
@Jdbaba read the documentation ?print.xtable and see the comment argumentYihui Xie
So, here is the solution: print(xtable(yourtable, comment = getOption("xtable.comment", FALSE))Maximilian

4 Answers

12
votes

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.

10
votes

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.

5
votes

You can accomplish this with xtable. Add caption to xtable and comment=FALSE to the print function.

print(
  xtable(
    head(iris),
    caption = 'Iris data'
  ),
  comment = FALSE,
  type = 'latex'
)

See the xtable and print.xtable documentation.

0
votes

I know this is since many years ago, but if there is someone like me who arrived here looking for an answer I'll tell you the answer that I found to the same problem.

It was really simply, we just need to put:

```{r mostrarSerie,results='asis',echo=FALSE}
library(xtable)
options(xtable.floating = TRUE)
print(xtable(AP, digits = 0,caption = "Serie de tiempo"),comment = FALSE)
```

The important step is to put into the options xtable.floating = TRUE because so LaTeX will recognize the table.