Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adply unexpected behavior when row raises an exception #257

Open
jeffwong opened this issue Jun 16, 2015 · 1 comment
Open

adply unexpected behavior when row raises an exception #257

jeffwong opened this issue Jun 16, 2015 · 1 comment

Comments

@jeffwong
Copy link

Hello, I am looping over rows of a data frame and doing some computation. Sometimes that computation throws an error for a specific row. I wrap everything in a tryCatch where adply correctly returns the value inside my error handler.

grid = data.frame(a = 1:3, b = 1:3)

xxx = adply(grid, 1, function(i) {
tryCatch({
if(any(i == 3)) {
stop("bad value")
} else {
return (4)
}
}, error = function(e) {
test = 2+3
return (test)
})
}, .inform=T)

Correctly returns xxx is
a b V1
1 1 1 4
2 2 2 4
3 3 3 5

Now if my error handler also prints a message about the error, e, adply fails.

grid = data.frame(a = 1:3, b = 1:3)

xxx = adply(grid, 1, function(i) {
tryCatch({
if(any(i == 3)) {
stop("bad value")
} else {
return (4)
}
}, error = function(e) {
message(e)
test = 2+3
return (test)
})
}, .inform=T)

Error in try(.fun(piece, ...)) : bad value
Error: with piece 3:
a b
3 3 3

xxx
Error: object 'xxx' not found

The pain point is that downstream I am expecting the variable xxx to exist, whether it's V1 column is populated with values from Catch or it is populated from values with Try. If I have message(e) in the error handler the variable xxx never gets set

@ankur-gupta
Copy link

I think message(e) throws an error.

grid = data.frame(a = 1:3, b = 1:3)

xxx = adply(grid, 1, function(i) {
  tryCatch({
    if(any(i == 3)) {
      stop("bad value")
    } else {
      return (4)
    }
  }, error = function(e) {
    message(e)
    print("This is never executed")
    test = 2+3
    return (test)
  })
}, .inform=T)

Try using print(e) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants