R将文本字段转换为函数

标签 r

我想使用字段中的信息并将其包含在 R 函数中,例如:

data #name of the data.frame with only one raw

"(if(nclusters>0){OptmizationInputs[3,3]*beta[1]}else{0})" # this is the raw

如果我想在函数中使用这些信息,我该怎么做?

Another example:
A=c('x^2')
B=function (x) A
B(2)
"x^2"  # this is the return. I would like to have the return something like 2^2=4.

最佳答案

使用 body<-并解析

A <- 'x^2'

B <- function(x) {}

body(B) <- parse(text = A)

B(3)
## [1] 9

还有更多想法here

关于R将文本字段转换为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260245/

相关文章:

r - 如何在 R 中绘制更平滑的曲线

r - 匹配。使用 MatchIt 和 Matching 进行数据模拟和估计。如何检索真实模型?

r - 如何在 Apple Silicon (M1) Mac 上安装 RcppArmadillo

r - 带 fst 的 bit64 整数

r - 如何计算连续行的时间差

r - dplyr::coalesce,但对于多行

r - 轨迹树

r - 在 R 中插值(并避免负插值)

r - 如何使用 R 中的热图绘制混淆矩阵?

css - 阻止shinydashboardPlus中的右侧边栏隐藏应用程序的主体