Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go


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.


# Generate a floating table with a caption.

ds %>% xtable(caption="Example xtable.", label="egtbl")

Table 22.1: Example xtable.
  Location MinTemp MaxTemp Rainfall Evaporation
1 Tuggeranong 10.00 25.10 0.20  
2 Wollongong 20.80 30.70 1.00  
3 Dartmoor 5.70 14.90 1.60 2.60
4 Sale 16.80 28.80 0.00 6.00
5 WaggaWagga 7.70 31.30 0.00 4.00

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") %>%

Table 22.2: Remove row numbers.
Location MinTemp MaxTemp Rainfall Evaporation
Tuggeranong 10.00 25.10 0.20  
Wollongong 20.80 30.70 1.00  
Dartmoor 5.70 14.90 1.60 2.60
Sale 16.80 28.80 0.00 6.00
WaggaWagga 7.70 31.30 0.00 4.00

Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.