From ripley at stats.ox.ac.uk  Wed Apr 12 08:28:35 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 12 Apr 2006 07:28:35 +0100 (BST)
Subject: [Rd] [R] type converters not being saved to workspace
In-Reply-To: <443C101A.4050507@r-project.org>
References: <200604090322.10067.joe@gnacademy.org>
	<Pine.LNX.4.64.0604090958440.5192@gannet.stats.ox.ac.uk>
	<443C101A.4050507@r-project.org>
Message-ID: <Pine.LNX.4.64.0604120657220.31187@gannet.stats.ox.ac.uk>

On Tue, 11 Apr 2006, John Chambers wrote:

> (diverted to r-devel)
>
> The problem seems to be rather that loading of the saved image takes place 
> _after_ the packages are attached.  So nothing that the methods package does 
> in its initialization will see the relevant type converters or other methods 
> objects.

I am sorry, but that is not the case: see ?Startup. The image is loaded 
_before_ the packages are attached (as documented).  You can check 
explicitly by

env R_DEFAULT_PACKAGES=NULL R --restore
# R starts and loads the workspace
> library(methods)

and it still does not work (as I had tried before answering).

> More generally, loading into .GlobalEnv will never cause the equivalent of 
> cacheMetaData(.GlobalEnv).
>
> It would be possible to hack the C code in main/saveload.c to generate a call 
> to cacheMetaData under the right circumstances, but is that the best 
> solution? 
> Intuitively, it's the objects containing the method/class definitions that 
> should trigger actions when they are loaded (emulating what happens when the 
> corresponding setMethod or setClass function call takes place).  I.e., a load 
> method for these classes of objects.  Possible?

Yes, but it will not help here as the objects are loaded without methods 
being loaded.

I hope one day when we have S4 objects marked in the object headers that 
loading such an object will load and attach the methods package.

> It might be better, now that we're past feature freeze, to leave this as a 
> "design infelicity" for the upcoming release.

Definitely.

I think that package methods needs to cache metadata in its .onAttach not 
.onLoad.  In principle it affects anything setting methods for generics in 
package:methods, but if this is done in a package, the dependency on 
methods will ensure that methods is loaded before the package, and then 
library() will run cacheMetaData on the package.


> Prof Brian Ripley wrote:
>
>> It is recommended that you use a package for this sort of thing.
>> 
>> When a package is loaded, the S4 methods it contains are merged into the 
>> metadata.  When the global environment is loaded, they are not.  Call 
>> 'cacheMetaData(1)' to do so.
>> 
>> [This looks like a bug: cacheMetaData is called on .GlobalEnv in the 
>> startup code for package methods, but seems not to work when called from 
>> there.  I think this is because package methods is not at that point on the 
>> search path (the call is from .onLoad) and hence the coerce() generic is 
>> not visible to cacheMetaData.  If you have S4 code in a package, it will 
>> ensure that package methods is attached before your package is loaded.]
>> 
>> On Sun, 9 Apr 2006, Joseph Wang wrote:
>> 
>>> Any one can explain why this happens or any work arounds?
>>> 
>>>> setClass('foo')
>>> 
>>> [1] "foo"
>>> 
>>>> setAs('foo', 'character', function(from) from)
>>>> showMethods('coerce')
>>> 
>>> 
>>> Function "coerce":
>>> from = "ANY", to = "array"
>>> from = "ANY", to = "call"
>>> from = "ANY", to = "character"
>>> from = "ANY", to = "complex"
>>> from = "ANY", to = "environment"
>>> from = "ANY", to = "expression"
>>> from = "ANY", to = "function"
>>> from = "ANY", to = "integer"
>>> from = "ANY", to = "list"
>>> from = "ANY", to = "logical"
>>> from = "ANY", to = "matrix"
>>> from = "ANY", to = "name"
>>> from = "ANY", to = "numeric"
>>> from = "ANY", to = "single"
>>> from = "ANY", to = "ts"
>>> from = "ANY", to = "vector"
>>> from = "ANY", to = "NULL"
>>> from = "foo", to = "character"
>>> 
>>>> q()
>>> 
>>> Save workspace image? [y/n/c]: y
>>> 
>>> 
>>> (restart)
>>> 
>>>> showMethods('coerce')
>>> 
>>> 
>>> Function "coerce":
>>> from = "ANY", to = "array"
>>> from = "ANY", to = "call"
>>> from = "ANY", to = "character"
>>> from = "ANY", to = "complex"
>>> from = "ANY", to = "environment"
>>> from = "ANY", to = "expression"
>>> from = "ANY", to = "function"
>>> from = "ANY", to = "integer"
>>> from = "ANY", to = "list"
>>> from = "ANY", to = "logical"
>>> from = "ANY", to = "matrix"
>>> from = "ANY", to = "name"
>>> from = "ANY", to = "numeric"
>>> from = "ANY", to = "single"
>>> from = "ANY", to = "ts"
>>> from = "ANY", to = "vector"
>>> from = "ANY", to = "NULL"
>>> from = "NULL", to = "OptionalFunction"
>>> 
>>>> 
>>> 
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide! 
>>> http://www.R-project.org/posting-guide.html
>>> 
>> 
>
>

-- 
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



[Rd] [R] type converters not being saved to workspace
Prof Brian Ripley