Warning when trying to access a variable out of scope?

View: New views
4 Messages — Rating Filter:   Alert me  

Warning when trying to access a variable out of scope?

by Steve Lianoglou-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I was wondering if I could get R to warn me, or give me a rude  
awakening somehow, if I'm accessing a variable that is out of my  
function's scope.

For example, often times I'm creating a function as I'm testing it in  
the REPL, copying and pasting between both.

As a simple example, I might end up with a function like:

f <- function(a, b) {
   a + b.test
}

Where b.test was defined in my workspace as I'm mucking about in the  
REPL, but "clearly" I should have written:

f <- function(a,b) {
   a + b
}

I could go on for a while in my session w/o noticing the problem  
(since b.test is in my global env), and unbeknownst to me, my function  
will keep accessing the "b.test" variable when I really want it to  
work on the "b" var that I'm passing in to it.

Is there some setting or someway I can get R to warn me that "b.test"  
is being accessed outside the scope of my function?

Thanks,
-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: Warning when trying to access a variable out of scope?

by Duncan Murdoch-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 7/2/2009 2:17 PM, Steve Lianoglou wrote:

> Hi,
>
> I was wondering if I could get R to warn me, or give me a rude  
> awakening somehow, if I'm accessing a variable that is out of my  
> function's scope.
>
> For example, often times I'm creating a function as I'm testing it in  
> the REPL, copying and pasting between both.
>
> As a simple example, I might end up with a function like:
>
> f <- function(a, b) {
>    a + b.test
> }
>
> Where b.test was defined in my workspace as I'm mucking about in the  
> REPL, but "clearly" I should have written:
>
> f <- function(a,b) {
>    a + b
> }
>
> I could go on for a while in my session w/o noticing the problem  
> (since b.test is in my global env), and unbeknownst to me, my function  
> will keep accessing the "b.test" variable when I really want it to  
> work on the "b" var that I'm passing in to it.
>
> Is there some setting or someway I can get R to warn me that "b.test"  
> is being accessed outside the scope of my function?

Not really, but the codetools package can analyze your functions and
tell you which ones do things like that.  For example,

 > f <- function(a, b) {
+    a + b.test
+ }
 > checkUsage(f)
<anonymous>: no visible binding for global variable ‘b.test’

or

 > checkUsageEnv(globalenv())
f: no visible binding for global variable ‘b.test’

This is normally used in the package test code, but you can use it on
individual functions or environments if you want.

Duncan Murdoch

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: Warning when trying to access a variable out of scope?

by Greg Snow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Does this do what you want?

> b.test <- 3
>
> f <- function(a,b) {
+ a+b.test
+ }
>
> f(10,20)
[1] 13
>
> environment(f) <- baseenv()
>
> f(10,20)
Error in f(10, 20) : object 'b.test' not found
>
>


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow@...
801.408.8111


> -----Original Message-----
> From: r-help-bounces@... [mailto:r-help-bounces@r-
> project.org] On Behalf Of Steve Lianoglou
> Sent: Thursday, July 02, 2009 12:17 PM
> To: R-help@...
> Subject: [R] Warning when trying to access a variable out of scope?
>
> Hi,
>
> I was wondering if I could get R to warn me, or give me a rude
> awakening somehow, if I'm accessing a variable that is out of my
> function's scope.
>
> For example, often times I'm creating a function as I'm testing it in
> the REPL, copying and pasting between both.
>
> As a simple example, I might end up with a function like:
>
> f <- function(a, b) {
>    a + b.test
> }
>
> Where b.test was defined in my workspace as I'm mucking about in the
> REPL, but "clearly" I should have written:
>
> f <- function(a,b) {
>    a + b
> }
>
> I could go on for a while in my session w/o noticing the problem
> (since b.test is in my global env), and unbeknownst to me, my function
> will keep accessing the "b.test" variable when I really want it to
> work on the "b" var that I'm passing in to it.
>
> Is there some setting or someway I can get R to warn me that "b.test"
> is being accessed outside the scope of my function?
>
> Thanks,
> -steve
>
> --
> Steve Lianoglou
> Graduate Student: Physiology, Biophysics and Systems Biology
> Weill Medical College of Cornell University
>
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
> ______________________________________________
> R-help@... mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: Warning when trying to access a variable out of scope?

by Duncan Murdoch-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 03/07/2009 3:28 PM, Greg Snow wrote:

> Does this do what you want?
>
>> b.test <- 3
>>
>> f <- function(a,b) {
> + a+b.test
> + }
>> f(10,20)
> [1] 13
>> environment(f) <- baseenv()
>>
>> f(10,20)
> Error in f(10, 20) : object 'b.test' not found

Probably not:  it messes up cases where f needs to call functions in the
global environment, or in stats, or some other package.  It might be
better to say

environment(f) <- parent.env(globalenv())

but that has problems too.

The question really isn't solvable in R.  Since functions and variables
aren't very different, and since any non-trivial function needs to make
use of functions outside itself, just about every function makes use of
globals.  Usually that's fine, they're globals like "mean". Sometimes
it's not fine, they're globals like "b.test", which aren't meant to be
seen.

codetools does a pretty good job of detecting these sorts of errors, but
there really isn't a foolproof test.

Duncan Murdoch

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.