Hi everyone thanks for the replies. The issue was NOT a font problem; I deliberately chose ll1 and l11 as examples of easily confused variable names (evidently these were too easily confused ;-). The code snippet was written as intended, and increment() contained a deliberate, highlighted, bug. I was asking for guidance on avoiding/finding this sort of coding error. That was why I wrote "#bug here" in the original code, and why the function was called increment()---because the function should have incremented x by adding a variable whose value was 1 (of course, the function as written, contrary to the desired functionality of increment(), added a variable whose value was 2). I guess I wasn't explicit enough here. Sorry. The fundamental problem was, how to tell that a variable being used in a function is not local? One answer (thanks Patrick!): conflicts() shows masked objects on the search path, which is not quite what I need: I want some way to list all non-local variables that increment() uses in its body. [The original variable names referred to genetic bandsharing data for possums, eg coates.female.pouchyoung.allbands.method5 and huapai.young.male.sibling.relatedness.method3 and huapai.old.female.nonsibling.relatedness.justdarkbands.method1 ad nauseum...hence the need for shorter example variable names!] ll1 <- 2 #sic increment <- function(x) { l11 <- 1 #sic return(x+ll1) #sic; deliberate bug here (sic) } -- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
Robin Hankin wrote:> > Hi everyone > > thanks for the replies. > > The issue was NOT a font problem; I deliberately chose ll1 and l11 as > examples of easily confused variable names (evidently these were too > easily confused ;-). The code snippet was written as intended, and > increment() contained a deliberate, highlighted, bug. I was asking > for guidance on avoiding/finding this sort of coding error. > > That was why I wrote "#bug here" in the original code, and why the > function was called increment()---because the function should have > incremented x by adding a variable whose value was 1 (of course, the > function as written, contrary to the desired functionality of > increment(), added a variable whose value was 2). I guess I wasn't > explicit enough here. Sorry. > > The fundamental problem was, how to tell that a variable being used in > a function is not local? > > One answer (thanks Patrick!): conflicts() shows masked objects on the > search path, which is not quite what I need: I want some way to list > all non-local variables that increment() uses in its body. > > [The original variable names referred to genetic bandsharing data for > possums, eg > > coates.female.pouchyoung.allbands.method5 > and > huapai.young.male.sibling.relatedness.method3 > and > huapai.old.female.nonsibling.relatedness.justdarkbands.method1 > > ad nauseum...hence the need for shorter example variable names!] > > ll1 <- 2 #sic > increment <- function(x) > { > l11 <- 1 #sic > return(x+ll1) #sic; deliberate bug here (sic) > }I think you are looking for ls() together with the debugging tool browser(): increment <- function(x){ l11 <- 1 browser() # just for debugging return(x+ll1) } increment(1) # Now the browser opens and you can look for objects in the current enviroment with ls(). Uwe Ligges> -- > > Robin Hankin, Lecturer, > School of Geography and Environmental Science > Tamaki Campus > Private Bag 92019 Auckland > New Zealand > > r.hankin at auckland.ac.nz > tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Tue, 18 Mar 2003, Robin Hankin wrote:> Hi everyone > > thanks for the replies. > > The issue was NOT a font problem; I deliberately chose ll1 and l11 as > examples of easily confused variable names (evidently these were too > easily confused ;-). The code snippet was written as intended, and > increment() contained a deliberate, highlighted, bug. I was asking > for guidance on avoiding/finding this sort of coding error. > > That was why I wrote "#bug here" in the original code, and why the > function was called increment()---because the function should have > incremented x by adding a variable whose value was 1 (of course, the > function as written, contrary to the desired functionality of > increment(), added a variable whose value was 2). I guess I wasn't > explicit enough here. Sorry. > > The fundamental problem was, how to tell that a variable being used in > a function is not local? > > One answer (thanks Patrick!): conflicts() shows masked objects on the > search path, which is not quite what I need: I want some way to list > all non-local variables that increment() uses in its body. > > > > [The original variable names referred to genetic bandsharing data for > possums, eg > > coates.female.pouchyoung.allbands.method5 > and > huapai.young.male.sibling.relatedness.method3 > and > huapai.old.female.nonsibling.relatedness.justdarkbands.method1 > > ad nauseum...hence the need for shorter example variable names!] > > > > > ll1 <- 2 #sic > increment <- function(x) > { > l11 <- 1 #sic > return(x+ll1) #sic; deliberate bug here (sic) > } >I realise this won't help now, but I am currently working on some code analysis tools for R that will hopefully be available by the end of summer. These will include facilities for determining what global variables are references in a piece of code; the current verion of would does the following on this example:> findGlobals(increment)[1] "{" "<-" "return" "+" "ll1" This set of tools will be integrated with name space mechanism that will be available in 1.7.0. and will allow packages to be checked for undefined functions and variables. I suspect these tools will become part of the tools package. luke -- Luke Tierney University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke at stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu