Data Science Desktop Survival Guide
by Graham Williams |
|||||
Formatting Tables Using XTable |
Raw Whilst knitr::kable() provides basic functionality much more extensive control over the formatting of tables is provided by xtable. By default the table produced is called a floating table so that it floats within the document to an appropriate location.
As a floating table we will add a caption= and a table
reference label= to the table so that they do not get
lost. We can then refer to the table within the text and have the
tables appear somewhere convenient automatically. The code block
below, for example, produces Table 22.1 on
page . The table and page numbers are automatically
assigned to the table. Within LaTeX we can access the table number
using \ref{egtbl}
(22.1) and the page number using
\pageref{egtbl}
().
# Load the package from the local library into the R session.
library(xtable) # Generate a floating table with a caption. ds %>% xtable(caption="Example xtable.", label="egtbl")
Also note that by default missing values (NA) are not printed nor are the extra lines that are printed by default when using knitr::kable(). There are very many formatting options available for fine tuning how the table is to be presented and we cover some of these in the following pages. We also note that some options are provisioned by xtable::xtable() whilst others are available through xtable::print.xtable(). An example option is include.rownames= which is an option available with xtable::print.xtable(). The result is seen in Table 22.2.
|
# Display a table without row names.
ds %>% xtable(caption="Remove row numbers.", label="tblnonums") %>% print(include.rownames=FALSE)
|