-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule6-Tables-Workbook.Rmd
87 lines (61 loc) · 3.13 KB
/
Module6-Tables-Workbook.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
title: "R Notebook"
output: html_notebook
---
# Tables in markdown
One can write tables directly in markdown:
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
But 'hardcoding' tables in this way is not good practice as it does not align with the principles of reproducible data analysis. Also, it is a lot of manual work. To show data in tables, it is better practice to tell R how to create a table based on a data frame.
There is a more extensive version of kable available, called [kableExtra](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html), which allows much more control over table layout, but we won't cover this here.
# Tables with kable
```{r echo=FALSE}
library(knitr) # Load knitr to get kable function available.
opts_chunk$set(results='asis') # default output as-is, i.e., write raw results from R into the output document
install.packages("kableExtra")
```
## Examples
For more examples see https://rdrr.io/cran/knitr/man/kable.html.
For knitr options see https://yihui.name/knitr/options/, in particular the text options
### Displaying all variables, first 5 rows
The head() function shows the first 5 rows of a data frame.
```{r}
kable(head(mtcars))
```
### Selecting columns and rows
```{r}
kable(mtcars[1:5, 1:3])
```
### Controlling number of digits displayed
```{r}
kable(head(mtcars),digits = 2, caption = "The cars")
```
## kabelExtra
For your convenience, in case you want to experiment with the features of [this extension](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) to kable. Also adding a small data frame by way of a variable dt.
```{r echo=FALSE}
library(kableExtra)
dt <- mtcars[1:5, 1:6]
```
kableExtra uses the pipe `%>% and adds layers to kable() similar in style to ggplot2 (where the plus sign plays an analogue role).
```{r}
dt %>%
kable(format = "html") %>%
kable_styling()
```
In the code above, the default styling is used. Styles can be used to make things look cooler, of course, like stripped. You can only see this in the preview, of course. (The styles come from a framework called Bootstrap, don't worry about what this means.)
```{r}
kable(dt, "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
```
**Question:** Have you noticed what the "hover" argument does?
And this is pretty cool styling of rows (one can do columns too, see in the link provided above):
```{r}
kable(dt, "html") %>%
kable_styling("striped", full_width = F) %>%
column_spec(5:7, bold = T) %>%
row_spec(3:5, bold = T, color = "white", background = "#D7261E")
```
There's lots of other formatting control possible with kabelExtra, but keep in mind that you want to keep your analysis reproducible, and simplicity helps a long way with that. kabelExtra's features are best used for the purpose of making (long, complex) tables more readable. Consider carefully how changes to your data affect table formatting and try to keep the formatting so flexible that it can work with changes in the data without requiring manual updates of the table formatting.
## Exercises