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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 30 01:43:44 CET 2006


On 10/29/06, Matthew Walker <m.g.walker at massey.ac.nz> wrote:
> Thank you Gabor for that explanation, I can now make a bit more sense of
> R's behaviour!  When I edit boxplot.stats, a new version is copied to my
> local workspace.  I have a few associated questions:
>
> 1. This has something to do with namespaces in R, and I know nothing
> about them.  Can anyone point me to some online documentation that could
> step me through the important points?  If that's not available (my
> searching failed to bring up anything) can anyone give me a quick list
> of functions that I could look up using help()?

Just issue the command:

search()

and you will get a list of environments through which R searches
in the order ordered, at least if you typed the command into the
console.  Also see ?environment

If the command was issued from a package with a namespace
it normally will look there first or if its is a method of
an S3 generic there is some info in ?UseMethod .

In general, if f is called from g it will look at the functions defined
in g, then the ones defined in g's lexical parent, i.e. the environment
where g was defined not where it was called from, the grandparent
and so on.

>
> 2. This question follows on from question (1): given that I have two
> copies of boxplot.stats (in what I assume are different namespaces), how
> can I specify a specific one to the function debug()?

e.g.
getAnywhere("boxplot.stats") # finds locations
# choose a location
debug(grDevices:::boxplot.stats)

>
> 3. How can I edit the original version of boxplot.stats without creating
> a copy in my local workspace?

See ?assignInNamespace .  Actually I think you are better off not to
do that in the first place.  Mucking around in the environments on the
search path might get you into trouble.

>
> 4. Is there a way that I can learn the status of the "debug flag" of a
> specific function?

Duncan posted code for that some time ago:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/27489.html

>
> 5. Finally, the function traceback() lists the call-stack.  How do I
> know which one of the two "boxplot.stats" functions was called?

I think you need to refer to the search list or your knowledge
of how it works in other cases.

>
> Thanks,
>
> Matthew
>
> On Fri, 2006-10-27 at 23:19 -0400, Gabor Grothendieck wrote:
> > The edit operations does not change the boxplot.stats that you
> > are debugging.  It creates a new boxplot.stats (i.e. now there
> > are two) and the new one does not have debugging turned on.
> > Try
> >
> > getAnywhere("boxplot.stats")
> >
> > and it finds two.  If you remove the one you just created using rm debugging
> > reappears since you are now accessing the original one for which
> > debugging is still on.  If you like you can turn debugging on in the
> > new one.
> >
> > On 10/27/06, Matthew Walker <m.g.walker at massey.ac.nz> wrote:
> > > Hi everyone,
> > >
> > > I think I have found a minor issue with the R function "boxplot.stats".
> > > But before I make such a rash comment, I'd like to check my facts by
> > > fixing what I think is the problem.  However, why I try to do this, R
> > > does not behave as I expect.  Can you tell me what I'm doing wrong?
> > >
> > > If I use the command:
> > > debug(boxplot.stats)
> > > I am allowed to step through the code as normal.
> > >
> > > I then want to edit the function, so I type:
> > > boxplot.stats <- edit(boxplot.stats)
> > > and I make my changes.
> > >
> > > I can check my changes have been made by typing:
> > > boxplot.stats
> > > and the updated version appears on screen.
> > >
> > > But now when I come to test my changes, the "debug" functionality has
> > > just up and disappeared; I can no longer step though the function as
> > > normal.  Further to that, it appears that the change has somehow not
> > > been registered, as exactly the same error occurs---although I cannot be
> > > sure that it is occurring inside boxplot.stats.
> > >
> > > Does anyone have any tips, suggestions or comments?  I'd love to be able
> > > to fix this.
> > >
> > > (For what it's worth I wish to change line 14 of boxplot.stats so that
> > > it reads "if (any(out[nna],na.rm=TRUE))".)
> > >
> > >
> > > Thank you for your thoughts,



More information about the R-help mailing list