[Rd] S3 generics without NS and cleanEx()

Torsten Hothorn Torsten.Hothorn at rzmail.uni-erlangen.de
Wed Feb 15 10:16:18 CET 2006

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 

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




is required.

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

Best wishes,


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> 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> methods(sctest)
[1] sctest.Fstats  sctest.efp     sctest.formula sctest.gefp    sctest.mob*

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

More information about the R-devel mailing list