[R] Modify base R functions in Rprofile.site

Yihui Xie xieyihui at gmail.com
Mon Oct 5 06:30:32 CEST 2009


Hi Charlie,

Thanks a lot! Your "dark voodoo" really helps!

And also, thanks so much to Duncan for your detailed explanation!
Finally I used Charlie's trick to modify makeRweaveLatexCoderunner(),
and has left parse() & deparse() untouched.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



On Sun, Oct 4, 2009 at 5:51 PM, cls59 <chuck at sharpsteen.net> wrote:
>
>
> 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




More information about the R-help mailing list