[R] Modify base R functions in Rprofile.site

cls59 chuck at sharpsteen.net
Mon Oct 5 00:51:17 CEST 2009



Yihui Xie wrote:
> 
> 
> Thanks a lot, Charlie. What a coincidence -- I'm also working on 
> Sweave functions. parse() and deparse() can make the code more tidy 
> (they are used in source() and RweaveLatexRuncode()), 
> 
>> RweaveLatex 
> function () 
> { 
>     list(setup = RweaveLatexSetup, runcode = RweaveLatexRuncode, 
>         writedoc = RweaveLatexWritedoc, finish = RweaveLatexFinish, 
>         checkopts = RweaveLatexOptions) 
> } 
> <environment: namespace:utils> 
> 
> but we can either (1) let R automatically 'tidy up' our code and 
> remove all the comments or (2) keep the comments but leave the 
> original code untouched. 
> 
> I worked out a trick to preserve the comments while tidying up the R 
> code, and I want to replace parse() and deparse() with my customized 
> functions, in which case Sweave will keep the comments. 
> 
> However, my functions will need base::parse() and base::deparse() to
> help me, therefore my real difficulty is, how to let Sweave use my
> functions to parse and deparse R code without really modifying them in
> the base enviroment? I'm using the command 'R CMD Sweave'. 
> 
> 

Well, if you want to alter the behavior of RweaveLatexRunCode(), it can be
done from within a Sweave file. This happens to be what I did to Sweave-- I
wanted a little more control over the specifics of how it formatted code
chunk output in the .tex file. The steps involved are:

1. Make a copy of the Sweave function makeRweaveLatexCoderunner(). Patch it
so that it generates a RweaveLatexRuncode() function of your liking.

2. Overwrite utils:::makeRweaveLatexCodeRunner using the method I described
previously.

3. Generate a new code runner function using your patched version of
makeRweaveLatexCodeRunner().

4. Search all environments for old versions of RweaveLatexRuncode() and
replace them with your patched one.

An example of this approach is used to build the tikzDevice vignette, you
can view it at:

http://github.com/Sharpie/RTikZDevice/blob/master/inst/doc/tikzDevice.Rnw

Look for the line that says "Begin Mother of All R Hacks".




Yihui Xie wrote:
> 
> 
> If there is no neat approach, I'll change my question to: how to write a
> package,
> say, Sweave2, that can be run using command line 'R CMD Sweave2 file'?
> 
> 

This is also doable, at least for Linux and OS X systems that are installing
from source. You need to add a Make target that installs your script, named
Sweave2, into R_HOME/bin/ when the package is installed. This is the
approach we took in the pgfSweave package-- you can view an example at:

http://github.com/cameronbracken/pgfSweave/blob/master/src/Makevars

I hope this helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/Modify-base-R-functions-in-Rprofile.site-tp25735437p25743082.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list