R Markdown - 交叉引用 HTML table 没有 `kable`

R Markdown - cross reference an HTML table without `kable`

Rmarkdown文档中所有关于table交叉引用的问题都需要使用kable交叉引用。我如何交叉引用不是由 kable 生成的 HTML table?例如,使用 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 会自动创建 link 和引用并将两者插入到最终的 HTML 文档中。但是,如果您可以不用自动编号就可以轻松地自己创建 link。

您可以使用 HTML 创建标题,通过参数 id 锚定它,然后 link 像这样锚定(这类似于 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, echo = FALSE}
table1::table1(~depth + table + price | cut, data = ggplot2::diamonds,
               caption = "<p id='tab:table'> Table 1: Your Caption</p>")
```