You are here:
Vanderbilt Biostatistics Wiki
>
Main Web
>
TWikiUsers
>
TheresaScott
>
EPSLatexTables
(31 Mar 2005,
TheresaScott
)
(raw view)
E
dit
A
ttach
---+Useful Stuff in R: ---++ Inserting Latex Tables into Microsoft Word ---++++ Theresa A Scott, M.S. <br> Biostatistician II, Department of Biostatistics <br> 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; see bottom of page) contains the sample data file I used. And the attached R code file (.R; see bottom of page) 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. <verbatim> 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") </verbatim> __NOTE__: When the Latex table is viewed using a viewer such as _dvi_ (see the _dvi_ function help page), 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). <verbatim> 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") </verbatim> __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: <verbatim> ps2epsi summ.ps summ.eps </verbatim> __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).
Attachments
2
Attachments
2
Topic attachments
I
Attachment
Action
Size
Date
Who
Comment
R
epstables.R
manage
1.9 K
05 Nov 2004 - 11:15
TheresaScott
R code of step-by-step process
txt
sampledata.txt
manage
0.8 K
05 Nov 2004 - 11:14
TheresaScott
Sample data file
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Edit
w
iki text
|
M
ore topic actions
Topic revision: r4 - 31 Mar 2005,
TheresaScott
Main
Department Home Page
Biostatistics Graduate Program
Vanderbilt University Medical Center
Main Web
Main Web Home
Search
Recent Changes
Changes
Topic list
Biostatistics Webs
Archive
Main
Sandbox
System
Register
|
Log In
Copyright © 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