Hi,

I'm writing a manual using Sweave and I want to be able to print errors from
bad code. Here's an example:

<Function-4a>>=
MySqrt <- function(x) {
  if (missing(x)) {
    stop("'x' is missing with no default")
  }
  if (!is.numeric(x)) {
    stop("'x' should only be numeric")
  }
  if (x < 0) {
    stop("'x' should be non-negative")
  }
  return(sqrt(x))
}
MySqrt()
MySqrt("a")
MySqrt(-2)
MySqrt(4)
@

And I would like the output to be:

MySqrt <- function(x) {
...
}
> MySqrt()
Error in MySqrt() : 'x' is missing with no default
> MySqrt("a")
Error in MySqrt("a") : 'x' should only be numeric
> MySqrt(-2)
Error in MySqrt(-2) : 'x' should be non-negative
> MySqrt(2)
[1] 1.414214

I.e. I want the Error statements to print in the tex file and not just make
Sweave to bomb.

Thanks,

--sundar

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to