[R-SIG-Mac] Problems with definitions of S4-generics

J ö rg Beyer Beyerj at students.uni-marburg.de
Thu Dec 21 20:37:19 CET 2006


Hello all, 

I installed R 2.4.1 yesterday (coming from 2.2.1), and made an interesting
observation today, which may be a simple coincidence, or related to R 2.4.1.

Platform: 
Mac G4/400 PCI (Oct. 1999, PPC-architecture)
Mac OS 10.4.8
R 2.4.1 for Mac OS X (CRAN binary, 2006-12-19) w/ R.app 1.18


Problem-intro:
The file "/Volumes/RAMdisk/beyerjoerg/Scripting R/source-0.R" contains code
that constructs and registers a S4-style generic function, *but only if the
function does not exist, to avoid unnecessary computations.*
This is the stub to test whether or not a S4-Generic is present:
  if ( ! isGeneric( <nameOfS4generic> )) {
    <setGeneric ...>
  } 

The code itself has worked without problems with R 2.2.1 -- the code is not
the problem. 
Okay, let's start. The function in this example is "cleanDataset"; to
demonstrate the problem, I'm using a second S4-generic here, it is named
"removeFactors". 


#### Fist the correct part
## Source the code and build the S4-generic
> source( "/Volumes/RAMdisk/beyerjoerg/Scripting R/source-0.R" )


## Do some tests to see if the generic is present,
## and have a look at some details
> isGeneric( "cleanDataset" )
[1] TRUE


> findFunction( "cleanDataset" )
[[1]]
<environment: R_GlobalEnv>


> cleanDataset
nonstandardGenericFunction for "cleanDataset" defined from package
".GlobalEnv"

function (object)
{
    standardGeneric("cleanDataset")
}
<environment: 0x293bbb8>
Methods may be defined for arguments: object


> getGeneric( "cleanDataset" )
## same result as above



#### Now what I suspect is a bug
## Delete the S4-generic (and all its methods, if any)
## and repeat the "forensic" test calls
> removeGeneric( "cleanDataset" )
[1] TRUE


# The following is as expected...
## [I'm trying here to re-translate the message from German back
## to English, so please don't be irritated if there are any differences]
> cleanDataset
Error: object "cleanDataset" not found


# Oops, but what's that?
> getGeneric( "cleanDataset" )
nonstandardGenericFunction for "cleanDataset" defined from package
".GlobalEnv"

function (object) 
{
    standardGeneric("cleanDataset")
}
<environment: 0x19be228>
Methods may be defined for arguments: object


## Why not try to remove it a second time?
> removeGeneric( "cleanDataset" )
[1] FALSE
Warning message:
generic function "cleanDataset" not found for removal in:
removeGeneric("cleanDataset")


# Another test to see if there is a function definition somewhere
# Weird ... 
> isGeneric( "cleanDataset" )
[1] TRUE


# Okay, let's see, where it is
# Ah, registered, but nowhere ...
> findFunction( "cleanDatasets" )
list()


# Just to be sure, test another generic (which I know is *present
# and working*)   Okay, as expected...
> findFunction( "removeFactors" )
[[1]]
<environment: R_GlobalEnv>




#### Consequence: 
# Because my code first tests whether a generic is present or not
# (see above), re-sourcing the definition code leads to nothing
> source( "/Volumes/RAMdisk/beyerjoerg/Scripting R/source-0.R" )
> cleanDataset
Error: object "cleanDataset" not found



When I skip testing the existence of a generic, everything works as
expected, of course.
Deleting and redefining generic functions this way never caused problems
under R 2.2.1 (I'm developing with this code since six months, the helper
functions have been called over and over again), but I don't have explicit
results for the above tests and R 2.2.1.

Running the above test case in R.app or from the terminal makes no
difference. At the moment, I can't see a reason why R should do what it
obviously does here, but I may have misunderstood something essential.

Some comments may be helpful.
Thanks for your interest.

Cheers

Jörg



More information about the R-SIG-Mac mailing list