R is a easy to learn statistical programming language. Its popularity has been increasing now-a-days. Many useful and productive packages are available in R. One of them is
xtable package.

In most of the scientific publication LaTeX is extensively in use. Similarly, HTML is used though out the web. Table Generation is a bit boring and repetitive process in both of them.
xtable package in R generate tables and the result of analysis into a beautiful tables for both LaTeX and HTML.

You can install the xtable package by using the install.packages command as,

1 |
install.packages("xtable") |

Load the package as,

1 |
library("xtable") |

Lets create some LaTeX and HTML table from R using
xtable command. For this, I am going to use the default datasets available in R. (To learn about datasets available in R, see the article “Using Data from different Packages”). Lets use carbon data from dataset package.

The top six observation in the dataset is,

1 |
head(CO2) |

The LaTeX table for these 6 observation is generated as,

1 2 |
xtabl <-xtable(head(CO2)) print(xtabl, type="latex", include.rownames=F) |

The output will be a LaTeX code as,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
% latex table generated in R 3.0.2 by xtable 1.7-1 package % Tue Jan 7 19:39:43 2014 begin{table}[ht] centering begin{tabular}{lllrr} hline Plant & Type & Treatment & conc & uptake \\ hline Qn1 & Quebec & nonchilled & 95.00 & 16.00 \\ Qn1 & Quebec & nonchilled & 175.00 & 30.40 \\ Qn1 & Quebec & nonchilled & 250.00 & 34.80 \\ Qn1 & Quebec & nonchilled & 350.00 & 37.20 \\ Qn1 & Quebec & nonchilled & 500.00 & 35.30 \\ Qn1 & Quebec & nonchilled & 675.00 & 39.20 \\ hline end{tabular} end{table} |

A HTML table can be generated as,

1 |
print(xtbl, type="html", include.rownames=F) |

The option
include.rownames=F stops the row number to display.

The output results are,

### Exercise

- Use the mtcars dataset, fit a linear model with mpg as response variable and wt, disp and hp as predictor variable. Generate a LaTeX table and HTML table for the fitted model using xtable.