Useful Stuff in R:

Inserting Latex Tables into Microsoft Word

Theresa A Scott, M.S.; Biostatistician II
Department of Biostatistics, Vanderbilt University School of Medicine

The tables generated by Latex are quite nice, because of their formatting and ease of creation. The following demonstrates how to generate an encapsulated postscript file containing the created Latex table, which can then be inserted into Microsoft Word.

The attached text file (.txt) contains the sample data file I used. And the attached R code file (.R) contains the R code, which you can copy, paste, and modify for your own use.

Process:
First we need to generate the table in R. This can be done using the summary and latex functions. library(Hmisc) x<-read.table("sampledata.txt", header=TRUE) x<-upData(x, labels=c(case="ID", age="Age", sex="Sex", height.in="Height", weight.lbs="Weight", outcome1="Outcome 1", outcome2="Outcome 2"), units=c(height.in="inches", weight.lbs="lbs.")) summ<-summary(~., data=x[-1]) summ.latex<-latex(summ, middle.bold=T, digits=3, caption="Descriptive Statistics of Sample Data")

NOTE: When the Latex table is viewed using a viewer such as dvi (see the dvi function), we notice that the table is cropped.

Next, we use the dvips function to create a postscript file of the Latex table.

NOTE: The current version of dvips places a page number at the bottom of the table. The following code redefines the dvi.latex function, which is called by the dvips function, such that no page number is shown (i.e. adds \pagestyle{empty} to the Latex document). dvi.latex <- function(object, prlog=FALSE, nomargins=TRUE, width=5.5, height=7, ...) { fi <- object$file; sty <- object$style if(length(sty))sty <- paste('\\usepackage{',sty,'}',sep='') if(nomargins) sty <- c(sty, paste('\\usepackage[paperwidth=',width, 'in,paperheight=', height, 'in,noheadfoot,margin=0in]{geometry}',sep='')) ## pre <- tempfile(); post <- tempfile() # 1dec03 tmp <- tempfile() tmptex <- paste(tmp, 'tex', sep='.') infi <- readLines(fi) cat('\\documentclass{report}', sty, '\\begin{document}\\pagestyle{empty}', infi, '\\end{document}\n', file=tmptex, sep='\n') sc <- if(under.unix)';' else '&' # DOS command separator sys(paste('cd',dQuote(tempdir()),sc,optionsCmds('latex'), '-interaction=scrollmode', dQuote(tmp)), output=FALSE) if(prlog) cat(scan(paste(tmp,'log',sep='.'),list(''),sep='\n')1, sep='\n') fi <- paste(tmp,'dvi',sep='.') structure(list(file=fi), class='dvi') } dvips(summ.latex, file="summ.ps")

NOTE: When the created postscript file (summ.ps) is opened, we notice that the table is placed in the upper left hand corner of the page, with no margins. Therefore, the table is no longer cropped.

Next, we use the ps2epsi program from the Unix command line (quit R first). ps2epsi generates an encapsulated postscript file (.eps) from the post-script file generated by dvips within R.

So, at the Unix command line type: ps2epsi summ.ps summ.eps

NOTE: When the created encapsulated postscript file (summ.eps) is opened, we notice that the table is no longer shown on a full page, but it now cropped.

We can now insert the encapsulated postscript version of our table into a Microsoft Word document by choosing Insert > Picture > From File from the menu bar, and choosing our desired file (summ.eps).

NOTE: When the encapsulated postscript file is inserted it may look a bit fuzzy or unfocused. However, when the table is printed it is as precise as if you created the table in Microsoft Word.

NOTE: If you or your collaborator would like to Edit the table in any way (i.e. modify the variables names or title), you need to do this either through the latex function, or in the Latex file generated by the latex function (i.e. summ.tex).
Topic attachments
I Attachment Action Size Date Who Comment
epstables.RR epstables.R manage 1.9 K 05 Nov 2004 - 11:15 TheresaScott R code of step-by-step process
epstables.pdfpdf epstables.pdf manage 64.2 K 05 Nov 2004 - 11:13 TheresaScott Step-by-step Process
sampledata.txttxt sampledata.txt manage 0.8 K 05 Nov 2004 - 11:14 TheresaScott Sample data file
Edit | Attach | Print version | History: r4 < r3 < r2 < r1 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r2 - 05 Nov 2004, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback