Re: [R] Debugging R's code: boxplot.stats

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 30 Oct 2006 - 01:17:44 GMT

On 10/29/06, Matthew Walker <m.g.walker@massey.ac.nz> wrote:
> On Sun, 2006-10-29 at 18:47 -0500, Duncan Murdoch wrote:
> [snip]
> > > Hi Duncan,
> > >
> > > Thanks for your reply.
> > >
> > > How do you know that (i) boxplot.stats lives in the grDevices namespace?
> >
> > getAnywhere(boxplot.stats) starts out as
> >
> > > getAnywhere(boxplot.stats)
> > A single object matching 'boxplot.stats' was found
> > It was found in the following places
> > package:grDevices
> > registered S3 method for boxplot from namespace grDevices
> > namespace:grDevices
> >
> > > and (ii) how do you know/change that boxplot will look in grDevices
> > > before it uses the local copy?

> >
> > That was actually a guess. But checking now:

> >
> > > getAnywhere(boxplot)
> > A single object matching 'boxplot' was found
> > It was found in the following places
> > package:graphics
> > namespace:graphics
> > with value
> >
> > function (x, ...)
> > UseMethod("boxplot")
> > <environment: namespace:graphics>

> >
> > so boxplot() is in the graphics package, and works in that namespace.
> > The graphics namespace file starts out
> >
> > import(grDevices)
> >
> > so functions in that package will look in grDevices before they look
> > elsewhere. I thought that meant that S3 methods would be found there
> > before they're found anywhere else, but I didn't actually check this.
> > So let's check:

> >
> > > boxplot.stats <- function(x, ...) stop("mine")
> > > x <- 1
> > > class(x) <- "stats"
> > > boxplot(x)
> > Error in boxplot.stats(x) : mine

>

> > Whoops! Looks as though my local copy does get found first. Not sure
> > if this is a bug...
>

> That's an interesting way to check. If I enter
> boxplot.stats <- function(x, ...) stop("mine")
> boxplot(1)
>

> I get a graph (i.e. no error) and so I concluded that the original
> boxplot is calling the original boxplot.stats. In other words, the
> original is ignoring my local copy.
>

> >
> > > I tried to do as you suggested and create a local copy of boxplot. I
> > > used the following commands:
> > > boxplot.stats <- edit(boxplot.stats) # Made changes to line 14
> > > boxplot <- edit(boxplot)
> > > boxplot.default <- edit(boxplot.default) # Added a call to "cat()"
> > >
> > > When I called boxplot() the local copy was executed (as I could see the
> > > output of my "cat" commands), however it appears that the local copy of
> > > boxplot.stats isn't the one being called from my version of "boxplot".
> > > How do I fix this?
> >
> > That's very strange. Which R version are you using? Are you sure the
> > wrong version was being called? I was doing my tests in 2.4.0.
>

> I'm using 2.4.0 too (on Linux; sessionInfo() copied below)
>

> If I execute the following:
> boxplot.stats <- function(x, ...) stop("mine")
>

> boxplot <- edit(boxplot)
> # Added cat("local boxplot\n")
>

> boxplot.default <- edit(boxplot.default)
> # Added cat("local boxplot.default\n")
>

> boxplot(1)
>

> I get a graph with the text
> "local boxplot
> local boxplot.default"
>

> Had boxplot.default called the local version of boxplot.stats, I would
> have expected the error "mine".
>

> Is there a way I can avoid all this? Perhaps I can somehow edit the
> file that R loads when it reads the original "boxplot.stats"?
>
>

> Cheers,
>

> Matthew
>

> *** Output of sessionInfo(): ***
> R version 2.4.0 (2006-10-03)
> i686-redhat-linux-gnu
>

> locale:
> LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C
>

> attached base packages:
> [1] "methods" "stats" "graphics" "grDevices" "utils" "datasets"
> [7] "base"
>

The problem is that even though you have copied boxplot.default, its environment is still the graphics package so when boxplot.default looks for boxplot.stats it looks in the graphics package first so you need to reset the environment of boxplot.default like this:

Type this into a fresh R session:

environment(boxplot.default) <- .GlobalEnv # this will also copy it boxplot.stats <- edit(boxplot.stats) # add cat("boxplot.stats\n") boxplot(1:10)



R-help@stat.math.ethz.ch 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. Received on Mon Oct 30 12:25:49 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 30 Oct 2006 - 03:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.