Adverb that starts browser() on failure #281
Comments
You're welcome! |
I've been using this function:
So basically encapsulate the error prone call in that and when it fails, you step right into where you want to be, including when inside implicit loops like I've also suggested trying to make a pipe version of this, but I couldn't figure out how to do that. |
This works: hopefully <- function(f) {
function(...) {
withCallingHandlers(
f(...),
error = function(e) {
# 1: h(simpleError(msg, call))
# 2: .handleSimpleError(function (e) <...>
# 3: stop(...)
ef <- eval_frame(4)
eval(quote(browser()), env = ef$env)
}
)
}
}
f <- function(x) {
y <- 20
if (x > 5) {
stop("!")
} else {
x
}
}
map(1:6, hopefully(f)) But is fragile: options(warn = 2)
f <- function(x) {
y <- 20
if (x > 5) {
warning("!")
} else {
x
}
}
map(1:6, hopefully(f)) @lionel- is there a better way here? |
This doesn't work in ESS because of some limitations of how it handles step-debugging. I don't think it's that fragile, this just needs another handler for warnings that checks for hopefully <- function(f) {
function(...) {
withCallingHandlers(
f(...),
error = function(e) {
# 1: h(simpleError(msg, call))
# 2: .handleSimpleError(function (e) <...>
# 3: stop(...)
with_env(frame$env, on.exit({
browser()
NULL # Workaround ESS issue
}))
return_from(frame)
},
warning = function(e) {
if (getOption("warn") == 2) {
frame <- eval_frame(7)
with_env(frame$env, on.exit({ browser(); NULL }))
return_from(frame)
}
}
)
}
} |
Unfortunately your work around doesn't work in RStudio. I think we'll have to switch based on For reasons I don't understand it no longer works when I extract the browse_in_frame <- function(frame) {
if (.Platform$GUI == "ESS") {
# Workaround ESS issue
with_env(frame$env, on.exit({
browser()
NULL
}))
return_from(frame)
} else {
eval(quote(browser()), envir = frame$env)
}
} |
Complete code in #304 |
Like this but in correct environment
The text was updated successfully, but these errors were encountered: