# R, Sweave, Hmisc: pretty-print numbers

I'm creating a LaTeX tables based on a matrix in R using the 'latex' function of the Hmisc package.

dat = matrix(c(1000, 100, 10000, 10000), 2)
latex(dat, file='')


This works (as expected) perfectly.

Additionally I would like to use

prettyNum(dat, '.')


to format the numbers in a more readable way. For example:

 100 -->   100
1000 --> 1.000


Is there a simple way to combine these functions without breaking the (automatic!) table alignment ('r' for numbers instead of 'l' for character)?

-
knitr has more flexibility than Sweave. Have you looked to see if it supports something like this? –  scottkosty Mar 4 '12 at 5:44
I stumbled over knitr some weeks ago, but build my current documents mainly with Sweave. Perhaps I'll try to switch in future projects. +1 for this tip :) –  FloE Mar 4 '12 at 15:56
@scottkosty knitr looks very interesting, although I don't think it solves this problem directly; the problem here lies with Hmisc's (unavoidable) dependence on particular LaTeX packages. But I may be missing something. If you're familiar with knitr maybe you could work up a solution using it? –  Alan Munn Mar 4 '12 at 17:37
@AlanMunn I'm just starting to dig below the surface of knitr so I won't be able to make an attempt. My guess is that you're right. –  scottkosty Mar 5 '12 at 3:42

UPDATE

The Hmisc package has been updated, and now allows for arbitrary column specifications. Please see Boris' answer for a simple solution.

One of the problems with the way most R packages generate tables is that they are not easy to adapt to changing functionality within LaTeX. The standard for pretty printing numbers and tables within LaTeX is the siunitx package, which Hmisc doesn't support. There is no simple way around this if you are generating the tables using Sweave unless you post-edit your resultant .tex file.

However, if you are willing to do that, it's not that difficult to generate tables with Hmisc and then replace its r columns with the S column type defined by siunitx.

Here's a example:

.Rnw file

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{siunitx,booktabs}
\sisetup{group-separator={.},group-minimum-digits={3},output-decimal-marker={,}}
\usepackage[noae]{Sweave}
\begin{document}

<<>>=
library("Hmisc")
dat <-  matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)
@
<<echo=false,results=tex>>=
latex(dat,table=F,center='centering',file='',
@

\end{document}


Output .tex file

When you Sweave this file, you produce the .tex file which contains the following line:

\begin{tabular}{rrr}


If you manually change this to:

\begin{tabular}{SSS}


the final .tex file looks like this:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{siunitx,booktabs}
\sisetup{group-separator={.},group-minimum-digits={3},output-decimal-marker={,}}
\usepackage[noae]{Sweave}
\begin{document}

\begin{Schunk}
\begin{Sinput}
> library("Hmisc")
> dat <-  matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)
\end{Sinput}
\end{Schunk}
% latex.default(dat, table = F, center = "centering", file = "",      booktabs = T, numeric.dollar = F, colheads = c("Col A", "Col B",          "Col C"), colnamesTexCmd = "bfseries")
%
\centering
\begin{tabular}{SSS}
\toprule
\multicolumn{1}{c}{\bfseries Col A}&\multicolumn{1}{c}{\bfseries Col B}&\multicolumn{1}{c}{\bfseries Col C}\tabularnewline
\midrule
1000&10000&   3.145\tabularnewline
100&10000&1700.420\tabularnewline
\bottomrule
\end{tabular}
\end{document}


Now siunitx can do its magic, and the output is the following:

-
It is much simpler with the new version of Hmisc - see my answer... –  Boris Apr 16 '12 at 18:01
@Boris. That's great. It makes Hmisc even more useful. Thanks for posting your answer. –  Alan Munn Apr 16 '12 at 18:25

Actually the new version of Hmisc has the parameter col.just, so you do not need to edit the tex file

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{siunitx,booktabs}
\sisetup{group-separator={.},group-minimum-digits={3},
output-decimal-marker={,}}
\usepackage[noae]{Sweave}
\begin{document}

<<>>=
library("Hmisc")
dat <-  matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)
@
<<echo=false,results=tex>>=
latex(dat,table=F,center='centering',file='',