Hi, Suppose a function that checks an object: stop.if.dims <- function(x) { if (! is.null(dim(x))) { stop("cannot handle dimensional data") } } This would be used by other functions that can only work with dimensionless objects. The problem is the error message would need to include the name of the function that called stop.if.dims, so that the user knows which function got an argument that was incorrect. How do I do this? Or maybe there is another way... -- Ernest
On Feb 3, 2011, at 10:27 AM, Ernest Adrogu? wrote:> Hi, > Suppose a function that checks an object: > > stop.if.dims <- function(x) { > if (! is.null(dim(x))) { > stop("cannot handle dimensional data") > } > } >> mtx <- matrix(c(1,0,0,1), 2) > stop.if.dims <- function(x) { objname <- deparse(substitute(x)) + if (! is.null(dim(x))) { + stop(paste(objname,"cannot handle dimensional data") ) + } + } > stop.if.dims(mtx) Error in stop.if.dims(mtx) : mtx cannot handle dimensional data> This would be used by other functions that can only work with > dimensionless objects. The problem is the error message would need to > include the name of the function that called stop.if.dims, so that the > user knows which function got an argument that was incorrect. > > How do I do this? Or maybe there is another way... > > -- > Ernest > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Try this: g <- function(.x) tryCatch(stop.if.dims(.x), error=function(e)sprintf("Error in %s: %s", deparse(sys.call(1)), e$message)) g(rbind(2, 3)) 2011/2/3 Ernest Adrogué <nfdisco@gmail.com>> Hi, > Suppose a function that checks an object: > > stop.if.dims <- function(x) { > if (! is.null(dim(x))) { > stop("cannot handle dimensional data") > } > } > > This would be used by other functions that can only work with > dimensionless objects. The problem is the error message would need to > include the name of the function that called stop.if.dims, so that the > user knows which function got an argument that was incorrect. > > How do I do this? Or maybe there is another way... > > -- > Ernest > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 03/02/2011 10:27 AM, Ernest Adrogu? wrote:> Hi, > Suppose a function that checks an object: > > stop.if.dims<- function(x) { > if (! is.null(dim(x))) { > stop("cannot handle dimensional data") > } > } > > This would be used by other functions that can only work with > dimensionless objects. The problem is the error message would need to > include the name of the function that called stop.if.dims, so that the > user knows which function got an argument that was incorrect. > > How do I do this? Or maybe there is another way...I see you have the answer you wanted, but I'd suggest you don't need this: the user should just use traceback() after the error to see the full call stack. Perhaps it's not the caller that's the problem, but the caller of the caller... Duncan Murdoch