[Rd] embed Sweave driver in .Rnw file

Duncan Murdoch murdoch.duncan at gmail.com
Tue Dec 14 17:13:44 CET 2010


On 14/12/2010 10:52 AM, Dominick Samperi wrote:
> Both of my questions were not clear, sorry.
>
> What I really want to do is have a customized version of Sweave.sty
> (Sweave++.sty)
> included automatically from some designated area, like the include directory
> of another
> package, rather than requiring the user to keep a version in the local
> directory. Since
> the changes are minor, it might make more sense to solve this by
> incorporating
> them into Sweave.sty.

I don't know a way to refer to package directories from a Sweave 
document.   It might be possible to write an R function that writes out 
system-dependent \usepackage{} commands, and put that in a results=tex 
code chunk.  However, I'm not certain LaTeX can handle Windows paths in 
general (spaces and tildes both seem to cause problems, and I don't know 
a way to avoid both), so this might be impossible.

> On the temp directory, yes, I'm talking about Sweave's prefix option. Your
> suggestion
> to use dir.create() sounds like a reasonable work-around for the fact that
> TeX cannot perform OS-level functions.

The directory needs to be in place by the time you write out a figure or 
concordance from Sweave, so TeX can't be involved in creating it.

> Yet another small issue that I'm sure others have encountered is that
> TeX processing will fail under Linux when the source files contain DOS CR's,
> and
> the error diagnostics would leave you in the dark. The only fix that I could
> come up with is to remove the CR's manually using tr. Is there a better way?

I think it is most productive to think of the .tex output from Sweave as 
system-dependent object files, not portable source.  I'm pretty sure 
Sweave can read CR/LF files on any system and will write out local line 
endings.  So it's only a problem when you distribute the .tex output of 
Sweave, not when you distribute the .Rnw input.  (I understand there are 
circumstances where this is unavoidable, but you shouldn't be surprised 
when object files aren't portable.)

Duncan Murdoch

> Thanks,
> Dominick
>
> On Tue, Dec 14, 2010 at 10:26 AM, Duncan Murdoch
> <murdoch.duncan at gmail.com>wrote:
>
> >  On 14/12/2010 9:54 AM, Dominick Samperi wrote:
> >
> >>  Another question about Sweave (actually it is more a question
> >>  about TeX). Is there a reliable (system-independent) way to
> >>  use Sweave.sty without having to place it in the current working
> >>  directory? MiKTeX under Windows has dropped the use of
> >>  TEXINPUTS, and this complicates the problem.
> >>
> >
> >  If you run it from R, the right path will be put in place.  The
> >  tools::texi2dvi function does this.
> >
> >
> >   Furthermore, you cannot refer to Sweave.sty using its full path
> >>  as this would obviously not be system-independent.
> >>
> >>  Yet another small issue: If I configure Sweave to use a
> >>  temp directory for its temp files, and if this temp directory
> >>  does not exist, TeX fails with error diagnostics that is not
> >>  clear at all. Is there a way to have TeX automatically
> >>  create the directory if necessary?
> >>
> >
> >  I don't understand this one.  Are you talking about a "prefix=somedir/"
> >  option to Sweave?  That needs to be there before TeX is run.  I sometimes
> >  put a  dir.create("somedir", showWarnings=FALSE) call in an early chunk in
> >  the document to create it.
> >
> >  Duncan Murdoch
> >
> >   Thanks,
> >>  Dominick
> >>
> >>  On Tue, Dec 14, 2010 at 7:21 AM, Friedrich Leisch<
> >>  friedrich.leisch at stat.uni-muenchen.de>   wrote:
> >>
> >>  >   >>>>>   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
> >>  >
> >>  >   ______________________________________________
> >>  >   R-devel at r-project.org mailing list
> >>  >   https://stat.ethz.ch/mailman/listinfo/r-devel
> >>  >
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >>
> >>  ______________________________________________
> >>  R-devel at r-project.org mailing list
> >>  https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >
> >
>



More information about the R-devel mailing list