[R-pkg-devel] Strange behaviour of function called from within a function

J C Nash pro|jcn@@h @end|ng |rom gm@||@com
Sat Aug 13 15:41:02 CEST 2022


Thanks to John Fox and Noah Greifer. Both their approaches resolved my immediate
problem.

That is, to provide a summary of the fix of my example code,

tw <- function(formula, data, start, control, trace, weights) {
   firstcoef <- c(b1=199, b2=50, b3=0.3)
   cat("firstcoef:\n")
   print(firstcoef)
   cat("weights:"); print(weights)
# Following fails -- closure error
#  secondw<-nls(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights=weights)
# from noah.greifer using gmail.com # this works OK
   secondw <- do.call("nls", list(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights = weights))
#  As does putting weights in the data dataframe (here not active)
#  data$weights <- weights # from John Fox
#  secondw<-nls(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights=weights)
   secondw
}

Afraid I avoid the wonders of non-standard evaluation, and this time it jumped up and bit me.
But then I remember what machine instruction 260000800009 did on an IBM 1620.

The swiftness of reply from John and Noah was much appreciated.

Best, JN



More information about the R-package-devel mailing list