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
- https://stat.ethz.ch/pipermail/r-help/2001-November/016817.html
- Modifying an R package function for current R session; assignInNamespace not behaving like fixInNamespace?
- How to unlock environment in R?
- http://adv-r.had.co.nz/Environments.html
- https://gist.github.com/wch/3280369#file-unlockenvironment-r
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