28.11 Formatting Tables Using Kable
REVIEW Including a typeset table based on a
dataset can be accomplished using knitr::kable(). Here we
will use the larger dataset from
rattle (G. Williams 2022) setting it up as a tibble::tibble()
courtesy of (Wickham et al. 2023). We will then choose specific columns and a
random selection of rows to include in the table. The source text we
include in our .Rnw file is listed in the following code
block.
<<example_kable, echo=TRUE, results="asis">>=
set.seed(42)
library(rattle)
nobs <- nrow(weatherAUS)
obs <- sample(nobs, 5)
vars <- 2:6
ds <- weatherAUS[obs, vars]
kable(ds)
@
The result (also showing the R code since we specified
echo=TRUE) is then:
# Set the seed so that results are repeatable.
set.seed(42)
# Load the package from the local library into the R session.
library(rattle)
# Record metadata for a sample of the dataset.
nobs <- nrow(weatherAUS)
obs <- sample(nobs, 5)
vars <- 2:6
ds <- weatherAUS[obs, vars]
# Generate the appropriate LaTeX code to display the data.
kable(ds)| Location | MinTemp | MaxTemp | Rainfall | Evaporation |
|---|---|---|---|---|
| Wollongong | 13.2 | 17.9 | 0 | NA |
| WaggaWagga | 12.3 | 22.7 | 0 | 4.8 |
| Melbourne | 8.2 | 15.3 | 0 | 2.4 |
| MountGinini | 16.7 | 27.3 | 0 | NA |
| Sydney | 13.2 | 20.9 | 0 | 3.4 |
Since we are working with a random sample and we would like the sampling to be repeatable we have used base::set.seed() to initialise the random number generator to a fixed value.
References
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.
Williams, Graham. 2022. Rattle: Graphical User Interface for Data Science in r. https://rattle.togaware.com/.
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0