简体   繁体   English

使用knitr在R降价文件上使用表格标题以在pandoc中使用以转换为pdf

[英]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 ?我想知道是否可以在 .Rmd 文件中使用 knitr 使用表格标题,如图形标题?

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" .我还想删除诸如"% 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:我用X表建表:我用的示例代码如下:

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

The table I got after processing through pandoc is as follows:我通过pandoc处理后得到的表如下:

在此处输入图片说明

I tried to use message=FALSE in Rmd file to get rid of the message shown above.我试图在 Rmd 文件中使用 message=FALSE 来摆脱上面显示的消息。 I also want to know if it is possible to automatically add the caption for table in Rmd ?我也想知道是否可以在 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.此输出是 pdf 使用 knitr 创建的 markdown 文件生成的 pdf 的快照。

在此处输入图片说明

Thank you.谢谢你。

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.如果您不坚持将 LaTeX/HTML-only 解决方案与其他很棒的xtable包一起使用,您可能会通过 Pandoc 的降价实现相同的效果。 One option is to add the caption manually below the table, or use my R Pandoc writer package :一种选择是在表格下方手动添加标题,或使用我的R Pandoc writer 包

> 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.然后使用 Pandoc 将此 Markdown 文件转换为 HTML、LaTeX、docx、odt 或任何其他流行的文档格式。

You can insert tables with automatically numbered captions in markdown for processing with pandoc using straight knitr code.您可以在 Markdown 中插入带有自动编号标题的表格,以便使用直 knitr 代码使用 pandoc 进行处理。 Insert this code snippet at the top of your .rmd file:在 .rmd 文件的顶部插入此代码片段:

```{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'.通过覆盖输出钩子和使用 tab.cap,你不需要用 results='asis' 来混淆你的块选项。

Thanks Knitr!谢谢针织!

PS: If you want to convert to latex/pdf you would probably want latex to number the tables for you. PS:如果您想转换为 Latex/pdf,您可能希望 Latex 为您编号表格。 In that case you could change tn(options$tab.cap) to paste('\\n\\n:', options$tab.cap, sep='') - but I haven't tested this.在这种情况下,您可以将tn(options$tab.cap)更改为paste('\\n\\n:', options$tab.cap, sep='') - 但我还没有测试过。

You can accomplish this with xtable .您可以使用xtable完成此xtable Add caption to xtable and comment=FALSE to the print function.caption添加到xtable并将comment=FALSEprint功能。

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

See the xtable and print.xtable documentation.请参阅xtableprint.xtable文档。

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.重要的一步是放入选项xtable.floating = TRUE因为这样 LaTeX 将识别表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM