[英]Error in get(as.character(FUN), mode = “function”, envir = envir)
I am new to R, so forgive me if the question is a little silly.我是 R 的新手,所以如果问题有点愚蠢,请原谅我。 I am trying to write a simple while loop for a value function iteration.
我正在尝试为值函数迭代编写一个简单的 while 循环。 My function (optim.routine) uses the solver ipoptr.
我的函数 (optim.routine) 使用求解器 ipoptr。 Here is my code:
这是我的代码:
d<-1
old1<-0
old2<-0
num.iter<-0
i.esp<-1e-05
i.T<-100
lb<-0
ub<-10
while (d>i.eps & num.iter<i.T){
new1 <- optim.routine(old1, old2, eval_f=eval_f, eval_grad_f=eval_grad_f, lb=lb, ub=ub, update=FALSE)
d<-dist(c(old1, new1), method="euclidean")
num.iter<-num.iter+1
old1<-new1
}
where optim.routine is the following function:其中 optim.routine 是以下函数:
optim.routine<-function(old1, old2, eval_f=obj, eval_grad_f=obj.deriv, lb=lb, ub=ub, update){
if (isTRUE(update)){
var2<-old2
var1<-old1
var1.deriv<-deriv(var1)
optimize <- ipoptr(x0 = old2, eval_f = eval_f, eval_grad_f = eval_grad_f, lb = lb,
ub = ub)
new1<- optimize$objective
new2<- optimize$solution
old2<-new2
old1<-new1
}else{
var2<-old2
var1<-old1
var1.deriv<-vf.deriv(var1)
optimize <- ipoptr(x0 = old2, eval_f = eval_f, eval_grad_f = eval_grad_f, lb = lb,
ub = ub)
new1<- optimize$objective
new2<- optimize$solution
old1<-new1
}
}
and deriv is a function that computes derivatives. deriv 是一个计算导数的函数。
I get the following error if i try to run the code:如果我尝试运行代码,我会收到以下错误:
source('/mnt/ide0/home/myname/Documents/optim.R')
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'fn' of mode 'function' was not found
and if I debug the function:如果我调试函数:
Browse[2]> n
Error in isTRUE(update) : argument "update" is missing, with no default
If I only source the function without the while loop no error is displayed.如果我只在没有 while 循环的情况下获取函数,则不会显示错误。 Honestly, I have no clue.
老实说,我不知道。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 Thanks!
谢谢!
Claudia克劳迪娅
I had exactly the same error message when I named a variable with the same name of an existing function in R. I've found this tip here: http://notepad.patheticcockroach.com/2565/a-bad-idea-in-r-using-variables-with-the-same-name-as-existing-functions/ Hope it helps you too.
当我用 R 中现有函数的相同名称命名一个变量时,我得到了完全相同的错误消息。我在这里找到了这个提示: http : //notepad.patheticcockroach.com/2565/a-bad-idea-in -r-using-variables-with-the-same-name-as-existing-functions/希望它也能帮助你。 – FraNut Oct 12 at 11:26
– FraNut 10 月 12 日 11:26
He's right refrain from using variables that might be function names too.他避免使用可能也是函数名的变量是对的。
eg例如
z1<-aggregate(steps ~ interval, data_df, mean)
mean<-mean(z[,2],na.rm = TRUE)
mean is a variable and a function name passed as an argument to the aggregate function causing a conflict mean 是一个变量和一个函数名作为参数传递给聚合函数导致冲突
Many times that error will appear when you previously created an object called "mean" in the R environment.很多时候,当您之前在 R 环境中创建一个名为“mean”的对象时,会出现该错误。 This creates a conflict when calling the function "mean".
这会在调用函数“mean”时产生冲突。 To stop this error use:
要停止此错误,请使用:
rm(mean)
This removes the object "mean" from the environment and allows R to call the function "mean".这将从环境中删除对象“mean”并允许 R 调用函数“mean”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.