[R] Sweave-LaTEX question

cls59 sharpsteen at mac.com
Sun Oct 12 05:42:40 CEST 2008



Felipe Carrillo wrote:
> 
> 
> Does LATEX have to be installed on your computer? 
> 
> 

Yes, the LaTeX package contains compilers necessary for transforming LaTeX
mark-up and macros into beautifully typeset documents. Making use of Sweave
requires a LaTeX distribution to be present on your machine. A good,
complete compilation for Windows/Unix is TexLive, obtainable from:

http://www.tug.org/texlive/acquire.html

If you use a Macintosh, I suggest MacTex which is quite similar:

http://www.tug.org/mactex/

Both of these downloads are pretty burly (~1.15 GB), the package is VERY
complete (several flavors of TeX, including LaTeX along with a heap of
add-on packages). 

If you use windows, a good light-weight solution is MiKTeX available from:

http://miktex.org/2.7/Setup.aspx

The nice thing about MiKTeX, is that it includes a package manager- so
instead of downloading 1.15 GB of packages you probably won't use you can
pick and choose the ones you want buffet-style.

A good website explaining basic LaTeX usage is located at:

http://andy-roberts.net/misc/latex/

The final piece is a good TeX-aware editor, for windows I prefer WinEdt:

http://www.winedt.com/

Linux users can take advantage of Kile:

http://kile.sourceforge.net/

And for Mac users I suggest TeX Shop which is included with MacTex:

http://www.uoregon.edu/~koch/texshop/


Felipe Carrillo wrote:
> 
> 
> How does the xtable package and Sweave work together? 
> 
> 

Sweave works beautifully for documents with embedded code which is not
terribly computationally intensive. To clarify: When you finish a LaTeX
document, there is a phase where you will make a small tweak, run the
typesetting program to see the effect, make another tweak, rerun the
typesetter... repeat, repeat, repeat. This usually takes no more that a
second or two even for large documents.

With Sweave, the run time of the embedded code is added on to the time it
takes to typeset the document. For most reports where some data is shuffled,
analyzed and plotted, this happens in the blink of an eye and is not
noticeable. However, if you are running something like an optimization
routine which takes five minutes, you will be waiting five minutes every
time you typeset the document. Which means you will be waiting five minutes
every time you want to see the effect of some minor tweak to a document.

There are of course ways around this problem, but it existence does decrease
the efficiency of Sweave for reporting the results of computationally
intense operations.



Felipe Carrillo wrote:
> 
> 
> How can I make the code below create a table as pdf file? 
> 
> \begin{table}[ht]
> \begin{center}
> \begin{tabular}{rrrrr}
>   \hline
>  & Estimate & Std. Error & z value & Pr($>$$|$z$|$) \\
>   \hline
> (Intercept) & 2.6470 & 0.0698 & 37.94 & 0.0000 \\
>   male & $-$0.4094 & 0.0482 & $-$8.49 & 0.0000 \\
>   langarts & $-$0.0147 & 0.0013 & $-$11.34 & 0.0000 \\
>    \hline
> \end{tabular}
> \end{center}
> \end{table}
> 

What you have here is a complete code fragment for a table, but it is a few
lines short of a full LaTeX document. At the very least you need:

% Percent signs comment LaTeX code.
% Choose your document class first,
% some publications provide their own
% document classes.
\documentclass[11pt]{article}

% Typesetting instructions and
% package inclusions usually
% go here

% Begin the content of the document
\begin{document}

\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
  \hline
 & Estimate & Std. Error & z value & Pr($>$$|$z$|$) \\
  \hline
(Intercept) & 2.6470 & 0.0698 & 37.94 & 0.0000 \\
  male & $-$0.4094 & 0.0482 & $-$8.49 & 0.0000 \\
  langarts & $-$0.0147 & 0.0013 & $-$11.34 & 0.0000 \\
   \hline
\end{tabular}
\end{center}
\end{table}

% End the document
\end{document}

Save the above as a file like report.tex, then run pdflatex on report.tex .
This will produce a pdf for you. Now, suppose the data for table was stored
in an R data frame named dataFrame. An example of using Sweave, xtable and
LaTeX would look something like:

\documentclass[11pt]{article}

\begin{document}

% Sweave code chunks are placed
% between <<>>= and @

% echo=F suppresses output of R
% commands, setting it to T can
% help when debugging a Sweave
% document.

<<dataAnalysis.R,echo=F>>=

# Here we run the commands
# required to create the variable
# dataFrame

@

% Now for the table
\begin{table}[ht]
\centering

% Results=tex indicates this code chunk
% is expected to create TeX code.
<<dataAnalysis.R,echo=F,results=tex>>=
# Load xtable
require(xtable)

#Create an xtable object from your variable
dataTable <- xtable(dataFrame)

#Add complicated row and column names
#Some objects such as ANOVA tables
#will have these set up by default
colnames(dataTable) <- c('Estimate',
'Std. Error','z value','Pr($<|z|$)')

rownames(dataTable) <- c('(Intercept)',
'male','langarts')

#Floating=T causes xtable
#to include it's own \begin
#and \end{table}, I find this
#annoying to work with.
print(dataTable,floating=F)

@

\end{table}

\end{document}

Save the above as a file like report.Rnw , then from inside R use
Sweave('path/to/report.Rnw') to execute the R code chunks and create
report.tex. Run pdflatex on report.tex to create your final product.

A good example of Sweave usage and table generation is provided by the
xtable demo document, you can find a pdf online:

http://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf

The above pdf was created using LaTeX and Sweave!

Hope this helps!

Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/Sweave-LaTEX-question-tp19937508p19938510.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list