作者:Yihui Xie
译者:郑宝童
日期:2021.05.19


2.5 Tables

For now, the most convenient way to generate a table is the function knitr::kable(), because there are some internal tricks in knitr to make it work with bookdown and users do not have to know anything about these implementation details. We will explain how to use other packages and functions later in this section.

目前,生成表最方便的方法是函数 knitr::kable(),因为在knitr中有一些内部技巧可以使它与bookdown一起工作,而用户不需要知道这些实现细节。我们将在本节后面将解释如何使用其他包和函数。

Like figures, tables with captions will also be numbered and can be referenced. The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label. For example, the table label for a code chunk with the label foo will be tab:foo, and we can still use the syntax \@ref(label) to reference the table. Table 2.2 is a simple example.

与图形一样,带有标题的表也将被编号且也可以被引用。 kable() 函数将自动为表环境生成一个标签,它的形式是前缀tab: 加上块标签。例如, 带有标签foo 的代码块的表标签将是 tab:foo, 我们仍然可以使用语法\@ref(label)来引用表。表2.2 是一个简单的示例。

  1. knitr::kable(
  2. head(mtcars[, 1:8], 10), booktabs = TRUE,
  3. caption = 'A table of the first 10 rows of the mtcars data.'
  4. )

TABLE 2.2: A table of the first 10 rows of the mtcars data.

mpg cyl disp hp drat wt qsec vs
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1

If you want to put multiple tables in a single table environment, wrap the data objects (usually data frames in R) into a list. See Table 2.3 for an example. Please note that this feature is only available in HTML and PDF output.

如果希望将多个表放在一个表环境中,可以将数据对象(通常是R中的data frames)包装到一个列表中。以表 2.3 为例。请注意,此功能仅在HTML和PDF输出中可用。

  1. knitr::kable(
  2. list(
  3. head(iris[, 1:2], 3),
  4. head(mtcars[, 1:3], 5)
  5. ),
  6. caption = 'A Tale of Two Tables.', booktabs = TRUE
  7. )

TABLE 2.3: A Tale of Two Tables.

When you do not want a table to float in PDF, you may use the LaTeX package longtable, which can break a table across multiple pages. To use longtable, pass longtable = TRUE to kable(), and make sure to include \usepackage{longtable} in the LaTeX preamble (see Section 4.1 for how to customize the LaTeX preamble). Of course, this is irrelevant to HTML output, since tables in HTML do not need to float.
当您不希望一个表在PDF中浮动时,您可以使用LaTeX包longtable,它可以将一个表分解横跨多个页面。要使用longtable,请将longtable = TRUE 传递给 kable(),,并确保在LaTeX序言中包含 \usepackage{longtable}(参见4 4.1节了解如何自定义LaTeX序言)。当然,这与HTML输出无关,因为HTML中的表不需要浮动。

  1. knitr::kable(
  2. iris[1:55, ], longtable = TRUE, booktabs = TRUE,
  3. caption = 'A table generated by the longtable package.'
  4. )

TABLE 2.4: A table generated by the longtable package.

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
4.6 3.4 1.4 0.3 setosa
5.0 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3.0 1.4 0.1 setosa
4.3 3.0 1.1 0.1 setosa
5.8 4.0 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1.0 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5.0 3.0 1.6 0.2 setosa
5.0 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5.0 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3.0 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5.0 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5.0 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3.0 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5.0 3.3 1.4 0.2 setosa
7.0 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4.0 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor

Pandoc supports several types of Markdown tables, such as simple tables, multiline tables, grid tables, and pipe tables. What knitr::kable() generates is a simple table like this:

Pandoc支持多种类型的Markdown tables,例如简单表、多行表、网格表和管道表(simple tables, multiline tables, grid tables, and pipe tables)。knitr::kable()生成的是如下这样一个简单的表:

  1. Table: A simple table in Markdown.
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width
  3. ------------- ------------ ------------- ------------
  4. 5.1 3.5 1.4 0.2
  5. 4.9 3.0 1.4 0.2
  6. 4.7 3.2 1.3 0.2
  7. 4.6 3.1 1.5 0.2
  8. 5.0 3.6 1.4 0.2
  9. 5.4 3.9 1.7 0.4

You can use any types of Markdown tables in your document. To be able to cross-reference a Markdown table, it must have a labeled caption of the form Table: (\#label) Caption here, where label must have the prefix tab:, e.g., tab:simple-table.

您可以在文档中使用任何类型的Markdown表。为了能够交叉引用一个Markdown表,它必须有一个Table: (\#label) Caption here标题形式的标签的表,其中标签label 有前缀 tab:,例如 tab:simple-table

If you decide to use other R packages to generate tables, you have to make sure the label for the table environment appears in the beginning of the table caption in the form (\#label) (again, label must have the prefix tab:). You have to be very careful about the portability of the table generating function: it should work for both HTML and LaTeX output automatically, so it must consider the output format internally (check knitr::opts_knit$get('rmarkdown.pandoc.to')). When writing out an HTML table, the caption must be written in the <caption></caption> tag. For simple tables, kable() should suffice. If you have to create complicated tables (e.g., with certain cells spanning across multiple columns/rows), you will have to take the aforementioned issues into consideration.

如果你决定使用其他的R包来生成表,你必须确保表环境的标签出现在表格标题的开头,形式上为 (\#label) (同样,标签label 必须有有前缀 tab:)。您必须非常小心表生成函数的可移植性:它应该自动用于HTML和LaTeX输出,所以它必须在内部考虑输出格式(检查 knitr::opts_knit$get('rmarkdown.pandoc.to'))。当写出一个HTML表格时,标题必须写在<caption></caption>标签中。对于简单的表,可以使用kable() 。如果必须创建复杂的表(例如,某些单元格跨越多个列/行),则必须考虑上述问题。