Reputation: 520
The manual "Bookdown: Authoring Books and Technical Documents with R Markdown" teaches how to present tables with knitr::kable and thus get automatic numbering to the table (among other benefits).
A simple table can also be manually created with R Markdown code, which is very easily readable and editable.
Question: how do I present such a manually created table with knitr::kable?
Please see the MWE below, written in an R Markdown/Bookdown project:
---
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
Upvotes: 2
Views: 13368
Reputation: 520
After hours of searching, I have found a couple of possible solutions to my own question. As the automatic numbering is slightly tricky in bookdown with kable, I thought I could share my findings.
Solution 1:
A manually created table can be composed easily with the tribble function. Subsequently, the tribble and kable commands can be stuffed in the same R chunk in R Markdown.
However, it is not as convenient as with simple markdown to type in data with tribble, as characters/strings require quotation marks.
Note also that there has to be a caption argument for kable, otherwise the automatic numbering won't work. (See my MWE below.)
Solution 2:
Another option is to use a read.table approach. With this method, quotation marks are not needed. However, the code is not as easily readable anymore.
It seems that even with markdown, one has to compromise with the readability of the code sometimes.
EDIT: After some further searching, I've come to the conclusion that the solution 1 (see above) coupled with the use of the datapasta package and kable hits the sweet spot - you get 1) the automatic numbering provided by kable, 2) there is no need to manually insert quotation marks with the "Paste as tribble" add-in of datapasta, and finally, 3) the readability is tolerable with tribble.
MWE:
---
documentclass: book
output:
bookdown::gitbook: default
---
# Just a test
Below we have the Table \@ref(tab:table1) that has been created with the tribble function and is presented with the kable function.
```{r table1, tidy=FALSE}
library(tidyverse)
data1 <- tribble(
~"Column 1", ~"Column 2",
"lorem ipsum dolor", "sit amet, consectetur",
"adipisci elit, sed eiusmod tempor", "incidunt ut labore et dolore magna aliqua"
)
knitr::kable((data1), booktabs = TRUE,
caption = 'A test table.')
```
<br>
Another option is to use a read.table approach. With this method, quotation marks are not needed. However, the code is not as easily readable anymore. Please see the Table \@ref(tab:table2) below.
<br>
```{r table2, tidy=FALSE}
data2 <- read.table(stringsAsFactors = FALSE, header = TRUE, sep="/", text =
'Column 1/Column 2
lorem ipsum dolor/sit amet, consectetur
adipisci elit, sed eiusmod tempor/incidunt ut labore et dolore magna aliqua'
)
knitr::kable((data2), booktabs = TRUE,
caption = 'Another test table.')
```
Upvotes: 0
Reputation: 3677
You can do it manually:
---
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>
Upvotes: 2