【发布时间】:2022-01-19 23:58:46
【问题描述】:
knitr 的简单的框架使表格的使用变得容易。但是,我不确定如何明确提供标签。
在此示例中,提供的数字 (1) 在标题中被忽略。
knitr::kable(d1, caption = "Iris head", label = "1")
Table: Iris head
| 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 |
在查看 kable() 时,标题是由两个支持函数创建的:kable_caption() 和 create_label()
> getAnywhere(kable_caption)
A single object matching ‘kable_caption’ was found
It was found in the following places
namespace:knitr
with value
function (label, caption, format)
{
if (is.null(label))
label = opts_current$get("label") #default is NULL
if (is.null(label))
label = NA
if (!is.null(caption) && !is.na(caption) && !is.na(label))
caption = paste0(
create_label(opts_knit$get("label.prefix")[["table"]], #"tab:" by default
label,
latex = (format == "latex")
),
caption
)
return(caption)
}
<bytecode: 0x00000147d346b820>
<environment: namespace:knitr>
默认的 NULL 标签丢失并从标题中排除。
> getAnywhere(create_label)
A single object matching ‘create_label’ was found
It was found in the following places
namespace:knitr
with value
function (..., latex = FALSE)
{
if (isTRUE(opts_knit$get("bookdown.internal.label"))) { #Default of option is NULL so condition by default is FALSE.
lab1 = "(\\#"
lab2 = ")"
}
else if (latex) {
lab1 = "\\label{"
lab2 = "}"
}
else {
return("")
}
paste(c(lab1, ..., lab2), collapse = "")
}
由于return("")这一行,只有opts_knit$get("bookdown.internal.label")是TRUE,格式是latex,并且提供了标题(标题不是NA或不是NULL )。这意味着任何用户提供的标签都将被忽略。文档也不清楚应该是什么类“标签”:数字?一个角色?
问题:你能明确地为kable分配标签吗?
注意:我意识到这可能更适合https://github.com/yihui/knitr/issues/new;不过按照作者的指引,我还是先发到这里吧。
【问题讨论】:
-
如何理解:显式?
-
好问题,@manro。显式的意思是告诉
kable()标签应该是什么,而不是依赖knitr自动生成标签。 -
也许我不完全理解你,但我们可以添加一个使用纯 LaTeX 的标签。是吗?
-
我在 GitHub 页面上添加了指向此问题的 knitr 链接:github.com/yihui/knitr/issues/2088