[R] including Sweave tangled code in '.Rnw' document
    Berwin A Turlach 
    berwin at maths.uwa.edu.au
       
    Sun Nov 30 10:24:36 CET 2008
    
    
  
G'day Kyle,
On Sat, 29 Nov 2008 11:11:37 -0800
"Kyle Matoba" <kmmatoba at ucdavis.edu> wrote:
> I have been using Sweave for my homework this last quarter [...] but
> would like to know if there is a clean way to do so using LaTeX or
> Sweave so that the latest version of the code is included in my
> homework each time I run 'R CMD Sweave HW4.Rnw; pdflatex HW4.tex'.
> 
> e.g I envision something such as:
A file HW4.Rnw such as the following seem to do what you want:
++++++++++++++ start HW4.Rnw ++++++++++++++++++
\documentclass[a4paper,11pt]{article}
\usepackage{listings}
\begin{document}
<<fig3, echo=FALSE, results=hide, fig=TRUE, eps=FALSE>>=
plot(runif(1000))
@
<<>>=
mean(runif(1000))  
@   
TA here is my code:
% some manner of Sweave/LaTeX macro to include the R code tangled out
% of this '.Rnw' document in this case it would just be
% 'plot(runif(1000))', perhaps manually enclosed in a verbatim
% environment 
<<echo=FALSE, results=hide,split=TRUE>>=
Stangle(file="HW4.Rnw", output="XYZ.R", annotate=FALSE)
@ 
\lstinputlisting[emptylines=0]{XYZ.R}
\end{document}
++++++++++++++ end HW4.Rnw ++++++++++++++++++++
If the last Stangle command should be included in the listing, remove
the "split=TRUE" option.  And the "file=" argument to Stangle would
have to be correctly set for each .Rnw file; did not find a way to
automate this.  
> I am running R-2.6.2 on Ubuntu Hardy Heron.
R 2.6.2 is quite old by now, the current version is R 2.8.0.  When you
upgrade, be aware of:
    o	The default for 'stylepath' in Sweave's (default) RweaveLatex
	driver is now FALSE rather than TRUE if
	SWEAVE_STYLEPATH_DEFAULT is unset : see ?RweaveLatex.
	To support this, tools::texi2dvi adds the R 'texmf' directory
	to the input search path.
which could mean that the way you process .Rnw file might no longer
work. 
HTH.
Cheers,
	Berwin
=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba
    
    
More information about the R-help
mailing list