How can I overwrite a method in R?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I overwrite a method in R?

Tim Hesterberg-2
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How can I overwrite a method in R?

Duncan Murdoch-2
On 09/10/2014, 2:14 AM, Tim Hesterberg 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.

To be clear, we're talking S3 methods here.  Your long version below
documents the issue pretty well.  There isn't a simple way to do what
you want, but there are a couple of not-really-simple ways.

The best is to submit a bug report with a patch for the method that
doesn't work, and eventually the base version will probably get fixed.
But maybe not the way you fixed it, and we might not agree that it's a
bug.  So this is a little slow and no use while you are testing your patch.

You can install R from source, and develop your patch that way.  This is
best if you are thinking of submitting the bug report.

If you want local changes that might not make it into the base code,
then I think the best thing to do is to change the class, and write a
method for a new class.  hist() produces objects of class "histogram";
you can modify a local copy to produce objects of class
c("newhistogram", "histogram").  Then plot() on one of those will call
plot.newhistogram in preference to plot.histogram.

An alternative approach that is less work in the short term, but more
error prone in the long term is to make a habit of calling hist() with
plot=FALSE, then call plot.histogram() on the result explicitly.  If you
have a local version of plot.histogram() this should use yours.

One more inline comment below.

>
> 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.

You could fix this by prefixing C_BinCount as graphics:::C_BinCount, but
that's internal code that might change in future releases.

Duncan Murdoch

>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How can I overwrite a method in R?

hadley wickham
In reply to this post by Tim Hesterberg-2
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How can I overwrite a method in R?

baccts
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: [hidden email]
> Date: Thu, 9 Oct 2014 07:00:31 -0500
> To: [hidden email]
> CC: [hidden email]
> 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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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/
>
> ______________________________________________
> [hidden email] 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.
     
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How can I overwrite a method in R?

Tim Hesterberg-2
Thank you Duncan, Brian, Hadley, and Lin.

In Lin's suggestion, I believe the latter two statements should be
reversed, so that the environment is added before the function is placed
into the graphics namespace.

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

The middle statement could also be
environment(plot.histogram) <- environment(graphics:::plot.histogram)
The point is to ensure that the replacement version has the same
environment as the original.

Having tested this, I will now submit a bug report :-)

On Thu, Oct 9, 2014 at 9:11 AM, C Lin <[hidden email]> wrote:

> 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: [hidden email]
> > Date: Thu, 9 Oct 2014 07:00:31 -0500
> > To: [hidden email]
> > CC: [hidden email]
> > 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 <[hidden email]>
> 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]]
> >>
> >> ______________________________________________
> >> [hidden email] 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/
> >
> > ______________________________________________
> > [hidden email] 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.
>
>



--
Tim Hesterberg
http://www.timhesterberg.net
 (resampling, water bottle rockets, computers to Costa Rica, hot shower =
2650 light bulbs, ...)

Help your students understand statistics:
    Mathematical Statistics with Resampling and R, Chihara & Hesterberg
http://www.timhesterberg.net/bootstrap/

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.