[R] Export to LaTeX

Dieter Menne dieter.menne at menne-biomed.de
Fri Jul 14 11:06:19 CEST 2006


Mag. Ferri Leberl <ferri.leberl <at> gmx.at> writes:

> I want to export data to LaTeX. As I want to employ the data as freely as 
> possible I want to avoid the xtable-command and instead generate some List 
> like
> 
> \MyOwnPrettyCommand{Adam}{Auer}{17}
> \MyOwnPrettyCommand{Bertram}{Bauer}{14}
> \MyOwnPrettyCommand{Christoph}{Huber}{75}
> 
> with \MyOwnPrittyCommand defined elsewhere.


Method 1: Use latex in package Hmisc. It is VERY flexible, and works for me in
most cases.

Hint 1: "\" rarely works in R as in C, it must be escaped to "\\"

Hint 2: To increase you changes of getting a reply, always provide a full
example generating the sample data. For example, in most cases "Adam" will be
stored as a factor in the dataframe, which make things a bit more tricky. I used
I() to avoid this in the example below

d = data.frame(name=I(c("Auer","Caesar")),
               vname=I(c("Dieter","Karl")),age=c(10,30))
for (i in 1:nrow(d)) {
  d0 = d[i,]
  cat("\\MyNiceCommand{",d0$name,"}{",d0$vname,"}{",d0$age,"}\n",sep="")
}

You may nest this with sink, or better use the file argument in cat, don't
forget append.
Or directly use Rweave, which should not be too difficult for a LaTeXer, and
does not require the sink stuff.

Dieter



More information about the R-help mailing list