嗨,我試着準備一個關於R中的閉包的小代碼,當時我偶然發現了一些奇怪的行爲。使用調試我意識到發生了故障,由於這樣的事實,LM內正在執行這一行R中的關閉和lm
mf <- eval(mf, parent.frame())
但它似乎擊敗帶密封蓋的編程功能方面?
我失蹤了什麼?這裏是我的小例子EVAL
# typical closure use
foo <- function(formula,data,weight=NULL) {
if(is.null(weight)) w=ones(nrow(data),1) else w=weight
force(w)
lm(formula,data,weights=w)
}
setup_something <- function(...) {
function(formula,data) {
foo(formula,data,...)
}
}
# set up our model function
model <- setup_something()
# set up our data
df = data.frame(x=seq(1,10,1),y=c(1:10)^1.5 * 3 + 2)
model(y~x,df)
錯誤(表達式,ENVIR,enclos):對象 'W' 未找到
它可能會有幫助:[範圍界定問題](http://adv-r.had.co.nz/Computing-on-the-language.html#scoping-issues) – zero323