Skip to content

clean programming

4 messages · Gabor Grothendieck, Duncan Murdoch, Christophe Genolini

#
Hello the list,

I am trying to write a "cleanProgramming" function to test the 
procedure I use. For example, I want to be sure that I am not using 
globals variables. The function "findGlobals" detect that.

To list the globals used in function "fun", the syntax is : 
"findGlobals(fun,FALSE)$variable"

My problem is that I want to use it in a function, something like :

cleanProg <- function(name){
  if(length(findGlobals(name,FALSE)$variable>0){
    cat("Warnings: there is globals is function ",name,"\a\n")
  }
}

But findGlobals take a function as first argument, not a variable 
containing a function name.

Anyway to solve that?

Christophe


----------------------------------------------------------------
Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre
#
Its a FAQ

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
On Dec 16, 2007 9:25 AM, <cgenolin at u-paris10.fr> wrote:
#
On 16/12/2007 9:25 AM, cgenolin at u-paris10.fr wrote:
Use get() to find an object with a given name.  You need to be careful 
to specify where it should look (the envir argument); typically 
parent.frame() is appropriate, but your cleanProg function should allow 
the user to override this.

Duncan Murdoch
1 day later
#
Gabor Grothendieck <ggrothendieck at gmail.com> a ??crit? :
Oups... Sorry for that.
Just to close the topic :

cleanProg <- function(name,tolerance){
if(length(findGlobals(get(name),FALSE)$variables) > tolerance){
   cat("More than",tolerance,"global variable(s) in ",name,"\a\n")
}
}
cleanProg(fun,0)

----------------------------------------------------------------
Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre