[R] How can I overwrite a method in R?

C Lin baccts at hotmail.com
Thu Oct 9 18:11:40 CEST 2014


I posted similar question a while ago. Search for "modify function in a package".
In your case, the following should work.

source('plot.histogram.R')
assignInNamespace('plot.histogram',plot.histogram,ns='graphics')
environment(plot.histogram) <- asNamespace('graphics');

Assuming you have your own plot.histogram function inside "plot.histogram.R" and the plot.histogram function you are trying to overwrite is in graphics package.

Lin

----------------------------------------
> From: h.wickham at gmail.com
> Date: Thu, 9 Oct 2014 07:00:31 -0500
> To: timhesterberg at gmail.com
> CC: R-help at stat.math.ethz.ch
> Subject: Re: [R] How can I overwrite a method in R?
>
> This is usually ill-advised, but I think it's the right solution for
> your problem:
>
> assignInNamespace("plot.histogram", function(...) plot(1:10), "graphics")
> hist(1:10)
>
> Haley
>
> On Thu, Oct 9, 2014 at 1:14 AM, Tim Hesterberg <timhesterberg at gmail.com> wrote:
>> How can I create an improved version of a method in R, and have it be used?
>>
>> Short version:
>> I think plot.histogram has a bug, and I'd like to try a version with a fix.
>> But when I call hist(), my fixed version doesn't get used.
>>
>> Long version:
>> hist() calls plot() which calls plot.histogram() which fails to pass ...
>> when it calls plot.window().
>> As a result hist() ignores xaxs and yaxs arguments.
>> I'd like to make my own copy of plot.histogram that passes ... to
>> plot.window().
>>
>> If I just make my own copy of plot.histogram, plot() ignores it, because my
>> version is not part of the same graphics package that plot belongs to.
>>
>> If I copy hist, hist.default and plot, the copies inherit the same
>> environments as
>> the originals, and behave the same.
>>
>> If I also change the environment of each to .GlobalEnv, hist.default fails
>> in
>> a .Call because it cannot find C_BinCount.
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> http://had.co.nz/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
 		 	   		  


More information about the R-help mailing list