[Rd] multiple defines of diag
Kasper Daniel Hansen
kasperdanielhansen at gmail.com
Thu Oct 6 19:53:00 CEST 2011
On Thu, Oct 6, 2011 at 1:19 PM, Terry Therneau <therneau at mayo.edu> wrote:
>
> On Thu, 2011-10-06 at 10:00 -0400, Kasper Daniel Hansen wrote:
>> if you're using two packages that both define a diag function/method
>> you absolutely _have_ to resolve this using your NAMESPACE. [Update:
>> I see both are methods. I actually don't know what happens when you
>> have the same generic in both packages]
>>
>
> Your response made me look further, with some surprising results.
>
> 1. Sequential loading
> tmt226% R --vanilla
> R version 2.13.0 (2011-04-13)
>
>> library(bdsmatrix)
>> tmat <- bdsmatrix(c(3,2,2,4),
> c(22,1,2,21,3,20,19,4,18,17,5,16,15,6,7, 8,14,9,10,13,11,12),
> matrix(c(1,0,1,1,0,0,1,1,0,1,0,10,0,
> 0,1,1,0,1,1,0,1,1,0,1,0,10), ncol=2))
>> tmat[1:7,1:7] [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,] 22 1 2 0 0 0 0
> [2,] 1 21 3 0 0 0 0
> [3,] 2 3 20 0 0 0 0
> [4,] 0 0 0 19 4 0 0
> [5,] 0 0 0 4 18 0 0
> [6,] 0 0 0 0 0 17 5>
>
>> diag(tmat)
> [1] 22 21 20 19 18 17 16 15 14 13 12 10 10
>
>> library(Matrix)
> Loading required package: lattice
>
> Attaching package: 'Matrix'
>
> The following object(s) are masked from 'package:base':
>
> det
>
>> diag(tmat)
> [1] 22 21 20 19 18 17 16 15 14 13 12 10 10
>
> Things to note:
> in this case I did not get a message about overwriting the diag
> method,
> it works.
>
> This was not my experience with ranef(), an S3 generic that coxme, nlme,
> and lme4 all define; there whichever library loaded last did not
> discover existing methods. That is, if one loaded nlme after coxme,
> then ranef(a coxme object) would not dispatch ranef.coxme. Our solution
> (Doug Bates and I) was to have both coxme and lme4 import ranef and
> fixef from the nlme library.
> However, per above it appears to work with S4 generics.
> Can I count on it though?
My _guess_ is that you cannot depend on this in general. What if the
two generics have different signatures, say one uses x,y and the other
uses x,y,z?
Kasper
>
> -------------
>
> Case 2:
>
> tmt229% R --vanilla
> R version 2.13.0 (2011-04-13)
>
>> library(coxme)
> Loading required package: survival
> Loading required package: splines
> Loading required package: bdsmatrix
> Loading required package: nlme
> Loading required package: Matrix
> Loading required package: lattice
>
> Attaching package: 'Matrix'
>
> The following object(s) are masked from 'package:base':
>
> det
>
> Warning message:
> replacing previous import ‘diag’ when loading ‘Matrix’
>
>> tmat <- bdsmatrix(c(3,2,2,4),
> c(22,1,2,21,3,20,19,4,18,17,5,16,15,6,7,
> 8,14,9,10,13,11,12),
> matrix(c(1,0,1,1,0,0,1,1,0,1,0,10,0,
> 0,1,1,0,1,1,0,1,1,0,1,0,10), ncol=2))
>>diag(tmat)
> [1] 22 21 20 19 18 17 16 15 14 13 12 10 10
>
>
> Things to note:
> I now get a warning message about diag. Why only here?
> Per the earlier comment I'm not importing all of nlme, just the two
> generics
> It still works
>
> (This example isn't reproducable for others: the coxme library on CRAN
> does not yet have a Matrix dependency.)
>
>
>
>
>
>
More information about the R-devel
mailing list