The marriage between R and LaTeX – Part 3 (xtable package)

In last article (part 2), I explained few commands and keys of the LaTeX tikz package and how to tweak figures generated in .tex files from R. In this article, I will explain how you can include any R table, whether a summary table or any table that shows any statistical data into your LaTeX document using the xtable package in R. It is actually an easy process. The last thing you want to do is to recreate the whole table into your LaTeX document manually.

The R xtable package – allows you to create LaTeX tables from inside R. In other words, transform any possible R table into a LaTeX table.  It allows you also to do the same for HTML, in other words transform your R tables into HTML tables (very handy for posting your R tables results on your website).

You install it by issuing the command in RStudio/R as:  install.packages(“xtable”). The R xtable package reference manual can be found here.

In R, you need to load the xtable package first by issuing:

This article is divided into 2 sections. The first explains how to export R tables into a LaTeX document. The second section explains how to do the same into HTML for web publishing.

R Tables into LaTeX documents

I will first use a dataset contained in the xtable package itself, called tli. It consists of 5 variables (columns) and 100 observations (rows or records). To load the data-set, issue the following code in the R console:

data(tli)

attach(tli)

The attach command  attaches the data-set so that variables can be called directly without referencing (i.e. without using the ). I will be using the first 20 rows (observations) from this dataset. Firstto20thRowTable <- tli[1:20, ] xtable() function is one of the most important function in the package to export R tables into LaTeX or HTML. The usage of the function is as follows: xtable(X, caption = NULL|c(“Long caption HERE”, “Short Caption HERE”) , label = NULL, align = NULL, digits= NULL, display = NULL, auto = FALSE|TRUE) Now all the arguments expect X can be optional. The vertical bar | in my notation means OR. I will not cover a lot of these arguments in this article. I will nevertheless talk about eth caption argument. The caption argument allows you to specify short and long captions for LaTeX. A lot newbies in LaTeX are not familiar with the concept of “short captions” for a figure, table, code listing or equation. The short caption is designed to appear in list of figures, list of tables, table of contents… This is specified between [ ] and the normal caption that appear next to the object is specified as usual between the { }. The following LaTeX code shows how to specify a short and long caption for any object (figure, table…) in LaTeX. \caption[Short version of your caption here]{Long version to appear next to the figure/table, equation etc…} You can of course specify one type of caption in this case only a long caption as follows: xtable(X, caption =’ This is a very long caption appearing next to the table’) The above puts the caption under the table in the LaTeX code generated which probably you don’t want that. I will explain later how to change this default behavior when I will talk about the print function in the xtable package (print.xtable). Issuing the following R code with defaults in all parameters in RStudio: xtable(Firstto20thRowTable) will give the following generated LaTeX code % latex table generated in R 3.2.5 by xtable 1.8-2 package % Thu May 18 16:43:59 2017 \begin{table}[ht] \centering \begin{tabular}{rrlllr} \hline & grade & sex & disadvg & ethnicty & tlimth \\ \hline 1 & 6 & M & YES & HISPANIC & 43 \\ 2 & 7 & M & NO & BLACK & 88 \\ 3 & 5 & F & YES & HISPANIC & 34 \\ 4 & 3 & M & YES & HISPANIC & 65 \\ 5 & 8 & M & YES & WHITE & 75 \\ 6 & 5 & M & NO & BLACK & 74 \\ 7 & 8 & F & YES & HISPANIC & 72 \\ 8 & 4 & M & YES & BLACK & 79 \\ 9 & 6 & M & NO & WHITE & 88 \\ 10 & 7 & M & YES & HISPANIC & 87 \\ 11 & 3 & M & NO & WHITE & 79 \\ 12 & 6 & F & NO & WHITE & 84 \\ 13 & 8 & M & NO & WHITE & 90 \\ 14 & 5 & M & NO & WHITE & 73 \\ 15 & 8 & F & NO & WHITE & 72 \\ 16 & 6 & F & NO & BLACK & 82 \\ 17 & 4 & M & NO & WHITE & 69 \\ 18 & 3 & F & YES & HISPANIC & 17 \\ 19 & 3 & M & NO & HISPANIC & 37 \\ 20 & 5 & M & NO & WHITE & 70 \\ \hline \end{tabular} \end{table} Realize from the above LaTeX code that there is no caption specified. Normally captions of Tables in LaTeX should appear above the tables, this can be changed easily of course. To specify a short and long captions in R you issue the following R code: xtable(Firstto20thRowTable, caption=c(“This is a very long caption appearing below to the table”, “I appear in the list of tables only”)) BE AWARE: xtable() is designed for 2 dimensional tables the like of contingencies tables. To use any result table coming from R function with xtable, the table has to be coerced into a matrix with the t function , in this way it becomes a 2 dimensional object and it then gets coerced to a dataframe with one rowname, “1” which gets printed by default. Let us say that we want to generate the summary statistics table of the “cars” builtin data-set (a data-set about Speed and Stopping Distances of Cars), in particular summary of the speed column. SummaryTable <- summary(carsspeed)

The above R statement, generate a summary statistics table for the speed column  in the cars data-set and store it in SummaryTable variable.

If we apply the following, we will get an error:

xtable(SummaryTable, caption = “Speed distributions of cars”)

Error in xtable.table(SummaryTable, caption = “Speed distributions of cars”) :
xtable.table is not implemented for tables of > 2 dimensions

We can generate a LaTeX version of this R table by using the print.xtable function or shorthand if you want print(…) as follows:

print.xtable(xtable(t(SummaryTable), caption = “Speed distributions of cars”), include.rownames=FALSE)

You will obtain the following code

% latex table generated in R 3.2.5 by xtable 1.8-2 package
% Thu May 18 16:43:59 2017
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrr}
\hline
Min. & 1st Qu. & Median & Mean & 3rd Qu. & Max. \\
\hline
4.00 & 12.00 & 15.00 & 15.40 & 19.00 & 25.00 \\
\hline
\end{tabular}
\caption{Speed distributions of cars}
\end{table}

The above is really the basic behavior that you can achieve in this function and you probably you want more advanced behavior, please check  R xtable package reference manual.

The function print(…) belongs to the xtable package [also referred to as print.xtable(…)] is an important function that prints exported tables whether to screen or it can save them to disk. It can generate HTML and LaTeX files alike. The only condition is that this function needs to be always associated with an xtable() function object.

How do you make the captions appear above generated tables? Well! the print function is your solution.

print( xtable(Firstto20thRowTable, caption=c(“This is a very long caption appearing below to the table”, “I appear in the list of tables only”)), caption.placement = getOption(“xtable.caption.placement”,”top”))

This generates the following LaTeX code:

% latex table generated in R 3.2.5 by xtable 1.8-2 package
% Thu May 18 16:43:59 2017
\begin{table}[ht]
\centering
\caption[I appear in the list of tables only]{This is a very long caption appearing below to the table}
\begin{tabular}{rrlllr}
\hline
\hline
1 & 6 & M & YES & HISPANIC & 43 \\
2 & 7 & M & NO & BLACK & 88 \\
3 & 5 & F & YES & HISPANIC & 34 \\
4 & 3 & M & YES & HISPANIC & 65 \\
5 & 8 & M & YES & WHITE & 75 \\
6 & 5 & M & NO & BLACK & 74 \\
7 & 8 & F & YES & HISPANIC & 72 \\
8 & 4 & M & YES & BLACK & 79 \\
9 & 6 & M & NO & WHITE & 88 \\
10 & 7 & M & YES & HISPANIC & 87 \\
11 & 3 & M & NO & WHITE & 79 \\
12 & 6 & F & NO & WHITE & 84 \\
13 & 8 & M & NO & WHITE & 90 \\
14 & 5 & M & NO & WHITE & 73 \\
15 & 8 & F & NO & WHITE & 72 \\
16 & 6 & F & NO & BLACK & 82 \\
17 & 4 & M & NO & WHITE & 69 \\
18 & 3 & F & YES & HISPANIC & 17 \\
19 & 3 & M & NO & HISPANIC & 37 \\
20 & 5 & M & NO & WHITE & 70 \\
\hline
\end{tabular}
\end{table}

The captions are above the table now. The print function is an amazing function which has many useful arguments. The following are the ones that use often:

• type = getOption (“xtable.type”, “latex”) OR just shorthand type=”latex” or type=”html”. This is a very important parameter that let you print the table either as HTML code or LaTeX code. The default type is LaTeX.
• file = getOption(“xtable.file”,””)  Specify an output file (normally saved in the default working directory. Example: print( xtable(Firstto20thRowTable, caption=c(“This is a very long caption appearing below to the table”, “I appear in the list of tables only”)), caption.placement = getOption(“xtable.caption.placement”,”top”), file = getOption(“xtable.file”,”myTable.tex”) ) outputs the table to myTable.tex. Of course you can use then \input or \include in your LaTeX document.
• append = getOption(“xtable.append”, FALSE)  or shorthand append=TRUE|FALSE  goes hand in hand with the previous argument and tells R whether to append to the output file or overwrite it.
• floating = getOption(“xtable.floating”, FALSE|TRUE) specify whether the LaTeX table is floating
• table.placement = getOption(“xtable.table.placement”,”ht”) specify the placement of table (floating argument should be TRUE and the type is LaTeX). You are probably familiar with the placement arguments { ‘h’, ‘t’, ‘b’, ‘p’ ,’ !’, ‘H’}. By default it is [ht].
• caption.placement = getOption(“xtable.caption.placement”,”top”). Default value here is “bottom”.
• … There are many others

R Tables into HTML

To demonstrate the concept I will use the tli dataset again which is part of the xtable R package.

Firstto20thRowTable <- tli[1:20, ]

To transform any xtable() result to HTML, it should be given to the function print as follows:

print (xtable(Firstto20thRowTable), type =”html”)

Obviously the HTML table is without style.  The following HTML code will be obtained:

<!– html table generated in R 3.4.0 by xtable 1.8-2 package –>
<!– Thu Jun 08 12:22:01 2017 –>
<table border=1>
<tr> <th> </th> <th> grade </th> <th> sex </th> <th> disadvg </th> <th> ethnicty </th> <th> tlimth </th> </tr>
<tr> <td align=”right”> 1 </td> <td align=”right”> 6 </td> <td> M </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 43 </td> </tr>
<tr> <td align=”right”> 2 </td> <td align=”right”> 7 </td> <td> M </td> <td> NO </td> <td> BLACK </td> <td align=”right”> 88 </td> </tr>
<tr> <td align=”right”> 3 </td> <td align=”right”> 5 </td> <td> F </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 34 </td> </tr>
<tr> <td align=”right”> 4 </td> <td align=”right”> 3 </td> <td> M </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 65 </td> </tr>
<tr> <td align=”right”> 5 </td> <td align=”right”> 8 </td> <td> M </td> <td> YES </td> <td> WHITE </td> <td align=”right”> 75 </td> </tr>
<tr> <td align=”right”> 6 </td> <td align=”right”> 5 </td> <td> M </td> <td> NO </td> <td> BLACK </td> <td align=”right”> 74 </td> </tr>
<tr> <td align=”right”> 7 </td> <td align=”right”> 8 </td> <td> F </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 72 </td> </tr>
<tr> <td align=”right”> 8 </td> <td align=”right”> 4 </td> <td> M </td> <td> YES </td> <td> BLACK </td> <td align=”right”> 79 </td> </tr>
<tr> <td align=”right”> 9 </td> <td align=”right”> 6 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 88 </td> </tr>
<tr> <td align=”right”> 10 </td> <td align=”right”> 7 </td> <td> M </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 87 </td> </tr>
<tr> <td align=”right”> 11 </td> <td align=”right”> 3 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 79 </td> </tr>
<tr> <td align=”right”> 12 </td> <td align=”right”> 6 </td> <td> F </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 84 </td> </tr>
<tr> <td align=”right”> 13 </td> <td align=”right”> 8 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 90 </td> </tr>
<tr> <td align=”right”> 14 </td> <td align=”right”> 5 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 73 </td> </tr>
<tr> <td align=”right”> 15 </td> <td align=”right”> 8 </td> <td> F </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 72 </td> </tr>
<tr> <td align=”right”> 16 </td> <td align=”right”> 6 </td> <td> F </td> <td> NO </td> <td> BLACK </td> <td align=”right”> 82 </td> </tr>
<tr> <td align=”right”> 17 </td> <td align=”right”> 4 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 69 </td> </tr>
<tr> <td align=”right”> 18 </td> <td align=”right”> 3 </td> <td> F </td> <td> YES </td> <td> HISPANIC </td> <td align=”right”> 17 </td> </tr>
<tr> <td align=”right”> 19 </td> <td align=”right”> 3 </td> <td> M </td> <td> NO </td> <td> HISPANIC </td> <td align=”right”> 37 </td> </tr>
<tr> <td align=”right”> 20 </td> <td align=”right”> 5 </td> <td> M </td> <td> NO </td> <td> WHITE </td> <td align=”right”> 70 </td> </tr>
</table>

You can have more control over the generated HTML tables through some options given to the print() function. Per example, to remove the ugly border of 1 px from the generated HTML you can use the following:

print (xtable(Firstto20thRowTable), type =”html”, html.table.attributes=”border=0″)

There is even something more amazing that you can do to style better the generated table. You can add a CSS class to the generated table (which of course you have defined it as either a custom CSS class or a Bootsrap class etc…). The way you do this is by the following:

print (xtable(Firstto20thRowTable), type =”html”, html.table.attributes=”class=’table-bordered'”)