Skip to content

Converting R to Sweave (Rnw)

4 messages · Rainer M Krug, Neil Shephard, Duncan Murdoch

#
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

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Faculty of Science
Natural Sciences Building
Private Bag X1
University of Stellenbosch
Matieland 7602
South Africa
#
Rainer M Krug-6 wrote:
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
#
On 3/2/2009 9:06 AM, Rainer M Krug wrote:
I don't think so, but writing a new driver is only a medium difficulty 
job.  Start with an existing one in

https://svn.r-project.org/R/trunk/src/library/utils/R/Sweave.R

and modify until you have what you want. The harder part of this is the 
design:  exactly what input and output is not going to be awkward?

A different approach to the same problem is to use specially formatted 
comments in the R source to generate documentation; I think the R.oo 
package includes such a thing, and there may be others.  I haven't used 
these with R, but have in other languages, and they were nice there.

Duncan Murdoch
#
On 3/2/2009 10:12 AM, Duncan Murdoch wrote:
Actually, the package I was thinking of was roxygen (on CRAN), but I 
couldn't remember the name and a search came up with the R.oo reference. 
  So there appear to be at least two such systems.

Duncan Murdoch