【发布时间】:2014-01-15 09:26:52
【问题描述】:
这似乎是一个基本问题,但我似乎无法在 stackoverflow 上找到答案。
如何获得如下效果:
f <- function(x = 1){x^2}
miracle(f)
[1] "x^2"
上下文是一个闪亮的应用程序(由 RStudio 打包),其中我有一个 textInput() 函数,我为其提供了一个初始值 x^2。虽然这有效:
textInput(inputId = "inFun", label = h4("Enter a function:"), value = "x^2")
这不是:
textInput(inputId = "inFun", label = h4("Enter a function:"), value = f)
看来我需要像“x^2”这样的值。
以下是我尝试过的几种变体的代表性示例:
eval(parse(text = f))
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
f(x = "x")
Error in x^2 : non-numeric argument to binary operator
`f`
function(x){x^2}
f(x = `x`)
Error in f(x = x) : object 'x' not found
这个有内置函数吗?
【问题讨论】:
-
body(f)呢? -
可以调整标题吗?没有把答案写在标题里?