Skip to content
Back to formatted view

Raw Message

Message-ID: <alpine.LFD.2.03.1301160921510.1915@uiowa.edu>
Date: 2013-01-16T15:27:57Z
From: Luke Tierney
Subject: Codetools Query (repost)
In-Reply-To: <CAJDot1oj5rTU+y1b_KHjGb6Up+B-3j3-6AZqVgfXox6i3Bvf_g@mail.gmail.com>

You really don't want to use internals like isBaseVar as there is no
guarantee they will continue to exist.  Even collectUsage and otehr
things mentioned on the same page may need to change if this is
reimplemented.

The most robust approach is to use findGlobals and omit what you don't
want, e.g.

     > omit <- ls("package:base", all.names=TRUE)
     > setdiff(findGlobals(moo),omit)
     [1] "y"

If you do want to use chollectUsage you could use something like

     funs <- new.env()
     omit <- ls("package:base", all.names=TRUE)
     enter <- function(type, v, e, w){
 	if (! v %in% omit)
 	    assign(v, TRUE, funs)
     }
     collectUsage(moo, enterGlobal = enter)

If %in% is too slow create a hashed envorinment and use exists().

Best,

luke

On Tue, 15 Jan 2013, Saptarshi Guha wrote:

> Sorry for reposting,  i keep forgetting this should be plain text.
> Will not make this mistake again
>
> Hello,
>
> The following code
>
> moo <- function(a=1){ x=1; x=x+y}
> funs <- new.env()
> enter <- function(type, v, e, w){
>  assign(v, TRUE, funs)
> }
> library(codetools)
> collectUsage(moo, enterGlobal = enter)
>
>
> adds + to the environment funs i.e.
>
> funs: "=" "{" "+" "y"
>
> How can i ignore variables which are present in base, utils, stat
> environments from being added (equivalently(?) symbols present in R
> when R is started)
>
> I tried
>
>
>
> funs <- new.env()
> enter <- function(type, v, e, w){
>  if(codetools:::isBaseVar(v, w$env) || codetools:::isStatsVar(v, w$env)
>     || codetools:::isUtilsVar(v, w$env) || v == "Quote")
>    return()
>  assign(v, TRUE, funs)
> }
>
> library(codetools)
> collectUsage(moo, enterGlobal = enter)
>
> but this threw
>
> Error in exists(v, envir = e, inherits = FALSE, mode = "function") :
>  invalid 'envir' argument
>
> Cheers
>
> ______________________________________________
> 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.
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu