mpg | cyl | disp | hp | drat | wt | |
---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 |
Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 |
Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 |
Hornet Sportabout | 18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 |
Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 |
Duster 360 | 14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 |
Merc 240D | 24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 |
Merc 230 | 22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 |
Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 |
Note: | ||||||
kable |
4 Tables intro
This chapter shows a few simple examples of including tables and getting cross-referencing to work across formats (HTML, Word, PDF). See Chapter 6 for more examples and comparisons of different table outputs.
In this chapter, I am going to use {flextable} for Word and HTML and {kabelExtra} for PDF. See Chapter 6 for a comparison of {flextable}, {kableExtra} and {gt}. There is a current problem that Quarto is not processing the cross-references with {flextable} into PDF and Word. But this is a known problem and they are working on it. {flextable} is the only table package that I have found the tends to work as expected across platforms. The {officer} package uses it so it works well with Word and works well with LaTeX.
*Note, I am using some customized functions to be able have a uniform look for my tables. These are in tables/_common.R
.
4.1 Example table
This is an example a table. We can reference Table 4.1 (note this is broken in Word output) easily and it is auto-numbered.
4.2 Including table files
It is often good to have your files in separate files so that when you edit your tables, you only have to work on the table code.
```{r child=here::here("tables", "Table_flex.Rmd")}
```
Df | Deviance | Resid. Df | Resid. Dev | |
---|---|---|---|---|
NULL | 99 | 129.5 | ||
ethnicty | 3 | 47.2 | 96 | 82.2 |
grade | 1 | 1.7 | 95 | 80.5 |
ethnicty:grade | 3 | 7.2 | 92 | 73.3 |
We can add a captions to a flextable with set_caption
but then we won’t have access to Quarto’s cross-format (Word, HTML, PDF) cross-referencing engine. We can also use tab.cap="caption"
in the chunk yaml but again we don’t get the cross-referencing engine.
Code
set_caption(ft,
caption = "a table caption with set_caption",
style = "Table Caption")
4.3 Cross-references
In Quarto, table links use the table label @tbl-tablabel
where tablabel
is the label you put on the table chunk. In the text it looks like this Table 4.3 (note this is broken in Word output). The chunk yaml looks like this
#| label: tbl-tablabel
#| tbl-cap: "my caption"
Year | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1954 | NA | NA | NA | NA | NA | NA | 1 | 2 | 3 | 5 | 5 | 6 |
1955 | 7 | 8 | 8 | 10 | 11 | 12 | 11 | 10 | 12 | 12 | 12 | 14 |
1956 | 17 | 20 | 22 | 24 | 26 | 28 | 30 | 30 | 32 | 33 | 35 | 36 |
1957 | 37 | 39 | 40 | 42 | 43 | 43 | 44 | 46 | 47 | 48 | 51 | 54 |
1958 | 54 | 55 | 57 | 57 | 57 | 58 | 58 | 61 | 62 | 62 | 63 | 63 |
1959 | 65 | 67 | 68 | 70 | 71 | 72 | 72 | 73 | 74 | 74 | 75 | 76 |
1960 | 78 | 79 | 78 | 80 | 83 | 83 | 86 | 86 | 87 | 88 | 89 | 90 |
1961 | 91 | 90 | 90 | 90 | 90 | 92 | 93 | 93 | 94 | 94 | 95 | 96 |
1962 | 95 | 96 | 95 | 93 | 94 | 93 | 95 | 97 | 98 | 99 | NA | NA |
Note: | ||||||||||||
kable |