[R] Modify base R functions in Rprofile.site

Duncan Murdoch murdoch at stats.uwo.ca
Sun Oct 4 18:54:37 CEST 2009


On 04/10/2009 12:41 PM, cls59 wrote:
> 
> 
> Yihui Xie wrote:
>> Hi everyone,
>>
>> I want to modify two base R functions 'parse' and 'deparse'
>> immediately after R has started, so I added some code in the file
>> 'Rprofile.site' under the 'etc' directory. Here is a simple example:

Why not just modify their source, and rebuild R?

Duncan Murdoch

>>
>> parse=function(...){
>>     base::parse(...)
>> }
>>
>> I'll get an error when I start R as follows:
>>
>> Error: cannot change value of locked binding for 'parse'
>>
>> Is there a solution for my problem? Please note that I *have to*
>> override the function names. Thanks very much!
>>
>>
> 
> 
> Hi Yihui,
> 
> I have had cause to do some some similar things to Sweave functions-- maybe
> the following will help:
> 
>   # Find out where the function lives.
>   env <- as.environment( 'package:base' )
> 
>   # Crack the binding.
>   unlockBinding( 'parse', env )
> 
>   # Replace the function.
>   assignInNamespace( 'parse', function(...){
>   
>    # Your function here, or an object that contains it.
> 
>    }, ns = 'base' )
> 
>   # Relock the binding.
>   lockBinding( 'parse', env )
> 
> 
> Note that the above is *very* dark voodoo-- it may cause unforeseen
> consequences.
> 
> Good luck!
> 
> -Charlie
> 
> -----
> Charlie Sharpsteen
> Undergraduate
> Environmental Resources Engineering
> Humboldt State University




More information about the R-help mailing list