如果我們直接R中調用一個函數,懶惰的評價發生,使函數參數不計算,直到它們在函數體中遇到。這樣做的一個效果是在函數的開頭使用match.call()
(比如lm
這樣的模型修正器)會使用未經評估的函數參數捕獲該調用。因此,通過執行模型擬合函數,可以用promise來取代對函數的調用,而不是通過評估參數。這樣做的缺點是函數必須在未獲取參數的調用之前執行。創建未計算的函數調用未評估的參數
另一方面,函數R的call
可用於創建對模型擬合函數的未評估調用,但會評估指定的參數。我想要做的是創建一個對未調用的函數的對象,該函數中函數參數也是未評估的。一個相當簡單的例子(假裝arg1 + arg2
-part是一些東西,是計算成本):
Foo <- function(arg1, arg2){
mycall <- match.call()
value <- arg1 + arg2
list(value = value,
call = mycall)
}
x <- 1
y <- 2
# calling Foo directly
test1 <- Foo(x,y)
test1 # the call with unevaluated arguments is now accessible, but the
# computations in Foo had to be performed as well.
# now via 'call'
Foocall <- call("Foo", arg1 = x, arg2 = y)
test2 <- eval(Foocall)
test2
Foocall # Foocall is an unevaluated call to Foo, but arg1 and arg2 in
# Foocall are no longer promises.
我想是這樣的:
Foocall <- call("Foo", arg1 = x, arg2 = y, eval.dots = FALSE)
產生
# not run
> Foocall
> Foo(arg1 = x, arg2 = y)
有沒有一個簡單的解決我的問題?
如果我理解正確的你,當你調用Foocall你想'美孚(ARG1 = X,ARG2 = Y)(型)'那是未評估的x和y? – infominer