Re: [Rd] Bug is as.matrix.data.frame with nested data.frame

2017-12-01 Thread Martin Maechler
> Patrick Perry 
> on Thu, 30 Nov 2017 22:45:21 -0500 writes:

> Converting a data.frame with a nested data.frame to a matrix fails:
> x <- structure(list(a = data.frame(letters)),
> class = "data.frame",
> row.names = .set_row_names(26))

> as.matrix(x)
> #> Error in ncol(xj) : object 'xj' not found

> The offending code is here, in the definition of as.matrix.data.frame 
> (source/base/all.R):
^^ that's *not* file from the sources of R !

> for (j in pseq) {
>if (inherits(X[[j]], "data.frame") && ncol(xj) > 1L)
>X[[j]] <- as.matrix(X[[j]])
>xj <- X[[j]]

> It should be ncol(X[[j]]), not ncol(xj). 

yes (or something similar ..)
Interesting, that this has not been detected earlier.

[ OTOH, who nests data.frames inside data.frames? ]

If I fix the above, there's a next "bug" preventing
as.matrix(x)  to work for your case.

I'll look into it.
 
   > Also, the code would be more 
> generic if it used is.data.frame here instead of inherits(, "data.frame").

No, that is not true as is.data.frame() is not generic, and base
functions call base, so there can't be a difference.

Martin

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


[Rd] tryCatch in on.exit()

2017-12-01 Thread William Dunlap via R-devel
The following example involves a function whose on.exit()
expression both generates an error and catches the error.
The body of the function also generates an error.

When calling the function wrapped in a tryCatch, should
that tryCatch's error function be given the error from the
body of the function, since the one from the on.exit has
already been dealt with?  Currently the outer tryCatch gets
the error from the on.exit expression.

xx <- function() {
  on.exit(tryCatch(
expr = stop("error in xx's on.exit"),
error=function(e) {
  cat("xx's on.exit caught error: <<", conditionMessage(e), ">>\n",
sep="")
}))
  stop("error in body of xx")
}
zz <- tryCatch(xx(), error=function(e)paste("outer tryCatch caught error
<<", conditionMessage(e), ">>", sep=""))
#xx's on.exit caught error: <>
zz
#[1] "outer tryCatch caught error <>"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

[[alternative HTML version deleted]]

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


Re: [Rd] tryCatch in on.exit()

2017-12-01 Thread William Dunlap via R-devel
Things work as I would expect if you give stop() a condition object instead
of a string:

makeError <- function(message, class = "simpleError", call = sys.call(-2)) {
structure(list(message=message, call=call), class=c(class, "error",
"condition"))
}
f0 <- function() {
on.exit(tryCatch(expr = stop("pb. in f0's on.exit"),
 error = function(e)cat("[error] caught",
paste(collapse="/", class(e)), ":", conditionMessage(e), "\n")))
stop("pb. in f0")
}
f1 <- function() {
on.exit(tryCatch(expr = stop(makeError("pb. in f1's on.exit",
class="simpleError")),
 error = function(e)cat("[error] caught",
paste(collapse="/", class(e)), ":", conditionMessage(e), "\n")))
stop(makeError("pb. in f1", class="simpleError"))
}
catch <- function(FUN) {
tryCatch(
expr = FUN(),
error = function(e)paste("[error] caught", paste(collapse="/",
class(e)), ":", conditionMessage(e)))
}
catch(f0) # calls stop("string")
#[error] caught simpleError/error/condition : pb. in f0's on.exit
#[1] "[error] caught simpleError/error/condition : pb. in f0's on.exit"
catch(f1) # calls stop(conditionObject)
#[error] caught simpleError/error/condition : pb. in f1's on.exit
#[1] "[error] caught simpleError/error/condition : pb. in f1"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Dec 1, 2017 at 12:58 PM, William Dunlap  wrote:

> The following example involves a function whose on.exit()
> expression both generates an error and catches the error.
> The body of the function also generates an error.
>
> When calling the function wrapped in a tryCatch, should
> that tryCatch's error function be given the error from the
> body of the function, since the one from the on.exit has
> already been dealt with?  Currently the outer tryCatch gets
> the error from the on.exit expression.
>
> xx <- function() {
>   on.exit(tryCatch(
> expr = stop("error in xx's on.exit"),
> error=function(e) {
>   cat("xx's on.exit caught error: <<", conditionMessage(e), ">>\n",
> sep="")
> }))
>   stop("error in body of xx")
> }
> zz <- tryCatch(xx(), error=function(e)paste("outer tryCatch caught error
> <<", conditionMessage(e), ">>", sep=""))
> #xx's on.exit caught error: <>
> zz
> #[1] "outer tryCatch caught error <>"
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>

[[alternative HTML version deleted]]

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


Re: [Rd] tryCatch in on.exit()

2017-12-01 Thread luke-tierney

Thanks -- will look into it.

luke

On Fri, 1 Dec 2017, William Dunlap via R-devel wrote:


The following example involves a function whose on.exit()
expression both generates an error and catches the error.
The body of the function also generates an error.

When calling the function wrapped in a tryCatch, should
that tryCatch's error function be given the error from the
body of the function, since the one from the on.exit has
already been dealt with?  Currently the outer tryCatch gets
the error from the on.exit expression.

xx <- function() {
 on.exit(tryCatch(
   expr = stop("error in xx's on.exit"),
   error=function(e) {
 cat("xx's on.exit caught error: <<", conditionMessage(e), ">>\n",
sep="")
   }))
 stop("error in body of xx")
}
zz <- tryCatch(xx(), error=function(e)paste("outer tryCatch caught error
<<", conditionMessage(e), ">>", sep=""))
#xx's on.exit caught error: <>
zz
#[1] "outer tryCatch caught error <>"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

[[alternative HTML version deleted]]

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



--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
   Actuarial Science
241 Schaeffer Hall  email:   luke-tier...@uiowa.edu
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu

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