Steve Lianoglou
2009-Jul-02 18:17 UTC
[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
Duncan Murdoch
2009-Jul-02 20:30 UTC
[R] Warning when trying to access a variable out of scope?
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
Greg Snow
2009-Jul-03 19:28 UTC
[R] Warning when trying to access a variable out of scope?
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 at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Steve Lianoglou > Sent: Thursday, July 02, 2009 12:17 PM > To: R-help at r-project.org > 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 at r-project.org 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.