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

后端 未结 3 1349
生来不讨喜
生来不讨喜 2020-12-28 08:29

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

相关标签:
3条回答
  • 2020-12-28 08:59

    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.

    0 讨论(0)
  • 2020-12-28 09:10

    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.

    0 讨论(0)
  • 2020-12-28 09:13

    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 讨论(0)
提交回复
热议问题