From friedrich.leisch at stat.uni-muenchen.de  Tue Dec 14 13:21:55 2010
From: friedrich.leisch at stat.uni-muenchen.de (Friedrich Leisch)
Date: Tue, 14 Dec 2010 13:21:55 +0100
Subject: [Rd] embed Sweave driver in .Rnw file
In-Reply-To: <4D075794.3010201@r-enthusiasts.com>
References: <4D075794.3010201@r-enthusiasts.com>
Message-ID: <19719.24931.968293.80971@angua.stat.uni-muenchen.de>

>>>>> On Tue, 14 Dec 2010 12:40:04 +0100,
>>>>> Romain Francois (RF) wrote:

  > Hello,
  > Sweave lets you use alternative drivers through the driver argument, and 
  > several packages take advantage of that and define custom Sweave driver 
  > for various purposes. Most of them are listed on the Reproducible 
  > Research CTV: 
  > (http://cran.r-project.org/web/views/ReproducibleResearch.html)

  > The next natural step is for package developpers to take advantage of 
  > this in their vignettes. In Rcpp we work around the way package building 
  > works and we do:
  > - let R build a dummy vignette
  > - then use the inst/doc/Makefile to replace it with a vignette that is 
  > processed by the driver from the highlight package (giving syntax 
  > highlighting).

  > I played with Sweave so that it would be able to create the driver from 
  > some text included in the text of the .Rnw file:

  > $ svn diff
  > Index: src/library/utils/R/Sweave.R
  > ===================================================================
  > --- src/library/utils/R/Sweave.R	(revision 53846)
  > +++ src/library/utils/R/Sweave.R	(working copy)
  > @@ -20,6 +20,16 @@
  >   # We don't need srclines for code, but we do need it for text, and 
  > it's easiest
  >   # to just keep it for everything.

  > +SweaveGetDriver <- function(file){
  > +    txt <- readLines(file)
  > +    line <- grep( "\\SweaveDriver", txt, value = TRUE )
  > +    if( length(line) ){
  > +        txt <- sub( "^.*\\SweaveDriver[{](.*)[}]", "\\1", line[1L] )
  > +        driver <- try( eval( parse( text = txt ) ), silent = TRUE )
  > +        if( !inherits( driver, "try-error") ) driver
  > +    }
  > +}
  > +
  >   Sweave <- function(file, driver=RweaveLatex(),
  >                      syntax=getOption("SweaveSyntax"), ...)
  >   {
  > @@ -28,7 +38,9 @@
  >       else if(is.function(driver))
  >           driver <- driver()

  > -
  > +    drv <- SweaveGetDriver(file)
  > +    if( !is.null(drv) ) driver <- drv
  > +
  >       if(is.null(syntax))
  >           syntax <- SweaveGetSyntax(file)
  >       if(is.character(syntax))



  > This allows one to write something like this in their file:

  > %\SweaveDriver{ { require(highlight); HighlightWeaveLatex() } }

  > So that when calling :

  >> Sweave( "somefile.Rnw" )

  > the highlight driver is used instead of the default driver.

  > Could something like that be added to Sweave ?

Yes, sure!

Will have a look at the patch later this week and apply it if it
passes the tests. The patch is against a current r-devel?

Best,
Fritz



[Rd] embed Sweave driver in .Rnw file
Friedrich Leisch