Skip to content
Prev 8326 / 12125 Next

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

Dear John,

Please see below interline:
On 2022-08-13 1:15 p.m., J C Nash wrote:
If I understand correctly, there are two contexts here: (1) the 
interface for your wrapnlsr() function, and (2) how nlsr() calls other 
functions, including nls().

The first should probably work like a standard statistical modelling 
function, including how the weights argument is evaluated. The second is 
hidden from the user, and is where the scoping issue that you reported 
arose. My suggestion to follow a standard statistical modelling function 
here is probably off-base, and the previous two solutions, one from me 
and one from Noah, both work.

With respect to my solution: if you actually adopt it, I'd prefer not to 
use a name like data$weights, for fear of overwriting an existing 
variable in data called weights, but rather some like .weights or even 
..weights.. .
I'm probably the wrong person for this -- I'm familiar with nonlinear 
regression, but your knowledge of the computational details vastly 
exceeds mine.
What I meant by "evaluate" was associate a value with the symbol 
weights. The scoping issue was that nls() ended up looking in the global 
environment, where it found the weights() function rather than the local 
variable (i.e., argument) weights in your function.

Best,
  John