6
我試圖使用一個變量來存儲函數名稱作爲字符串來繪製曲線。像下面這樣:將表達式作爲變量傳遞給曲線
f1 <- function(x) 0*x
f2 <- function(x) 1 + 0*x
f3 <- function(x) 2 + 0*x
fn <- "f1"
plot.new()
plot.window(xlim = c(0, 1), ylim = c(-1, 3))
curve(get(fn), add = TRUE)
Error in curve(get(fn), add = TRUE) :
'expr' must be a function, or a call or an expression containing 'x'
curve(f1, add = TRUE)
curve(f2, add = TRUE)
curve(f3, add = TRUE)
我知道,我的思念與如何curve
處理表達與替代,或如何get
被傳遞功能的東西。我特別困惑,因爲class(get(fn))
返回「功能」。
如果你想添加額外的參數觸發產生功能,比如'加= TRUE',它會看起來像這樣:'do.call(曲線,列表(as.name(fn),add = TRUE))' –
你也可以通過首先強制評價'{get(fn) - > f;曲線女,添加= TRUE)}' –