#LyX 1.6.3 created this file. For more info see http://www.lyx.org/ \lyxformat 345 \begin_document \begin_header \textclass literate-article \begin_preamble \usepackage{ae} \renewcommand{\rmdefault}{ppl} \renewcommand{\sfdefault}{aess} \renewcommand{\ttdefault}{aett} \end_preamble \use_default_options true \language english \inputencoding auto \font_roman default \font_sans default \font_typewriter default \font_default_family default \font_sc false \font_osf false \font_sf_scale 100 \font_tt_scale 100 \graphics default \paperfontsize default \spacing single \use_hyperref true \pdf_title "Extending Sweave Capabilities" \pdf_author "Yihui Xie" \pdf_subject "Sweave" \pdf_keywords "R" \pdf_bookmarks true \pdf_bookmarksnumbered true \pdf_bookmarksopen false \pdf_bookmarksopenlevel 1 \pdf_breaklinks false \pdf_pdfborder false \pdf_colorlinks true \pdf_backref false \pdf_pdfusetitle true \papersize default \use_geometry true \use_amsmath 1 \use_esint 1 \cite_engine basic \use_bibtopic false \paperorientation portrait \leftmargin 2.5cm \topmargin 2.5cm \rightmargin 2.5cm \bottommargin 2.5cm \secnumdepth 3 \tocdepth 3 \paragraph_separation indent \defskip medskip \quotes_language english \papercolumns 1 \papersides 1 \paperpagestyle default \tracking_changes false \output_changes false \author "" \author "" \end_header \begin_body \begin_layout Title Extending Sweave Capabilities \end_layout \begin_layout Author Yihui Xie \begin_inset Foot status open \begin_layout Plain Layout Email: \begin_inset CommandInset href LatexCommand href name "xie@yihui.name" target "xie@yihui.name" type "mailto:" \end_inset Homepage: \begin_inset Flex URL status collapsed \begin_layout Plain Layout http://yihui.name \end_layout \end_inset \end_layout \end_inset \end_layout \begin_layout Scrap <>= \begin_inset Newline newline \end_inset options(prompt = "R> ") \begin_inset Newline newline \end_inset source("Sweave2.r") \begin_inset Newline newline \end_inset @ \end_layout \begin_layout Standard \begin_inset ERT status open \begin_layout Plain Layout \backslash SweaveOpts{prefix.string=fig,eps=FALSE,pdf=FALSE,png=TRUE} \end_layout \end_inset \end_layout \begin_layout Section Overview \end_layout \begin_layout Standard Sweave is an excellent utility to write reproducible documents by mixing up R and \begin_inset ERT status open \begin_layout Plain Layout \backslash LaTeX \backslash \end_layout \end_inset together. In my daily work, I find some limitations could actually be removed if Sweave offers more flexible options, e.g. using customized parse and deparse functions or graphics devices. Anyway, I'm maintaining a collection of R code to make Sweave more flexible. \end_layout \begin_layout Standard To use the code, simply put the file \family typewriter Sweave2.r \family default in the same directory with your Sweave document and \family typewriter source("Sweave2.r") \family default in your first code chunk (i.e. before any code chunk is run). \end_layout \begin_layout Section Partially Preserve Comments \end_layout \begin_layout Standard We know Sweave will discard all comments by default due to the behaviour of \family typewriter parse() \family default and \family typewriter deparse() \family default . \end_layout \begin_layout Quote Sweave FAQ A.19: Why does Sweave delete all comments from the R code? Why does it mess up line breaks for continuation lines? \end_layout \begin_layout Standard This problem can be solved if Sweave allows users to set their own \begin_inset Quotes eld \end_inset parsers \begin_inset Quotes erd \end_inset (e.g. in \family typewriter \backslash SweaveOpts{} \family default ). There is a function named \family typewriter tidy.source() \family default in the R package \family typewriter animation \family default which can partially preserve R comments while (de)parsing R code. The trick is to mask comments as if there were normal R code for assignments: \family typewriter # R comment \family default \begin_inset Formula $\Rightarrow$ \end_inset \family typewriter \bar under begin.identifier = " \bar default # R comment \bar under end.identifier" \family default \bar default , then \family typewriter parse() \family default it and remove the identifiers in the process of \family typewriter deparse() \family default ing. \end_layout \begin_layout Scrap <>= \begin_inset Newline newline \end_inset ## yeah! we like comments! \begin_inset Newline newline \end_inset rnorm(10) \begin_inset Newline newline \end_inset ## discard inline comments and preserve single lines of comments \begin_inset Newline newline \end_inset # source code is: rnorm(10) # you won't see me! \begin_inset Newline newline \end_inset # output: \begin_inset Newline newline \end_inset rnorm(10) # you won't see me! \begin_inset Newline newline \end_inset ## spaces and tabs are added where appropriate \begin_inset Newline newline \end_inset # source code: \begin_inset Newline newline \end_inset # replicate(100,{x=runif(10,1,3);y=x+rnorm(length(x));coef(lm(y~x))}) \begin_inset Newline newline \end_inset # the 'tidy' version will be like this: \begin_inset Newline newline \end_inset replicate(100,{x=runif(10,1,3);y=x+rnorm(length(x));coef(lm(y~x))}) \begin_inset Newline newline \end_inset # does this look more human-readable? \begin_inset Newline newline \end_inset @ \end_layout \begin_layout Standard We can use \family typewriter tidy.source() \family default to \begin_inset Quotes eld \end_inset tidy up \begin_inset Quotes erd \end_inset R code, but Sweave does not have options to customize the parser. So I used the \begin_inset Quotes eld \end_inset dark voodoo \begin_inset Quotes erd \end_inset suggested by Charlie Sharpsteen ( \begin_inset Flex URL status collapsed \begin_layout Plain Layout https://stat.ethz.ch/pipermail/r-help/2009-October/213906.html \end_layout \end_inset ). This might not be a good solution because it used very \begin_inset Quotes eld \end_inset brutal \begin_inset Quotes erd \end_inset method to modify \family typewriter makeRweaveLatexCodeRunner() \family default , but it just works. \end_layout \begin_layout Section PNG Graphics Device \end_layout \begin_layout Standard Using the same trick in the previous section, we can add a \family typewriter png \family default option in \family typewriter \backslash SweaveOpts{} \family default . See Figure \begin_inset CommandInset ref LatexCommand ref reference "fig:boxplot" \end_inset for example. As pdf \begin_inset ERT status open \begin_layout Plain Layout \backslash LaTeX \backslash \end_layout \end_inset searches PDF graphs before PNG (can we set the priority?), we can only specify one type of graphics device for the whole document, e.g. \family typewriter \backslash SweaveOpts{eps=FALSE,pdf=FALSE,png=TRUE} \family default . \end_layout \begin_layout Standard It should not be difficult to specify one graphics device for each code chunk. I'll consider it later. \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Scrap \align block <>= \begin_inset Newline newline \end_inset par(mar=c(4,4,.1,.1)) \begin_inset Newline newline \end_inset boxplot(count ~ spray, data = InsectSprays, col = "lightgray") \begin_inset Newline newline \end_inset @ \end_layout \begin_layout Plain Layout \begin_inset Caption \begin_layout Plain Layout A boxplot in PNG format \begin_inset CommandInset label LatexCommand label name "fig:boxplot" \end_inset \end_layout \end_inset \end_layout \begin_layout Plain Layout \end_layout \end_inset \end_layout \begin_layout Scrap \end_layout \begin_layout Section \start_of_appendix Source code: \family typewriter Sweave2.r \end_layout \begin_layout Standard \begin_inset CommandInset include LatexCommand verbatiminput filename "Sweave2.r" \end_inset \end_layout \end_body \end_document