[Rd] embed Sweave driver in .Rnw file
Romain Francois
romain at r-enthusiasts.com
Tue Dec 14 12:40:04 CET 2010
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 ?
Romain
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/fT2rZM : highlight 0.2-5
|- http://bit.ly/gpCSpH : Evolution of Rcpp code size
`- http://bit.ly/hovakS : RcppGSL initial release
More information about the R-devel
mailing list