[Rd] Detecting typo in function argument

John C Nash nashjc at uottawa.ca
Mon Dec 12 22:10:07 CET 2011


With some chagrin after spending a couple of hours trying to debug a script, I realized I
had typed in something like

ans<-optimx(start, myfn, mygr, lower<-lo, upper=up)

that is, the "<-" rather than "=". The outcome on my machine was a non-obvious error
several layers deep in the call stack. For info, optim() seems to stop much more quickly.

The error is "obvious", but I'm wondering if there is a simple way to trap or warn of it.
For completeness, I include the commands I used to force the error. Note that it will only
work fully with the latest (R-forge) version of optimx/optfntools because of the form of
gr="gfwd" that allows a choice of different numerical gradient routines.

This is a curiosity rather than a necessity, but if there is a simple way to check, I'll
put it in my codes.

Cheers,

JN

rm(list=ls())
start<-rep(3,6)
lo<-rep(2,6)
up<-rep(4,6)
flb <- function(x)
    { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
ans<-optim(start, flb, lower=lo, upper=up)
ans
ans<-optim(start, flb, lower<-lo, upper=up)
ans
ans1<-optim(start, flb, lower<-lo, upper=up)
ans1
require(optimx)
ans1x<-optimx(start, flb, lower<-lo, upper=up)
ans1x<-optimx(start, flb, gr="gfwd",lower<-lo, upper=up)
ans1<-optim(start, flb, gr=NULL,lower<-lo, upper=up)



More information about the R-devel mailing list