[R] Fwd: Converting R to Sweave (Rnw)

Neil Shephard nshephard at gmail.com
Mon Mar 2 15:52:26 CET 2009




Rainer M Krug-6 wrote:
> 
> Hi
> 
> I am thinking about using Sweave more frequently, especially for
> documenting code. But the syntax is slightly awkward for me (<<name>>=
> ... @), and I was thinking if there would be a way of importing the
> type of code extracted from an Rnw file back into an Rnw file? The
> advantage would be that the code could run in R without tangling.
> Obviously, sweave options could not be imported, but that would be
> fine for me. Below an example of the code generated by Rtangle, which
> I would like to import into a sweave file.
> 
> Cheers
> 
> Rainer
> 
> ###################################################
> ### chunk number 1: a
> ###################################################
> x <- 10
> 
> 
> 
> ###################################################
> ### chunk number 2:
> ###################################################
> asequence<- seq(from=0,to=5,by=0.1)
> expnegx2 <- exp(-asequence^2)
> 
> plot(asequence,expnegx2,type="l",ylab=expression(exp(-z^2)),xlab="z")
> 
> 
> ###################################################
> ### chunk number 3: Normal1
> ###################################################
> mu <- 3
> sigma <- 5
> 

This seems like trying to put the cart before the horse to my mind.

A .Rnw is a hybrid of LaTeX and R code the later is delineated from the
former by being encapsulated by the (<<name>>= ... @) tags which also define
whether the results and/or images should be included in the LaTeX output.

If you always want to use Sweave to document your code thats you're
prerogative, but its really designed for writing a report with the R-code
embedded, some of that code (reading in files etc.) is not relevant to the
code so is suppressed, whilst others the output of the commands is required
(and you duly write the tags around the R code to show the relevant output).

If all you want to do is comment you're code, then I see nothing wrong or
hard about using the '#' delimiter which comments out all text that follows
its insertion in your .R file.  Personally when I write Sweave documents I
include comments in the R section of the files using this delimiter.

Neil

-- 
View this message in context: http://www.nabble.com/Fwd%3A-Converting-R-to-Sweave-%28Rnw%29-tp22288734p22289546.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list