【发布时间】:2018-03-02 15:12:21
【问题描述】:
我面临更改boxplot 函数以使晶须延伸到5 和95 分位数的问题。我知道我不是第一个尝试这个的人。
我已经阅读了这些主题
- 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
并在下面编写了这段注释代码,它应该将boxplot.default 代码更改为使用“myboxplot.stats”而不是“boxplot.stats”,但它不能完成这项工作。当我运行fixInNamespace 时,我收到错误消息,我无法更改boxplot.default,因为它已被锁定。由于我以前从未使用过命名空间、环境和 bildings,因此代码中可能存在明显的错误。如果是这样,我会很高兴,如果你也能指出我,或者就如何正确地做到这一点提供一些建议。
我很感激!
### 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
【问题讨论】: