R Markdown-交叉引用没有kable的HTML表

问题描述

Rmarkdown文档中有关表交叉引用的所有问题都需要使用kable进行交叉引用。如何交叉引用不是由kable生成的HTML表?例如,使用table1包:

---
title: Test Table Cross-Reference
output: bookdown::html_document2
---

This should be a cross reference to table \@ref(tab:table).

```{r table,echo = FALSE}
table1::table1(~depth + table + price | cut,data = ggplot2::diamonds)
```

解决方法

这不像kable那样简单,knitr会自动创建链接和引用,并将两者都插入最终的HTML文档中。但是,如果您可以不使用自动编号而生活,则可以轻松地自己创建链接。

您可以使用HTML创建标题,通过参数id锚定标题,然后像这样链接到该锚点(这类似于knitr自动执行的操作):

This should be a cross reference to table [1](#tab:table)

```{r table2,echo = FALSE}
table1::table1(~depth + table + price | cut,data = ggplot2::diamonds)
```

<center><p id='tab:table'> Table 1: Your Caption</p></center>

请注意,table1::table1()还有一个选项caption,您可以向其中传递一个字符串,因此类似的事情也可以起作用:

```{r table2,data = ggplot2::diamonds,caption = "<p id='tab:table'> Table 1: Your Caption</p>")
```

enter image description here