R Programming/Publication Quality Ouput

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Sweave

help("Sweave", package="utils") 
  • Documentation starts with a line starting with @
  • Code starts with <<>>=
    • fig=T : if you want to include the figure in the LaTeX document
    • By default, figures are exported as pdf and eps files. If you only want one format suppress the other one with pdf=F or eps=F option
    • echo=F suppress the R code
    • results=tex treats the output as LaTeX code
    • results=verbatim treats the output as Verbatim (the default)
    • results=hide does not include the results in the LaTeX output
    • eval=F does not include the code in the code chunk
  • Compile using the following command :
R> Sweave("macro4.Rnw")
Writing to file macro4.tex
Processing code chunks ...
 1 : echo keep.source term verbatim pdf
 2 : echo keep.source term verbatim pdf

Vous pouvez maintenant lancer LaTeX sur 'macro4.tex'
  • If there is no error, you can run the tex file.
    • Note that you may download Sweave.sty since it is not part of the standard MikTeX distribution
  • You can also add your results in your text using the Sexpr() function.
  • Stangle() extract the R code from a .Rnw file.
R> Stangle("macro4.Rnw")
Writing to file macro4.R 

[edit] Text editor

  • Vim provides syntax highlighting for Sweave file (R no web syntax)

[edit] Examples of Sweave Documents

[edit] Export to LaTeX

  • See the LaTeX Wikibook if you want to learn about LaTeX


Index of introduced function

The goal of this page is to compare those functions.

  • toLatex() in the utils package
    • does not handle matrix.
> toLatex(sessionInfo())
\begin{itemize}
  \item R version 2.2.0, 2005-10-06, \verb|powerpc-apple-darwin7.9.0|
  \item Base packages: base, datasets, grDevices,
    graphics, methods, stats, utils
\end{itemize}
  • toLatex() have been adapted to matrix and ftables in the memisc package.
>N <- 10
>u <- rnorm(N)
>x1 <- rnorm(N)
>x2 <- rnorm(N)
>y <- 1+ x1 + x2 + u
>fit<-lm(y~x1+x2)
>summary(fit)
>source("http://pj.freefaculty.org/R/outreg-worked.R")
> outreg(fit,title="My One Tightly Printed Regression", lyx=F )
\begin{table}
 \caption{ My One Tightly Printed Regression }\label{  }
 \begin{center}
 \begin{tabular}{*{2}{l}}
 \hline
               & Estimate  \\
              & (S.E.)  \\
\hline 
 \hline
  (Intercept)   &    0.607* \\
    &   ( 0.255) \\
  x1   &    2.008* \\
    &   ( 0.278) \\
  x2   &    1.772* \\
    &   ( 0.325) \\
 \hline 
N   &    10 \\
 $RMSE$       & 0.662  \\
 $R^2$       & 0.912  \\
 $AIC$      & 24.564  \\
 \hline\hline
* $p \le 0.05$\end{tabular}
\end{center}
\end{table}

One can also use the xtable() function in the xtable library

> library(xtable)
> xtable(fit)
% latex table generated in R 2.2.0 by xtable 1.3-1 package
% Sun Jun 21 09:11:30 2009
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 0.6074 & 0.2546 & 2.39 & 0.0485 \\
x1 & 2.0077 & 0.2779 & 7.22 & 0.0002 \\
x2 & 1.7723 & 0.3249 & 5.46 & 0.0010 \\
\hline
\end{tabular}
\end{center}
\end{table}

xtable() can also be used to export the results to an HTML file :

> tab <- xtable(fit)
> print(tab,type="html")

[edit] Export to HTML

[edit] References

  1. Ista ZahnLearning To Sweave in APA Style, The PracTeX Journal 2008, 1
Previous: Natural Language Processing Index