Hi,
Short answer: import 'as.matrix' and export your method(s) for it. From
WRE:
"All S4 classes to be used outside the package need to be listed in an
exportClasses directive. Alternatively, they can be specified using
exportClassPattern.(46) in the same style as for exportPattern. To export
methods for generics from other packages an exportMethods directive can be
used."
Details: the precise details depend on what exactly is in your NAMESPACE file.
The curious difference you observe is due to the fact that as.matrix is defined
in 'base' as S3 generic.
When you set an S4 method for it you effectively create your own as.matrix S4
generic and your settings in NAMESPACE probably export everything you
"own".
When you depend on package 'Matrix', 'as.matrix' is made S4
generic by it and you are defining a method for
An imported function, so you need to use exportMethods (see the above excerpt),
otherwise your method is not exported. (Indeed, in your example the arror is
from array() in the default method).
Note also that it matters where you define the test function. I am pretty sure
that you defined it in the global workspace (not in the package) to get the
error.
It is very useful practice to run 'R CMD check' (or its devtools
equivalent). In this case it would have warned you to import as.matrix. Also, to
make your life easier during initial development, import the whole
'Matrix' and when things work think about cleaning up and importing only
stuff that you need.
Best regards,
Georgi Boshnakov
------------------------------
Message: 15
Date: Fri, 27 Jan 2017 22:41:10 +0000
From: Robert McGehee <rmcgehee at walleyetrading.net>
To: "r-devel at r-project.org" <r-devel at r-project.org>
Subject: [Rd] Matrix package breaks as.matrix method
Message-ID:
<30D28A63376088428E8318DD67FD407F705B2E at
ny-mailstore1.walleyetrading.net>
Content-Type: text/plain; charset="us-ascii"
Hi,
The Matrix package and the as.matrix method do not seem to be compatible inside
of a package.
Here's an example. I've created a simple package "mat" that
defines an eponymous class and as.matrix method on that class. All is well,
unless that package has the Matrix package in its Depends or Imports (and
imports, e.g. the "Diagonal" function). Then my as.matrix method stops
working, even if I'm not using any part of the Matrix package.
Here's an example on R 3.3.2:
First, create an empty package "mat" (e.g. with package.skeleton) with
one file in mat/R/mat.R with the following contents:
setClass("mat", representation(H="matrix"))
mat <- function(H) new("mat", H=H)
setMethod("as.matrix", signature("mat"), function(x, ...)
crossprod(x at H))
testmat <- function() {
H <- matrix(1:3, 1, 3)
M <- mat(H)
as.matrix(M)
}
Then install the mat package :> require(mat)
> testmat()
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 4 6
[3,] 3 6 9
All works fine!
Now add "Depends: Matrix" into the package's DESCRIPTION file
(alternatively add "Imports: Matrix" and
'importFrom("Matrix","Diagonal")' in the NAMESPACE).
Try again:> require(mat)
> testmat()
Error in as.vector(data) :
no method for coercing this S4 class to a vector
Bug? If not, can anyone provide a work around? In my case, I'd like to mix
matrix and Matrix functions in my package, but am obviously having difficulty.
I've come across a somewhat similar report on stackoverflow
http://stackoverflow.com/questions/13812373/overloading-operator-in-r-s4-classes-and-matrix-package
regarding defining the "+" operator with the Matrix package, but I
don't think the solution or the problem quite applies.
Thanks in advance, Robert
> R.version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 3.2
year 2016
month 10
day 31
svn rev 71607
language R
version.string R version 3.3.2 (2016-10-31)
nickname Sincere Pumpkin Patch
------------------------------
Message: 16
Date: Fri, 27 Jan 2017 18:52:33 -0800
From: Henrik Bengtsson <henrik.bengtsson at gmail.com>
To: R-devel <r-devel at r-project.org>
Subject: [Rd] Subject: Milestone: 10000 packages on CRAN
Message-ID:
<CAFDcVCRC6pp3Humv7o48AerZ10Ox0bnxuo3COydzaGfCY17DUA at mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Continuing the tradition to post millennia milestones on CRAN:
So, it happened. Today (January 27, 2017 PCT) CRAN reached 10,000 packages [1].
Needless to say, the rate with which new packages are added to CRAN
keeps increasing and so does the number of contributors (maintainers).
Somewhere out there, there are ~3 persons who are about to submit
their first packages to CRAN today and ~3 persons who will submit
another package of theirs. And by the amazing work of the CRAN team,
these packages are inspected and quality controlled before going live
- which often happens within a day or so.
As usual and it can't be said too many times: A big thank you to the
CRAN team, to the R core, to all package developers, to our friendly
community, to everyone out there helping others, and to various online
services that simplify package development. We can all give back by
carefully reporting bugs to the maintainers, properly citing packages
we use in publications (see citation("pkg")), and help newcomers to
use R.
Milestones:
2017-01-27 10000 pkgs (+6.3/day over 158 days) 5845 mnts (+3.5/day)
2016-08-22 9000 pkgs (+5.7/day over 175 days) 5289 mnts (+5.8/day)
2016-02-29 8000 pkgs (+5.0/day over 201 days) 4279 mnts (+0.7/day)
2015-08-12 7000 pkgs (+3.4/day over 287 days) 4130 mnts (+2.4/day)
2014-10-29 6000 pkgs (+3.0/day over 335 days) 3444 mnts (+1.6/day)
2013-11-08 5000 pkgs (+2.7/day over 442 days) 2900 mnts (+1.2/day)
2012-08-23 4000 pkgs (+2.1/day over 469 days) 2350 mnts
2011-05-12 3000 pkgs (+1.7/day over 585 days)
2009-10-04 2000 pkgs (+1.1/day over 906 days)
2007-04-12 1000 pkgs
2004-10-01 500 pkgs
2003-04-01 250 pkgs
2002-09-17 68 pkgs
1997-04-23 12 pkgs
These data are for CRAN only [1-13]. There are many more packages
elsewhere, e.g. Bioconductor, GitHub, R-Forge etc.
[1] http://cran.r-project.org/web/packages/
[2] https://en.wikipedia.org/wiki/R_(programming_language)#Milestones
[3] http://www.r-pkg.org/
[4] Private data
[5] https://stat.ethz.ch/pipermail/r-devel/2007-April/045359.html
[6] https://stat.ethz.ch/pipermail/r-devel/2009-October/055049.html
[7] https://stat.ethz.ch/pipermail/r-devel/2011-May/061002.html
[8] https://stat.ethz.ch/pipermail/r-devel/2012-August/064675.html
[9] https://stat.ethz.ch/pipermail/r-devel/2013-November/067935.html
[10] https://stat.ethz.ch/pipermail/r-devel/2014-October/069997.html
[11] https://stat.ethz.ch/pipermail/r-package-devel/2015q3/000393.html
[12] https://stat.ethz.ch/pipermail/r-devel/2016-February/072388.html
[13] https://stat.ethz.ch/pipermail/r-devel/2016-August/073011.html
All the best,
Henrik
(just a user)
------------------------------
Subject: Digest Footer
_______________________________________________
R-devel at r-project.org mailing list DIGESTED
https://stat.ethz.ch/mailman/listinfo/r-devel
------------------------------
End of R-devel Digest, Vol 167, Issue 25