我和R玩弄了一下,我很想知道为什么这不起作用:
#! /usr/bin/Rscript
# initializes function
s <- function() {
return(0);
};
# run function to see it's there (should return 0)
s();
while (s() != 12) {
# print function to see its contents
print(s);
# rewrite function, hopefully it will return 12 now
s <- paste("function() {",
"return(",
sample(1:100,1),
"+",
sample(1:100,1),
")",
"}"
)
}
print(solve);
输出结果为:
dooh@dooh:~$ '/home/dooh/Desktop/solve.R/solve.R'
[1] 0
function ()
{
return(0)
}
Error: could not find function "s"
Execution halted
不知何故,s
变成了一个字符串,并且不再作为一个函数存活 - 因此,while
之后的评估将永远不会返回12.我怎样才能强制执行字符串进入一个功能?
答案 0 :(得分:0)
请改为尝试:
s <- function() {
return(0);
};
# run function to see it's there (should return 0)
s();
while (s() != 12) {
# print function to see its contents
print(s);
# rewrite function, hopefully it will return 12 now
s <- as.function(alist(sample(1:100,1)+sample(1:100,1)))
}