Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread peter dalgaard

On Apr 10, 2011, at 19:54 ,  wrote:

> On Sun, 10 Apr 2011, Hadley Wickham wrote:
> 
 Okay, I understand what you are trying to do.  Yes, you have fooled 
 codetools in this instance.
>>> 
>>> ...but notice that the codetools warning is just that: It _is_ acknowledged 
>>> that these things occasionally happen by design. There are a couple of 
>>> cases in base R too:
>>> 
>>> * checking R code for possible problems ... NOTE
>>> glm.fit: no visible binding for global variable ‘n’
>> 
>> Are you sure that's not a bug?  There's:
>> 
>> aic.model <- aic(y, n, mu, weights, dev) + 2 * rank
>> 
>> and n.ok is defined, but n isn't defined anywhere.
> 
> It is (or should be) defined by the call to
> 
>eval(family$initialize)
> 

...iff actually used by family$aic. And, it is a different n from n.ok (a 
vector, the per-element size parameter of the binomial)



>> 
>>> quantile.ecdf: no visible binding for global variable ‘y’
>> 
>> I wonder why it warns on y, but not nobs.
> 
> It does when run on stats:::quantile.ecdf directly:
> 
>> codetools::checkUsage(stats:::quantile.ecdf)
> : no visible binding for global variable ‘nobs’
> : no visible binding for global variable ‘y’
> 
> Maybe in the context where you saw this nobs is defined in an
> enclosing environment.
> 

It came from make check-devel, so I suspect that it picks up stats:::nobs() 
(which would be horribly wrong, but, well...)


> luke
> 
>> 
>> Hadley

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread luke-tierney

On Sun, 10 Apr 2011, Hadley Wickham wrote:


Okay, I understand what you are trying to do.  Yes, you have fooled codetools 
in this instance.


...but notice that the codetools warning is just that: It _is_ acknowledged 
that these things occasionally happen by design. There are a couple of cases in 
base R too:

* checking R code for possible problems ... NOTE
glm.fit: no visible binding for global variable ‘n’


Are you sure that's not a bug?  There's:

aic.model <- aic(y, n, mu, weights, dev) + 2 * rank

and n.ok is defined, but n isn't defined anywhere.


It is (or should be) defined by the call to

eval(family$initialize)




quantile.ecdf: no visible binding for global variable ‘y’


I wonder why it warns on y, but not nobs.


It does when run on stats:::quantile.ecdf directly:


codetools::checkUsage(stats:::quantile.ecdf)

: no visible binding for global variable ‘nobs’
: no visible binding for global variable ‘y’

Maybe in the context where you saw this nobs is defined in an
enclosing environment.

luke



Hadley





--
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
   Actuarial Science
241 Schaeffer Hall  email:  l...@stat.uiowa.edu
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread Hadley Wickham
>> Okay, I understand what you are trying to do.  Yes, you have fooled 
>> codetools in this instance.
>
> ...but notice that the codetools warning is just that: It _is_ acknowledged 
> that these things occasionally happen by design. There are a couple of cases 
> in base R too:
>
> * checking R code for possible problems ... NOTE
> glm.fit: no visible binding for global variable ‘n’

Are you sure that's not a bug?  There's:

aic.model <- aic(y, n, mu, weights, dev) + 2 * rank

and n.ok is defined, but n isn't defined anywhere.

> quantile.ecdf: no visible binding for global variable ‘y’

I wonder why it warns on y, but not nobs.

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread peter dalgaard

On Apr 10, 2011, at 15:10 , Duncan Murdoch wrote:

> On 11-04-09 9:22 PM, Spencer Graves wrote:
>> On 4/9/2011 6:12 PM, Duncan Murdoch wrote:
>>> On 11-04-09 7:02 PM, Spencer Graves wrote:

 
 "R CMD check" will give this message sometimes when I don't feel
 it's appropriate.  For example, I define a data object ETB in a package,
 then give that as the default in a function call like
 f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
 check", I get "no visible binding for global variable 'ETB'", even
 though the function is tested and works during R CMD check.
>>> 
>>> What is ETB?  Your code is looking for a global variable by that name,
>>> and that's what codetools is telling you.
>> 
>> Duncan:  Thanks for the question.
>> 
>> 
>> ETB is a data object in my package.  codetools can't find it because
>> data(ETB) is needed before ETB becomes available.  codetools is not
>> smart enough to check to see if ETB is a data object in the package.
> 
> Okay, I understand what you are trying to do.  Yes, you have fooled codetools 
> in this instance.

...but notice that the codetools warning is just that: It _is_ acknowledged 
that these things occasionally happen by design. There are a couple of cases in 
base R too:

* checking R code for possible problems ... NOTE
glm.fit: no visible binding for global variable ‘n’
quantile.ecdf: no visible binding for global variable ‘y’

I can't seem to spot the 'n' just now, though...

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread Spencer Graves

On 4/10/2011 6:10 AM, Duncan Murdoch wrote:

On 11-04-09 9:22 PM, Spencer Graves wrote:

On 4/9/2011 6:12 PM, Duncan Murdoch wrote:

On 11-04-09 7:02 PM, Spencer Graves wrote:

On 4/9/2011 2:31 PM, Hadley Wickham wrote:

On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson
wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block 
anything

when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around 
here

and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. 
Know

what I mean?

dat<- whatever

someNewFunction<- function(z, w){
 #do something with z and w and create a new "dat"
 # but forget to name it "dat"
  lm (y, x, data=dat)
 # lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to 
fill

in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


 Is this run by "R CMD check"?  I've seen this message.


 "R CMD check" will give this message sometimes when I 
don't feel
it's appropriate.  For example, I define a data object ETB in a 
package,

then give that as the default in a function call like
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
check", I get "no visible binding for global variable 'ETB'", even
though the function is tested and works during R CMD check.


What is ETB?  Your code is looking for a global variable by that name,
and that's what codetools is telling you.


Duncan:  Thanks for the question.


ETB is a data object in my package.  codetools can't find it because
data(ETB) is needed before ETB becomes available.  codetools is not
smart enough to check to see if ETB is a data object in the package.


Okay, I understand what you are trying to do.  Yes, you have fooled 
codetools in this instance.



  I'm sorry:  I did not intend to fool codetools.  ;-)


  I just wanted to provide sensible defaults in a way that seemed 
obvious to me.



  Thanks again for all your work on Rtools and the R project more 
generally.  Spencer




Duncan Murdoch




--
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-10 Thread Duncan Murdoch

On 11-04-09 9:22 PM, Spencer Graves wrote:

On 4/9/2011 6:12 PM, Duncan Murdoch wrote:

On 11-04-09 7:02 PM, Spencer Graves wrote:

On 4/9/2011 2:31 PM, Hadley Wickham wrote:

On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson
wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
 #do something with z and w and create a new "dat"
 # but forget to name it "dat"
  lm (y, x, data=dat)
 # lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


 Is this run by "R CMD check"?  I've seen this message.


 "R CMD check" will give this message sometimes when I don't feel
it's appropriate.  For example, I define a data object ETB in a package,
then give that as the default in a function call like
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
check", I get "no visible binding for global variable 'ETB'", even
though the function is tested and works during R CMD check.


What is ETB?  Your code is looking for a global variable by that name,
and that's what codetools is telling you.


Duncan:  Thanks for the question.


ETB is a data object in my package.  codetools can't find it because
data(ETB) is needed before ETB becomes available.  codetools is not
smart enough to check to see if ETB is a data object in the package.


Okay, I understand what you are trying to do.  Yes, you have fooled 
codetools in this instance.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Spencer Graves

On 4/9/2011 6:12 PM, Duncan Murdoch wrote:

On 11-04-09 7:02 PM, Spencer Graves wrote:

On 4/9/2011 2:31 PM, Hadley Wickham wrote:

On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson
wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
#do something with z and w and create a new "dat"
# but forget to name it "dat"
 lm (y, x, data=dat)
# lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


Is this run by "R CMD check"?  I've seen this message.


"R CMD check" will give this message sometimes when I don't feel
it's appropriate.  For example, I define a data object ETB in a package,
then give that as the default in a function call like
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
check", I get "no visible binding for global variable 'ETB'", even
though the function is tested and works during R CMD check.


What is ETB?  Your code is looking for a global variable by that name,
and that's what codetools is telling you.


Duncan:  Thanks for the question.


ETB is a data object in my package.  codetools can't find it because 
data(ETB) is needed before ETB becomes available.  codetools is not 
smart enough to check to see if ETB is a data object in the package.



Spencer



Duncan Murdoch



--
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Spencer Graves

On 4/9/2011 6:12 PM, Duncan Murdoch wrote:

On 11-04-09 7:02 PM, Spencer Graves wrote:

On 4/9/2011 2:31 PM, Hadley Wickham wrote:
On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson   
wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
#do something with z and w and create a new "dat"
# but forget to name it "dat"
 lm (y, x, data=dat)
# lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


Is this run by "R CMD check"?  I've seen this message.


"R CMD check" will give this message sometimes when I don't feel
it's appropriate.  For example, I define a data object ETB in a package,
then give that as the default in a function call like
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
check", I get "no visible binding for global variable 'ETB'", even
though the function is tested and works during R CMD check.


What is ETB?  Your code is looking for a global variable by that name, 
and that's what codetools is telling you.


Duncan:  Thanks for the question.


ETB is a data object in my package.  codetools can't find it because 
data(ETB) is needed before ETB becomes available.  codetools is not 
smart enough to check to see if ETB is a data object in the package.



Spencer



Duncan Murdoch



--
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Duncan Murdoch

On 11-04-09 7:02 PM, Spencer Graves wrote:

On 4/9/2011 2:31 PM, Hadley Wickham wrote:

On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson   wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
#do something with z and w and create a new "dat"
# but forget to name it "dat"
 lm (y, x, data=dat)
# lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


Is this run by "R CMD check"?  I've seen this message.


"R CMD check" will give this message sometimes when I don't feel
it's appropriate.  For example, I define a data object ETB in a package,
then give that as the default in a function call like
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
check", I get "no visible binding for global variable 'ETB'", even
though the function is tested and works during R CMD check.


What is ETB?  Your code is looking for a global variable by that name, 
and that's what codetools is telling you.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Spencer Graves

On 4/9/2011 2:31 PM, Hadley Wickham wrote:

On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson  wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
   #do something with z and w and create a new "dat"
   # but forget to name it "dat"
lm (y, x, data=dat)
   # lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



library(codetools)
checkUsage(someNewFunction)

: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)


  Is this run by "R CMD check"?  I've seen this message.


  "R CMD check" will give this message sometimes when I don't feel 
it's appropriate.  For example, I define a data object ETB in a package, 
then give that as the default in a function call like 
f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD 
check", I get "no visible binding for global variable 'ETB'", even 
though the function is tested and works during R CMD check.



  Spencer


Hadley




--
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Hadley Wickham
On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson  wrote:
> Years ago, I did lots of Perl programming. Perl will let you be lazy
> and write functions that refer to undefined variables (like R does),
> but there is also a strict mode so the interpreter will block anything
> when a variable is mentioned that has not been defined. I wish there
> were a strict mode for checking R functions.
>
> Here's why. We have a lot of students writing R functions around here
> and they run into trouble because they use the same name for things
> inside and outside of functions. When they call functions that have
> mistaken or undefined references to names that they use elsewhere,
> then variables that are in the environment are accidentally used. Know
> what I mean?
>
> dat <- whatever
>
> someNewFunction <- function(z, w){
>   #do something with z and w and create a new "dat"
>   # but forget to name it "dat"
>    lm (y, x, data=dat)
>   # lm just used wrong data
> }
>
> I wish R had a strict mode to return an error in that case. Users
> don't realize they are getting nonsense because R finds things to fill
> in for their mistakes.
>
> Is this possible?  Does anybody agree it would be good?


> library(codetools)
> checkUsage(someNewFunction)
: no visible binding for global variable ‘y’
: no visible binding for global variable ‘x’
: no visible binding for global variable ‘dat’

Which also picks up another bug in your function ;)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wish there were a "strict mode" for R interpreter. What about You?

2011-04-09 Thread Duncan Murdoch

On 11-04-09 3:51 PM, Paul Johnson wrote:

Years ago, I did lots of Perl programming. Perl will let you be lazy
and write functions that refer to undefined variables (like R does),
but there is also a strict mode so the interpreter will block anything
when a variable is mentioned that has not been defined. I wish there
were a strict mode for checking R functions.

Here's why. We have a lot of students writing R functions around here
and they run into trouble because they use the same name for things
inside and outside of functions. When they call functions that have
mistaken or undefined references to names that they use elsewhere,
then variables that are in the environment are accidentally used. Know
what I mean?

dat<- whatever

someNewFunction<- function(z, w){
#do something with z and w and create a new "dat"
# but forget to name it "dat"
 lm (y, x, data=dat)
# lm just used wrong data
}

I wish R had a strict mode to return an error in that case. Users
don't realize they are getting nonsense because R finds things to fill
in for their mistakes.

Is this possible?  Does anybody agree it would be good?



It would be really bad, unless done carefully.

In your function the free (undefined) variables are dat and lm.  You 
want to be warned about dat, but you don't want to be warned about lm. 
What rule should R use to determine that?


(One possible rule would work in a package with a namespace.  In that 
case, all variables must be found in declared dependencies, the search 
could stop before it got to globalenv().  But it seems unlikely that 
your students are writing packages with namespaces.)


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel