The most convenient way to generate a table containing R output is the function knitr::kable()
. Like figures, tables with captions will also be numbered and can be referenced in the text of your post. To do this, you will need to do two things:
- Label the R code chunk used to produce the table
- Use
knitr::kable(caption = "look at this")
to provide a table caption.
To reference the table in the text of your post, use the syntax \@ref(tab:chunk-label)
,1 where chunk-label
is the label of the chunk containing the R code that produces the table. 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 use the syntax \@ref(tab:label)
to reference the table in text. Table 1 is a simple example.
library(dplyr)
library(knitr)
ct <- mtcars[, 1:8] %>%
head(10)
ct %>%
kable("html", caption = '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 |
For some additional formatting options, you can also use the kableExtra package. Table 2 uses the kable_styling()
function to center the table. Read the vignette for more options.
library(kableExtra)
ct %>%
kable("html", caption = 'This table is centered.') %>%
kable_styling(position = "center")
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 |
Tables can also be formatted using the formattable package, in combination with knitr::kable()
and the kableExtra package. Table 3 uses the color_tile()
and color_bar()
functions. Read this vignette for more options.
library(formattable)
ct %>%
mutate(mpg = color_tile("transparent", "lightpink")(mpg),
hp = color_bar("lightseagreen")(hp)) %>%
kable("html", escape = F, caption = 'This table is colored.') %>%
kable_styling(position = "center") %>%
column_spec(4, width = "3cm")
mpg | cyl | disp | hp | drat | wt | qsec | vs |
---|---|---|---|---|---|---|---|
21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 |
21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 |
22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 |
21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 |
18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 |
18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 |
14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 |
24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 |
22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 | 22.90 | 1 |
19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 |
To make an interactive table, see our post on adding HTML widgets using the DT package.
Do not forget the leading backslash! And also note the parentheses
()
afterref
; they are not curly braces{}
.↩