我有一个函数和一个参数列表。
F <- function(a,b,...) {a^b+b/a}
L <- list("a" = 5, "b" = 2, "c" = 0)
我想用未知的 x (或“x”)替换参数之一(“a”、“b”或“c”)并使用 ggplot 的 stat_function 进行绘图。
这些计算是闪亮应用程序的一部分,用户将 1) 从下拉列表中选择一个参数,例如“a”,作为未知数,2) 使用滑块选择其他参数的值。 L 中的数字 5、2、0 是默认参数值,在用户交互之前使用。有几个这样的函数。这里参数列表 L 有一个在 F 中未使用的元素。
我已经被这个问题困扰太久了,以至于我已经无法正常思考了。在我尝试过的许多事情中,以下是其中之一:
# select a parameter to vary:
Y <- "a"
f1 <- function(f = F, l = L, y = Y, x, ...){
l[y] <- x # replace "a" with x
do.call(f, l, ...)
}
# make a stat_function ggplot:
library("ggplot2")
df <- data.frame(x = c(0,10))
p <- ggplot(df, aes(x))
p <- p + stat_function(fun = f1)
print(p)
这将返回以下错误:
Error in (function (f = F, l = L, y = Y, x, ...) :
argument "x" is missing, with no default
Error in as.environment(where) : 'where' is missing
我尝试了几种变体,包括:设置 l[y]