【问题标题】:Explictly providing labels to knitr::kable()?显式向 knitr::kable() 提供标签?
【发布时间】: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

标签: r knitr kable


【解决方案1】:

这是你需要的吗?

来自您的next 问题的数据集

```{r}
A <- tibble::as_tibble(matrix(c(rep(0, 9), rep(1,3)), nrow = 4, dimnames = list(NULL, LETTERS[1:3])))
```

LaTeX中的一些代码:

\begin{table}[hbtp]
\captionof{table}{My table made in kable}
\label{tab:caption}
\centering
`r knitr::kable(A)`
\end{table}

不要忘记添加这些包:

header-includes:
- \usepackage{float}
- \usepackage{caption}

【讨论】:

  • 谢谢。这肯定会奏效,可能会给易辉和公司一些思考。但是,我的问题是指定一个标签值,而不是让它成为默认值(“NULL”)——即knir::kable(A, label = "1")。在上面的问题中。我是说label 只能是NULL 用于打印没有外部代码提示的标签,如上所示。有没有办法在没有外部代码的情况下使用kable() 中的label = "1"label = 1 提供相同的结果?
猜你喜欢
  • 2018-10-14
  • 2018-04-20
  • 1970-01-01
  • 2014-12-13
  • 2019-04-10
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多