[Rd] S3 generics without NS and cleanEx()

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 15 10:41:30 CET 2006


Follow up:

You said

>> which is exported in party's namespace
>>
>>     S3method(sctest, mob)

but that's not true: it is registered not exported.  You need to export it 
as strucchange does not have a namespace.


On Wed, 15 Feb 2006, Prof Brian Ripley wrote:

> Torsten,
>
> The error is that .__S3MethodsTable__. ends up in the global environment.
> Are you importing sctest (I can't see it)?  I think that's the problem.
>
>> From R-exts:
>
> The standard method for S3-style @code{UseMethod} dispatching might fail
> to locate methods defined in a package that is imported but not attached
> to the search path.  To ensure that these methods are available the
> packages defining the methods should ensure that the generics are
> imported and register the methods using @code{S3method} directives.
> ^^^^^^^^
>
> Brian
>
> On Wed, 15 Feb 2006, Torsten Hothorn wrote:
>
>>
>> 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>
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list