25.14 Formatting Tables Using XTable

REVIEW Whilst knitr::kable() provides basic functionality much more extensive control over the formatting of tables is provided by (Dahl et al. 2019). 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 ?? on page . The table and page numbers are automatically assigned to the table. Within LaTeX we can access the table number using \@ref(egtbl) (??) and the page number using \pageref{egtbl} ().

# Load the package from the local library into the R session.


# Generate a floating table with a caption.

ds %>% xtable(caption="Example xtable.", label="egtbl")
% latex table generated in R 4.0.5 by xtable 1.8-4 package % Fri May 7 09:51:39 2021

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 ??.

# Display a table without row names.

ds %>% 
  xtable(caption="Remove row numbers.", label="tblnonums") %>% 
% latex table generated in R 4.0.5 by xtable 1.8-4 package % Fri May 7 09:51:39 2021


Dahl, David B., David Scott, Charles Roosen, Arni Magnusson, and Jonathan Swinton. 2019. Xtable: Export Tables to LaTeX or HTML. http://xtable.r-forge.r-project.org/.

Your donation will support ongoing development 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-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.