您可以使用直接knitr代码在Markdown中插入带有自动编号标题的表格,以便使用pandoc进行处理。将以下代码片段插入到您的.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)
})
```
插入带编号的表格标题:
```{r myirischunk, tab.cap="This is the head of the Iris table"}
kable(head(iris))
```
通过覆盖输出钩子并使用tab.cap,您无需在chunk选项中添加results ='asis'即可轻松输出结果。
感谢Knitr!
附注:如果要转换为latex / pdf,则可能希望latex为您编号表格。在这种情况下,您可以将tn(options $ tab.cap)
更改为paste('\n\n:', options $ tab.cap,sep ='')
- 但我还没有测试过。
?xtable
和?print.xtable
来实现。请查看type
和caption
。 - Tyler Rinkerxtable
中得到解决;请参见https://github.com/yihui/knitr-book/issues/3(新版本的`xtable`现在已经在CRAN上发布)。 - Yihui Xie{r table2, results='asis', message=FALSE,echo=FALSE} library(xtable) print(xtable(head(iris)))
</pre>。我做错了什么吗? - Jd Baba?print.xtable
并查看comment
参数。 - Yihui Xie