Re: [Rd] Why is the diag function so slow (for extraction)?

2015-05-12 Thread Henrik Bengtsson
Along Luke's lines, would(n't) it be enough to look for existence of
attribute 'class' to decide whether to dispatch or not, i.e. if c() is
needed or not?  Even without .subset(), there is a remarkable
improvement.  I think it's worth condition the code on dispatch or
not.  For example:

[HB-X201]{hb}: svn diff diag.R
Index: diag.R
===
--- diag.R  (revision 68345)
+++ diag.R  (working copy)
@@ -23,9 +23,11 @@
 stop("'nrow' or 'ncol' cannot be specified when 'x' is a matrix")

 if((m <- min(dim(x))) == 0L) return(vector(typeof(x), 0L))
+nms <- dimnames(x)
+nrow <- dim(x)[1L]
 ## NB: need double index to avoid overflows.
-y <- c(x)[1 + 0L:(m - 1L) * (dim(x)[1L] + 1)]
-nms <- dimnames(x)
+if (!is.null(attr(x, "class"))) x <- c(x)
+y <- x[1 + 0L:(m - 1L) * (nrow + 1)]
 if (is.list(nms) && !any(sapply(nms, is.null)) &&
 identical((nm <- nms[[1L]][seq_len(m)]), nms[[2L]][seq_len(m)]))
 names(y) <- nm

?

/Henrik

On Tue, May 12, 2015 at 5:33 AM, Martin Maechler
 wrote:
>> Steve Bronder 
>> on Thu, 7 May 2015 11:49:49 -0400 writes:
>
> > Is it possible to replace c() with .subset()?
>
> It would be possible, but I think "entirely" wrong.
>
> .subset() is documented to be an internal function not to be
> used "lightly" and more to the point it is documented to *NOT*
> dispatch at all.
>
> If you read and understood what Peter and Luke wrote, you'd not
> special case here:
>
> diag() should not work only for pure matrices, but for all
> "matrix-like" objects for which ``the usual methods'' work, such
> as
>as.vector(.), c(.)
>
> That's why there has been the c(.) in there.
>
> You can always make code faster if you write the code so it only
> has to work in one special case .. and work there very fast.
>
>
> > Example below
> > 
> > 
>
> > library(microbenchmark)
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] WISH: A more informative abor message than "aborting ..."

2015-05-12 Thread Henrik Bengtsson
When R aborts ("core dumps"), it outputs:

  "aborting ..."

This message is rather generic and can be hard to track back to R
itself, i.e. it is not always clear whether it is R itself that
aborted or some other piece of code that caused the abort/core dump
and outputted that message.

May I suggest to expand the message to clarify that it is R that
aborts and that make it explicit that it is the very last message that
is generated by R, e.g.

  "An exception occurred that R could not recover from. The R session
is now aborting ..."

The code that needs to be updated is in
https://svn.r-project.org/R/trunk/src/main/main.c.  Here's a patch for
the above suggestion:

$ svn diff src/main/main.c
Index: src/main/main.c
===
--- src/main/main.c (revision 68355)
+++ src/main/main.c (working copy)
@@ -594,7 +594,7 @@
}
}
 }
-REprintf("aborting ...\n");
+REprintf("An exception occurred that R could not recover from.
The R session is now aborting ...\n");
 R_CleanTempDir();
 /* now do normal behaviour, e.g. core dump */
 signal(signum, SIG_DFL);

FYI, after signal(), raise(signum) is called and I think that's it from R.

Thanks,

Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Unexpected failure when calling new() with unnamed arg and

2015-05-12 Thread Hervé Pagès

Hi,

The man page for new() suggests that if 'a' is an object with slots
"slot1" and "slot2" and C is a class that extends the class of 'a',
then the 2 following calls should be equivalent:

  new("C", a, ...)
  new("C", slot1=a@slot1, slot2=a@slot2, ...)

This is generally the case but I just ran into a situation where it's
not. In the following example the former fails while the latter works:

  setClass("A", representation(slot1="numeric", slot2="logical"))
  setClass("B", contains="A", representation(design="formula"))
  setClass("C", contains="B")
  a <- new("A", slot1=77, slot2=TRUE)

  new("C", a, design=x ~ y)  # fails
  new("C", slot1=a@slot1, slot2=a@slot2, design=x ~ y)  # works

Note that new("B", a, design=x ~ y) works so the 3-level class
hierarchy is really needed in order to reproduce.

Probably related to this, I also noted that new("B") and/or new("C")
return invalid objects:

  c <- new("C")

  validObject(c)
  # Error in validObject(c) :
  #  invalid class “C” object: invalid object for slot "design"
  #  in class "C": got class "S4", should be or extend class "formula"

  is(c@design, "formula")
  # [1] FALSE

  class(c@design)
  # [1] "S4"

Note that 'c' can be fixed:

  c@design <- formula(NULL)

  validObject(c)
  # [1] TRUE

Maybe something that the default "initialize" method should take care
of?

Another singularity that is maybe at the root of all of this is that
the "formula" S4 class is virtual:

  showClass("formula")
  # Virtual Class "formula" [package "methods"]
  #
  # Slots:
  #
  # Name:   .S3Class
  # Class: character
  #
  # Extends: "oldClass"

so a bare call to new("formula") fails:

  new("formula")
  # Error in new("formula") :
  #   trying to generate an object from a virtual class ("formula")

Shouldn't new("formula") just return an "empty" S3 formula (like
formula(NULL) does), in the same way that new("integer") returns
an empty ordinary integer vector?

Thanks,
H.

> sessionInfo()
R version 3.2.0 Patched (2015-04-17 r68202)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: Ubuntu 14.04.2 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8LC_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 utils datasets  methods   base

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] S4 method dispatch sometimes leads to incorrect when object loaded from file?

2015-05-12 Thread Martin Morgan

On 05/12/2015 05:31 AM, Martin Maechler wrote:

Martin Morgan 
 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 ?


I added dim() methods, which also misbehave (differently)

  setMethod("dim", "A", function(x) "A-dim")
  setMethod("dim", "B", function(x) "B-dim")

~/tmp$ R --vanilla --slave -e "load('b.Rda'); dim(b)"
Loading required package: PkgA
NULL
~/tmp$ R --vanilla --slave -e "require('PkgA'); load('b.Rda'); dim(b)"
[1] "B-dim"

but sort of auto-heal (versus show, which is corrupted)

~/tmp$ R --vanilla --slave -e "load('b.Rda'); dim(b); dim(b)"
Loading required package: PkgA
NULL
[1] "B-dim"
~/tmp$ R --vanilla --slave -e "load('b.Rda'); b; b"
Loading required package: PkgA
A
A




 >> 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@r-project.org mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-devel




--
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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Why is the diag function so slow (for extraction)?

2015-05-12 Thread Martin Maechler
> Steve Bronder 
> on Thu, 7 May 2015 11:49:49 -0400 writes:

> Is it possible to replace c() with .subset()? 

It would be possible, but I think "entirely" wrong.

.subset() is documented to be an internal function not to be
used "lightly" and more to the point it is documented to *NOT*
dispatch at all.

If you read and understood what Peter and Luke wrote, you'd not
special case here:

diag() should not work only for pure matrices, but for all
"matrix-like" objects for which ``the usual methods'' work, such
as
   as.vector(.), c(.)

That's why there has been the c(.) in there.

You can always make code faster if you write the code so it only
has to work in one special case .. and work there very fast.


> Example below
> 
> 

> library(microbenchmark)

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] S4 method dispatch sometimes leads to incorrect when object loaded from file?

2015-05-12 Thread Martin Maechler
> Martin Morgan 
> 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@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel