From Torsten.Hothorn at rzmail.uni-erlangen.de  Wed Feb 15 10:16:18 2006
From: Torsten.Hothorn at rzmail.uni-erlangen.de (Torsten Hothorn)
Date: Wed, 15 Feb 2006 10:16:18 +0100 (CET)
Subject: [Rd] S3 generics without NS and cleanEx()
Message-ID: <Pine.LNX.4.64.0602150954340.20143@artemis.imbe.med.uni-erlangen.de>


Good morning,

we recently observed a problem with importing S3 generics from a foreign 
package (without namespace), defining a S3 method in a package _with_ 
namespace and the `cleanEx()' function which is automatically generated 
and executed before examples are run by R CMD check.

To be more precise. Package `strucchange' defines a S3 generic

     sctest <- function(x, ...) UseMethod("sctest")

and the development version of `party' defines

     sctest.mob <- function(x, node = NULL, ...) {
         ...

which is exported in party's namespace

     S3method(sctest, mob)

Creating a `mob' object and running `sctest' on it works fine.

However, when we attach `party' and run `cleanEx()', the `sctest' method 
for `mob' is no longer found. Before `cleanEx()' we get

R> methods(sctest)
[1] sctest.Fstats  sctest.efp     sctest.formula sctest.gefp 
sctest.mob*

and afterwards

R> methods(sctest)
[1] sctest.Fstats  sctest.efp     sctest.formula sctest.gefp

is missing.

Debugging `cleanEx()' shows that those objects are removed from the 
global environment:

ls(envir = env, all.names = TRUE)
[1] ".Random.seed"         ".__S3MethodsTable__." "GCtorture"
[4] "a"                    "euro"

where the second one looks very suspicious.

`party_0.4-0.tar.gz' is available from

     http://www.imbe.med.uni-erlangen.de/~hothorn/party_0.4-0.tar.gz

and

     http://www.imbe.med.uni-erlangen.de/~hothorn/modeltools_0.2-3.tar.gz

is required.

The example below can be reproduced with both R-2.2.1 and R-2.3.0.

Best wishes,

Torsten



R> invisible(options(echo = TRUE))
R> ### * <HEADER>
R> ###
R> attach(NULL, name = "CheckExEnv")
R> assign(".CheckExEnv", as.environment(2), pos = length(search())) # base
R> ## add some hooks to label plot pages for base and grid graphics
R> setHook("plot.new", ".newplot.hook")
R> setHook("persp", ".newplot.hook")
R> setHook("grid.newpage", ".gridplot.hook")
R> 
R> assign("cleanEx",
+        function(env = .GlobalEnv) {
+ 	   rm(list = ls(envir = env, all.names = TRUE), envir = env)
+            RNGkind("default", "default")
+ 	   set.seed(1)
+    	   options(warn = 1)
+ 	   delayedAssign("T", stop("T used instead of TRUE"),
+ 		  assign.env = .CheckExEnv)
+ 	   delayedAssign("F", stop("F used instead of FALSE"),
+ 		  assign.env = .CheckExEnv)
+ 	   sch <- search()
+ 	   newitems <- sch[! sch %in% .oldSearch]
+ 	   for(item in rev(newitems))
+                eval(substitute(detach(item), list(item=item)))
+ 	   missitems <- .oldSearch[! .oldSearch %in% sch]
+ 	   if(length(missitems))
+ 	       warning("items ", paste(missitems, collapse=", "),
+ 		       " have been removed from the search path")
+        },
+        env = .CheckExEnv)
R> assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now
R> assign("ptime", proc.time(), env = .CheckExEnv)
R> grDevices::postscript("party-Ex.ps")
R> assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv)
R> options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly"))
R> options(warn = 1) 
R> library('party')
Loading required package: survival
Loading required package: splines
Loading required package: grid
Loading required package: modeltools
Loading required package: coin
Loading required package: mvtnorm
Loading required package: zoo
Loading required package: sandwich
Loading required package: strucchange
R> 
R> methods(sctest)
[1] sctest.Fstats  sctest.efp     sctest.formula sctest.gefp    sctest.mob*

    Non-visible functions are asterisked
R> 
R> assign(".oldSearch", search(), env = .CheckExEnv)
R> assign(".oldNS", loadedNamespaces(), env = .CheckExEnv)
R> cleanEx(); ..nameEx <- "BinaryTree-class"
R> 
R> methods(sctest)
[1] sctest.Fstats  sctest.efp     sctest.formula sctest.gefp 
R> 
R> 
R> proc.time()
[1] 1.77 0.01 1.97 0.00 0.00
R>



[Rd] S3 generics without NS and cleanEx()
Torsten Hothorn