Martin Morgan
2015-May-10 15:19 UTC
[Rd] S4 method dispatch sometimes leads to incorrect when object loaded from file?
Loading an S4 object from a file without first loading the library sometimes (?, the example below and actual example involves a virtual base class and the show generic) leads to incorrect dispatch (to the base class method). The attached package reproduces the problem. It has setClass("A") setClass("B", contains="A") setMethod("show", "A", function(object) cat("A\n")) setMethod("show", "B", function(object) cat("B\n")) with NAMESPACE import(methods) exportClasses(A, B) exportMethods(show) This creates the object and illustrated expected behavior ~/tmp$ R --vanilla --slave -e "library(PkgA); b = new('B'); save(b, file='b.Rda'); b" B Loading PkgA before the object leads to correct dispatch ~/tmp$ R --vanilla --slave -e "library(PkgA); load(file='b.Rda'); b" B but loading the object without first loading PkgA leads to dispatch to show,A-method. ~/tmp$ R --vanilla --slave -e "load(file='b.Rda'); b" Loading required package: PkgA A 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
Martin Morgan
2015-May-11 17:18 UTC
[Rd] S4 method dispatch sometimes leads to incorrect when object loaded from file?
On 05/10/2015 08:19 AM, Martin Morgan wrote:> Loading an S4 object from a file without first loading the library sometimes (?, > the example below and actual example involves a virtual base class and the show > generic) leads to incorrect dispatch (to the base class method). > > The attached package reproduces the problem. It hasThe package was attached but stripped; a version is at https://github.com/mtmorgan/PkgA FWIW the sent mail was a multi-part MIME with the header on the package part Content-Type: application/gzip; name="PkgA.tar.gz" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="PkgA.tar.gz" From http://www.r-project.org/mail.html#instructions "we allow application/pdf, application/postscript, and image/png (and x-tar and gzip on R-devel)" so I thought that this mime type would not be stripped? Martin Morgan> > setClass("A") > setClass("B", contains="A") > setMethod("show", "A", function(object) cat("A\n")) > setMethod("show", "B", function(object) cat("B\n")) > > with NAMESPACE > > import(methods) > exportClasses(A, B) > exportMethods(show) > > This creates the object and illustrated expected behavior > > ~/tmp$ R --vanilla --slave -e "library(PkgA); b = new('B'); save(b, > file='b.Rda'); b" > B > > Loading PkgA before the object leads to correct dispatch > > ~/tmp$ R --vanilla --slave -e "library(PkgA); load(file='b.Rda'); b" > B > > but loading the object without first loading PkgA leads to dispatch to > show,A-method. > > ~/tmp$ R --vanilla --slave -e "load(file='b.Rda'); b" > Loading required package: PkgA > A > > 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
Martin Maechler
2015-May-12 12:31 UTC
[Rd] S4 method dispatch sometimes leads to incorrect when object loaded from file?
>>>>> Martin Morgan <mtmorgan at fredhutch.org> >>>>> on Mon, 11 May 2015 10:18:07 -0700 writes:> On 05/10/2015 08:19 AM, Martin Morgan wrote: >> Loading an S4 object from a file without first loading the library sometimes (?, >> the example below and actual example involves a virtual base class and the show >> generic) leads to incorrect dispatch (to the base class method). "Of course", this is not as desired. Other code automatically does try and typically succeed to load the package (yes "package" ! ;-)) when 'needed', right, so show() is an exception here, no ? >> The attached package reproduces the problem. It has > The package was attached but stripped; a version is at > https://github.com/mtmorgan/PkgA > FWIW the sent mail was a multi-part MIME with the header on the package part > Content-Type: application/gzip; > name="PkgA.tar.gz" > Content-Transfer-Encoding: base64 > Content-Disposition: attachment; > filename="PkgA.tar.gz" > From http://www.r-project.org/mail.html#instructions "we allow application/pdf, > application/postscript, and image/png (and x-tar and gzip on R-devel)" so I > thought that this mime type would not be stripped? You were alright in your assumptions -- but unfortunately, the accepted type has been application/x-gzip instead of .../gzip. I now *have* added the 2nd one as well. Sorry for that. The other Martin M.. > Martin Morgan >> >> setClass("A") >> setClass("B", contains="A") >> setMethod("show", "A", function(object) cat("A\n")) >> setMethod("show", "B", function(object) cat("B\n")) >> >> with NAMESPACE >> >> import(methods) >> exportClasses(A, B) >> exportMethods(show) >> >> This creates the object and illustrated expected behavior >> >> ~/tmp$ R --vanilla --slave -e "library(PkgA); b = new('B'); save(b, >> file='b.Rda'); b" >> B >> >> Loading PkgA before the object leads to correct dispatch >> >> ~/tmp$ R --vanilla --slave -e "library(PkgA); load(file='b.Rda'); b" >> B >> >> but loading the object without first loading PkgA leads to dispatch to >> show,A-method. >> >> ~/tmp$ R --vanilla --slave -e "load(file='b.Rda'); b" >> Loading required package: PkgA >> A >> >> 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 > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Maybe Matching Threads
- S4 method dispatch sometimes leads to incorrect when object loaded from file?
- S4 method dispatch sometimes leads to incorrect when object loaded from file?
- improved error message when existing implicit S4 generic is not imported?
- S4 Generics and NAMESPACE : justified warning ?
- S4 generic not exported correctly / incorrect dispatch?