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

John Fox j|ox @end|ng |rom mcm@@ter@c@
Sat Aug 13 18:58:44 CEST 2022


Dear John,

After further thought, it's probably a better idea to evaluate the 
weights argument in the same environment as the formula rather than to 
bypass nonstandard evaluation. You could use lm() as a guide. I don't 
entirely understand what you're trying to do so maybe this suggestion is 
off-base.

Best,
  John

On 2022-08-13 9:41 a.m., J C Nash wrote:
> 
> 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
-- 
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/



More information about the R-package-devel mailing list