r - 如何手动创建一个简单的表格并用knitr呈现它:kable in R Markdown (more precisely: in Bookdown)?

标签 r r-markdown knitr bookdown kable

手册“Bookdown:使用 R Markdown 创作书籍和技术文档”教授如何使用 knit::kable 呈现表格,从而获得表格的自动编号(以及其他好处)。 p>

还可以使用 R Markdown 代码手动创建简单的表格,非常容易阅读和编辑。

问题:如何使用 knitr::kable 呈现这样一个手动创建的表格?

请参阅下面的 MWE,它是在 R Markdown/Bookdown 项目中编写的:

---
documentclass: book
output:
  bookdown::gitbook: default
---

# Just a test

With knitr::kable, I have references to automatically numbered tables. Such as Table \@ref(tab:table1). 


```{r table1, tidy=FALSE, echo=FALSE}
knitr::kable(
  head(mtcars[, 1:2], 2), booktabs = TRUE,
  caption = 'Test table 1.'
)
```

<br>

In my project, I manually create tables with R Markdown code. However, I dont know how to: 

1. express this manually created table with knitr::kable, and 
2. thus get automatic numbering to the table.

See an example of a manually entered table below. Problems: no numbering, and how to refer to it? 

<br>

Table: Manually entered data in a simple table - easy to read, easy to edit as R Markdown code.

Column 1       Column 2
-------------  ---------
lorem          ipsum
dolor          sit

最佳答案

您可以手动完成:

---
documentclass: book
output:
  bookdown::gitbook: default
---

# Just a test

With knitr::kable, I have references to automatically numbered tables. Such as Table \@ref(tab:table1). 


```{r table1, tidy=FALSE, echo=FALSE}
knitr::kable(
  head(mtcars[, 1:2], 2), booktabs = TRUE,
  caption = 'Test table 1.'
)
```

<br>
Such as Table <a href="#tab:table2">1.2</a>.</p>. 
In my project, I manually create tables with R Markdown code. However, I dont know how to: 

1. express this manually created table with knitr::kable, and 
2. thus get automatic numbering to the table.

See an example of a manually entered table below. Problems: no numbering, and how to refer to it? 

<br>      

<table>
<caption><span id="tab:table2">Table 1.2: </span>Test table 2.</caption>

Column 1       Column 2
-------------  ---------
lorem          ipsum
dolor          sit

</table>

enter image description here

关于r - 如何手动创建一个简单的表格并用knitr呈现它:kable in R Markdown (more precisely: in Bookdown)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70565733/

相关文章:

python - 如何在 RMarkdown 中渲染 Python 绘图?

r - 如何将 R 格式表呈现为 pdf 输出以及如何在表格中显示百分比

latex - 减少RMarkdown中对齐环境和文本内容之间的垂直空间

r - 在 ggplot2 中创建自定义图例

r - 绑定(bind)数组的维度

java - 某处是否有动态词/标签云 Java API?

Rmarkdown beamer 目录幻灯片,加上非当前部分的灰色

r - 如何在 `bookdown::gitbook` 中添加自定义 htmlDependency

r - knitr:有条件地评估正常 block

r - 在 ggplot 中圈出圆形条形图