From mtmorgan at fhcrc.org  Sun May 23 00:24:10 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Sat, 22 May 2010 15:24:10 -0700
Subject: [Rd] S4 method defined but not used
In-Reply-To: <0B75A6276180B546A1CA8D2B46E7580503362D5194@ICEXM3.ic.ac.uk>
References: <0B75A6276180B546A1CA8D2B46E7580503362D5194@ICEXM3.ic.ac.uk>
Message-ID: <4BF8598A.6030707@fhcrc.org>

Hi Thibaut --

On 05/21/2010 08:34 AM, Jombart, Thibaut wrote:
> Dear R developers, 
> 
> I am having a slightly weird issue with a S4 method defined in my package adegenet 1.2-4, with R 2.11.0. As far as I know, the problem is new, and the code implementing the method has not changed for more than a year and worked well so far.
> 
> The problem is the following. I define, in the package, a method "[" for the S4 class 'genind'. The method's definition is:
> setMethod("[", signature(x="genind", i="ANY", j="ANY", drop="ANY"), function(x, i, j, ..., loc=NULL, treatOther=TRUE, drop=FALSE) { 
> ... ## code of the function
> })
> 
> When sourcing the code directly from R, this method is found and used normally. However, when loading the package, the method does not seem to be "found" any longer by the dispatcher. For instance, here is an error I get:
> ### R code
>> library(adegenet)
>> data(nancycats) # nancycat is a S4 object with class 'genind'
>> class(nancycats)
> [1] "genind"
> attr(,"package")
> [1] "adegenet"
>> nancycats[1]
> Error in nancycats[1] : object of type 'S4' is not subsettable # "[" IS NOT USED HERE
> ### end R code

For what it's worth, I can't reproduce this problem, and in a new R
session after library(adegenet) I have

> sessionInfo()
R version 2.11.0 Patched (2010-05-01 r51886)
x86_64-unknown-linux-gnu

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base

other attached packages:
[1] adegenet_1.2-4 MASS_7.3-5

which has a different set of attached / loaded packages -- maybe you are
picking up something from .RData or .Rprofile, and you need to start R
with --vanilla?

Martin



> 
> And still, the method is defined in the environment:
> ### R code
>> showMethods("[", class="genind")
> Function: [ (package base)
> x="genind"
> 
>> findMethods("[", classes="genind")$genind
> Method Definition:
> 
> function (x, i, j, ..., drop = FALSE) 
> {
>     .local <- function (x, i, j, ..., loc = NULL, treatOther = TRUE, 
>         drop = FALSE) 
>     {
>         if (missing(i)) 
>             i <- TRUE
>         if (missing(j)) 
>             j <- TRUE
>         pop <- NULL
> 
> ... # rest of the code of the function
> ### end R code
> 
> findMethods and showMethods find the method. However, getMethod doesn't:
> ### R code
>> getMethod("[","genind")
> Error in getMethod("[", "genind") : 
>   No method found for function "[" and signature genind
> ### end R code
> 
> Now, if I just try the same thing after sourcing the code of the method manually, everything works fine. showMethods then reads:
> ### R code
>> showMethods("[", classes="genind")
> Function: [ (package base)
> x="genind"
> x="genind", i="ANY", j="ANY", drop="ANY"
> x="genind", i="numeric", j="missing", drop="missing"
>     (inherited from: x="genind", i="ANY", j="ANY", drop="ANY")
> ### end R code
> 
> My R version/session is:
> ### R code
>> R.version
>                _                            
> platform       i686-pc-linux-gnu            
> arch           i686                         
> os             linux-gnu                    
> system         i686, linux-gnu              
> status                                      
> major          2                            
> minor          11.0                         
> year           2010                         
> month          04                           
> day            22                           
> svn rev        51801                        
> language       R                            
> version.string R version 2.11.0 (2010-04-22)
> 
>> sessionInfo()
> R version 2.11.0 (2010-04-22) 
> i686-pc-linux-gnu 
> 
> locale:
>  [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
>  [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
>  [5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
>  [7] LC_PAPER=en_GB.utf8       LC_NAME=C                
>  [9] LC_ADDRESS=C              LC_TELEPHONE=C           
> [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base     
> 
> other attached packages:
> [1] adegenet_1.2-4 MASS_7.3-5    
> 
> loaded via a namespace (and not attached):
> [1] graph_1.26.0  phylobase_0.5 tcltk_2.11.0  tools_2.11.0 
> 
> ### end R code
> 
> R was compiled from the sources. All packages are up-to-date (as of the 21 May 2010). My system is a Ubuntu 10.04 (32 bits), with kernel 2.6.31-20-generic. I could not reproduce the problem using R 2.11.0 on Windows Vista, or on Debian testing (R 2.11.0 compiled from the sources). The package adegenet does not have a namespace. The error arises whether using a field 'Collate' in DESCRIPTION, or not. I checked that the source file is indeed sourced when loading the package: it is. The method "[" is not defined by any other package for 'genind' objects.
> 
> I would be pleased to get any piece of insight, advice, or explanation for this issue. Thanks in advance for your time.
> 
> Best regards,
> 
> Thibaut Jombart.
> 
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



[Rd] S4 method defined but not used
Martin Morgan