4
myFunc <- function(x)
{
  x <- timeSeries(x, charvec=as.Date(index(x)))
  t<-tryCatch(  doSomething(x), error=function(x) rep(0,ncol(x))
  )
  t
}

How do I pass x into the error function? When I run the above I get:

Error in rep(0, ncol(x)) : invalid 'times' argument

1
  • Read the error message: it's saying ncol(x) is NA (or possibly NULL or less likely Inf). You didn't say what package timeSeries belongs to, but I'd guess it doesn't return an object which has columns. Commented May 23, 2013 at 11:38

1 Answer 1

5

The error argument is a handler, documented (see ?tryCatch) to accept one argument (the error condition). The error handler has access to whatever variables were available at the time stop was invoked. So

f = function() {
    tryCatch({
        i = 1
        stop("oops")
    }, error=function(e) {
        stop(conditionMessage(e), " when 'i' was ", i)
    })
}

catches the error thrown by the code, discovers the value i, and emits a more informative message. So I'd guess

myFunc <- function(x)
{
    tryCatch({
        x <- timeSeries(x, charvec=as.Date(index(x)))
        doSomething(x)
    }, error=function(...) rep(0, ncol(x)))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.