0

I am faced with the problem of changing the boxplot function so that the whiskers extend to the 5 and 95 quantile. I know I'm not the firts one to try this.

I've read these threads

and wrote this commented code below which should change the boxplot.default code to use 'myboxplot.stats' instead of 'boxplot.stats', but it does not do the job. When I run fixInNamespace I get the error, that I can't alter boxplot.default because it is locked. As I've never worked with namespaces, environments and bildings before, there may be an obvious mistake in the code. If that is so, I'd be glad, if you could point it out too me, or else give some advice on how to do this properly.

I appreciate it!

### new boxplot stats function with 5%/95% whiskers
### source: https://stat.ethz.ch/pipermail/r-help/2001-November/016817.html

myboxplot.stats <-  function (x, coef = NULL, do.conf = TRUE, do.out = TRUE) {
  nna <- !is.na(x)
  n <- sum(nna)
  stats <- quantile(x, c(.05,.25,.5,.75,.95), na.rm = TRUE)
  iqr <- diff(stats[c(2, 4)])
  out <- x < stats[1] | x > stats[5]
  conf <- if (do.conf)
  stats[3] + c(-1.58, 1.58) * diff(stats[c(2, 4)])/sqrt(n)
  list(stats = stats, n = n, conf = conf, out = x[out & nna])
}

old.boxplot <- boxplot.default ### for comparison

### https://stackoverflow.com/questions/23279904/modifying-an-r-package-function-for-current-r-session-assigninnamespace-not-beh
fixInNamespace("boxplot.default", ns = "graphics")

### Error in assignInNamespace(subx, x, ns) : 
###   locked binding of ‘boxplot.default’ cannot be changed

### how to unlock binding:
### https://stackoverflow.com/questions/19132492/how-to-unlock-environment-in-r
### http://adv-r.had.co.nz/Environments.html

### in boxplot.default I find: '<environment: namespace:graphics>'

unlockBinding(sym = "boxplot.default", env = asNamespace("graphics"))
### checking if it worked (https://gist.github.com/wch/3280369#file-unlockenvironment-r)
bindingIsLocked(sym = "boxplot.default", env = asNamespace("graphics")) ### FLASE
fixInNamespace("boxplot.default", ns = "graphics") ### here I want to change 'boxplot.stats' to 'myboxplot.stats'

### Error in assignInNamespace(subx, x, ns) : 
###   locked binding of ‘boxplot.default’ cannot be changed

lockBinding(sym = "boxplot.default", env = asNamespace("graphics"))
new.boxplot <- boxplot.default ### for comparison
PetraR
  • 3
  • 3

1 Answers1

1

If you want, you can use the godmode package from Github:

# save original version
orig <- graphics::boxplot.default

# devtools::install_github("miraisolutions/godmode")
godmode:::assignAnywhere("boxplot.default", boxplot.default_new)

# switch back
godmode:::assignAnywhere("boxplot.default", orig)

boxplot.default_new should then be your re-write of boxplot.default, possibly including your function myboxplot.stats (maybe even rename it) and the call to it.

RolandASc
  • 3,863
  • 1
  • 11
  • 30
  • Thank you RolandASc, I tested your code, which is easy to understand for a beginner and it worked! Am I right to assume, that with a package named `godmode` and the structure of replacing existing functions as you've provided, I can alter any function to my needs in their namespace/environment/place? – PetraR Mar 02 '18 at 17:39
  • well, this will take you quite far, but it cannot cover all possible cases. so instead of `any` let's just say `many` :) needless to say that it's better to keep such substitutions to a minimum – RolandASc Mar 05 '18 at 13:53