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

2006-10-30 Thread Martin Maechler
 Duncan == Duncan Murdoch [EMAIL PROTECTED]
 on Sat, 28 Oct 2006 08:03:33 -0400 writes:

[...]

Duncan Not sure why boxplot.stats is in grDevices rather
Duncan than graphics, but that's not really relevant to
Duncan your problems.

because it does computation for Graphics (capitalized on purpose)
which should not at all be limited to old-style S graphics which
is the topic of the graphics package.
E.g. grid and grid-based graphics should use boxplot.stats
(for consistency).
For the same reason, color computations are in 'grDevices',
[[and we have acknowledged a while ago that 'grDevices' has become a
  slight misnomer

Martin Maechler

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


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

2006-10-30 Thread Matthew Walker
Hi Martin,

Sorry, I did intend to put some examples in but must have forgotten.
This example is easy to understand:

boxplot(c(1,Inf,Inf,Inf,Inf))

has lower bound: 1, lower quartile: Inf, median: Inf, upper quartile:
Inf, upper bound: Inf.
The command currently errors with the obscure message
Error in if (any(out[nna])) stats[c(1, 5)] - range(x[!out], na.rm =
TRUE) : missing value where TRUE/FALSE needed
In this case I think boxplot should draw a (lower) whisker at 1 and
nothing else.  If my alteration is made, boxplot draws just that.

The argument against this change is that the range argument
(see ?boxplot) isn't able to be considered (but what is the value of the
inter-quartile range anyway? Inf - Inf = NaN.)

The smallest example to demonstrate this issue is:

boxplot(c(1,Inf,Inf))

I hope that's helpful!

Cheers,

Matthew

On Mon, 2006-10-30 at 15:53 +0100, Martin Maechler wrote:
 Hi Matthew,
 
 I'm considering to apply your proposed change to the R sources
 of boxplot.stats().
 
 However, I'd like to see interesting examples where the new
 and old version exhibit differences.
 Since you've now spent so much time on this,
 you will have a reproducible-code small example, will you?
 
 Thanks in advance!
 Martin 
 
 Martin [EMAIL PROTECTED]  http://stat.ethz.ch/people/maechler
 Seminar für Statistik, ETH Zürich  LEO C16Leonhardstr. 27
 CH-8092 Zurich, SWITZERLAND
 phone: +41-44-632-3408   fax: ...-1228  
 
 
  Matthew == Matthew Walker [EMAIL PROTECTED]
  on Mon, 30 Oct 2006 19:32:25 +1300 writes:
 
 Matthew On Sun, 2006-10-29 at 20:18 -0500, Duncan Murdoch
 Matthew wrote:
  If you're sure your change is a good idea then post a
  patch here along with an explanation of why it's so good:
  and it might make it into the next release.
 
 Matthew Thank you to both Duncan and Gabor, your help was
 Matthew really appreciated.
 
 Matthew My 10 character alteration did what I hoped it
 Matthew would.  So I'd like to offer it to you or whoever
 Matthew else might be interested.
 
 Matthew Boxplot does it's job well, it even mostly works
 Matthew with infinite values by not plotting certain lines.
 Matthew For example, if the upper bound is infinite, the
 Matthew upper whisker isn't plotted.
 
 Matthew However boxplot doesn't work if the upper bound,
 Matthew upper quartile, median, and lower quartile are all
 Matthew infinite.  Although there is sufficient data to
 Matthew plot a lower bound, boxplot.stats errors instead.
 Matthew This error also occurs when the lower bound through
 Matthew to the upper quartile are negative infinity.
 
 Matthew This is not tricky to fix.  All that needs to
 Matthew change is line 14 of boxplot.stats.  It currently
 Matthew reads: if (any(out[nna])) Changing it to: if
 Matthew (any(out[nna],na.rm=TRUE)) fixes these issues.
 
 Matthew Cheers,
 
 Matthew Matthew
 
 Matthew __
 Matthew R-help@stat.math.ethz.ch mailing list
 Matthew https://stat.ethz.ch/mailman/listinfo/r-help PLEASE
 Matthew do read the posting guide
 Matthew http://www.R-project.org/posting-guide.html and
 Matthew provide commented, minimal, self-contained,
 Matthew reproducible code.


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


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

2006-10-29 Thread Matthew Walker
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()?  

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()?

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

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

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

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 [EMAIL PROTECTED] 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,
 
  Matthew
 
  __
  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.
 


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


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

2006-10-29 Thread Matthew Walker
On Sat, 2006-10-28 at 08:03 -0400, Duncan Murdoch wrote:
 On 10/27/2006 11:06 PM, Matthew Walker 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)).)
 
 Besides what Gabor said, an issue here is that boxplot.stats lives in 
 the grDevices namespace.  If you call boxplot(), it will look there 
 before it sees your locally modified copy.
 
 For testing, you can call boxplot.stats directly from the console, or 
 also make a local copy of boxplot.
 
 Not sure why boxplot.stats is in grDevices rather than graphics, but 
 that's not really relevant to your problems.
 
 Duncan Murdoch
 

Hi Duncan,

Thanks for your reply.

How do you know that (i) boxplot.stats lives in the grDevices namespace?
and (ii) how do you know/change that boxplot will look in grDevices
before it uses the 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?

Thanks again,

Matthew

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


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

2006-10-29 Thread Duncan Murdoch
On 10/29/2006 6:08 PM, Matthew Walker wrote:
 On Sat, 2006-10-28 at 08:03 -0400, Duncan Murdoch wrote:
 On 10/27/2006 11:06 PM, Matthew Walker 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)).)
 Besides what Gabor said, an issue here is that boxplot.stats lives in 
 the grDevices namespace.  If you call boxplot(), it will look there 
 before it sees your locally modified copy.

 For testing, you can call boxplot.stats directly from the console, or 
 also make a local copy of boxplot.

 Not sure why boxplot.stats is in grDevices rather than graphics, but 
 that's not really relevant to your problems.

 Duncan Murdoch

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

 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.

Duncan Murdoch

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


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

2006-10-29 Thread Gabor Grothendieck
On 10/29/06, Matthew Walker [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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,

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


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

2006-10-29 Thread Gabor Grothendieck
On 10/29/06, Duncan Murdoch [EMAIL PROTECTED] wrote:
 On 10/29/2006 6:08 PM, Matthew Walker wrote:
  On Sat, 2006-10-28 at 08:03 -0400, Duncan Murdoch wrote:
  On 10/27/2006 11:06 PM, Matthew Walker 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)).)
  Besides what Gabor said, an issue here is that boxplot.stats lives in
  the grDevices namespace.  If you call boxplot(), it will look there
  before it sees your locally modified copy.
 
  For testing, you can call boxplot.stats directly from the console, or
  also make a local copy of boxplot.
 
  Not sure why boxplot.stats is in grDevices rather than graphics, but
  that's not really relevant to your problems.
 
  Duncan Murdoch
 
 
  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...

I think UseMethod uses dynamic scoping whereas ordinary funtions
use lexical scoping.  From ?UseMethod

Name spaces can register methods for generic functions. To support
this, UseMethod and NextMethod search for methods in two places: first
in the environment in which the generic function is called, and then
in the registration data base for the environment in which the generic
is defined (typically a name space). So methods for a generic function
need to be available in the environment of the call to the generic, or
they must be registered. (It does not matter whether they are visible
in the environment in which the generic is defined.)

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


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

2006-10-29 Thread Matthew Walker
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

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


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

2006-10-29 Thread Gabor Grothendieck
On 10/29/06, Matthew Walker [EMAIL PROTECTED] 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.


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

2006-10-29 Thread Duncan Murdoch
On 10/29/2006 8:01 PM, Matthew Walker 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.

Sorry, I didn't realize that boxplot.default was calling boxplot.stats. 
  To get boxplot.default to call your local copy of boxplot.stats, you 
need to make a local copy of it, and (the part I left out before) set 
its environment to globalenv().  For example:

boxplot.default - boxplot.default
environment(boxplot.default) - globalenv()

The environment is where it goes to look for names that aren't defined 
locally within it.

 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?

Well, you can edit the source to R, but it's not usually advisable: 
when the next release comes out, you'll have to worry about merging your 
changes in.  It's better to write your own function with a different 
name to do what you want.

If you're sure your change is a good idea then post a patch here along 
with an explanation of why it's so good:  and it might make it into the 
next release.

Duncan Murdoch

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


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

2006-10-29 Thread Matthew Walker
On Sun, 2006-10-29 at 20:18 -0500, Duncan Murdoch wrote:
 If you're sure your change is a good idea then post a patch here along 
 with an explanation of why it's so good:  and it might make it into the 
 next release.

Thank you to both Duncan and Gabor, your help was really appreciated.

My 10 character alteration did what I hoped it would.  So I'd like to
offer it to you or whoever else might be interested.

Boxplot does it's job well, it even mostly works with infinite values
by not plotting certain lines.  For example, if the upper bound is
infinite, the upper whisker isn't plotted.  

However boxplot doesn't work if the upper bound, upper quartile, median,
and lower quartile are all infinite.  Although there is sufficient data
to plot a lower bound, boxplot.stats errors instead.  This error also
occurs when the lower bound through to the upper quartile are negative
infinity.

This is not tricky to fix.  All that needs to change is line 14 of
boxplot.stats.  It currently reads:
if (any(out[nna]))
Changing it to:
if (any(out[nna],na.rm=TRUE))
fixes these issues.

Cheers,

Matthew

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


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

2006-10-28 Thread Duncan Murdoch
On 10/27/2006 11:06 PM, Matthew Walker 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)).)

Besides what Gabor said, an issue here is that boxplot.stats lives in 
the grDevices namespace.  If you call boxplot(), it will look there 
before it sees your locally modified copy.

For testing, you can call boxplot.stats directly from the console, or 
also make a local copy of boxplot.

Not sure why boxplot.stats is in grDevices rather than graphics, but 
that's not really relevant to your problems.

Duncan Murdoch

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


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

2006-10-27 Thread Matthew Walker
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,

Matthew

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


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

2006-10-27 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] 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,

 Matthew

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


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