Skip to content
Prev 47549 / 63424 Next

package NAMESPACE question

You can more or less get what you want by reassigning the environment
of the formula to be a child of its original environment, where you put
your private functions in the new environment.  For example, do not
export trt and make the following change to your R code:   

% diff -u foo/R/cmt.R~ foo/R/cmt.R
--- foo/R/cmt.R~        2014-01-28 09:10:58.272711000 -0800
+++ foo/R/cmt.R 2014-01-28 09:09:06.299398000 -0800
@@ -1,9 +1,12 @@
-trt <- function(x) x
+trt <- function(x) { cat("Calling foo:::trt\n"); x }

 cmt <- function(formula, data, subset, na.action = na.pass)  {

   if (!inherits(formula, "formula"))
     stop("Method is only for formula objects.")
+  intercalatedEnvir <- new.env(parent=environment(formula))
+  intercalatedEnvir$trt <- trt
+  environment(formula) <- intercalatedEnvir
   mf <- match.call(expand.dots = FALSE)
   args <- match(c("formula", "data", "subset", "na.action"),
                 names(mf), 0)

Bill Dunlap
TIBCO Software
wdunlap tibco.com