knitr's simple framework makes it easy to use tables. However, I am unsure how to explicitly provide a label.
In this example, the number supplied (1) is ignored in the caption.
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 |
Upon looking into kable(), the caption is created from two supporting functions: kable_caption() and 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>
The default NULL label becomes missing and is excluded from the caption.
> 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 = "")
}
Due to the return("") line, a label will only be generated if opts_knit$get("bookdown.internal.label") is TRUE, the format is latex, and the caption is supplied (caption is not NA or not NULL). This implies that any user-supplied label will be ignored. The documentation is also not clear what class "label" should be: a number? a character?
Question: Can you explicitly assign a label to a kable?
Note: I realize this may be more appropriate on https://github.com/yihui/knitr/issues/new; however, in accordance with the author's guidelines, I am posting it here first.
