Comments (2)
This seems to work :
debug_warning <- function(pattern = NULL, class = NULL) {
code <- '`*tmp_warning*` <- function (..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, domain = NULL) {
if (...length() == 1L && inherits(..1, "condition")) {
if ((is.null(CLASS) || inherits(..1, CLASS)) && (is.null(PATTERN) || grepl(PATTERN , ..1))) {
called_by_rlang_warn <- !is.null(call <- sys.call(-1)) && is.call(call) && (
identical(call[[1]], quote(warn)) || identical(call[[1]], quote(rlang::warn))
)
env <- if (called_by_rlang_warn) parent.frame(2) else parent.frame()
on.exit(eval(quote(browser()), env), add = TRUE)
}
cond <- ..1
if (nargs() > 1L)
cat(gettext("additional arguments ignored in warning()"),
"\n", sep = "", file = stderr())
message <- conditionMessage(cond)
call <- conditionCall(cond)
withRestarts({
.Internal(.signalCondition(cond, message, call))
.Internal(.dfltWarn(message, call))
}, muffleWarning = function() NULL)
invisible(message)
}
else {
if (is.null(PATTERN) || grepl(PATTERN , paste(c(...), collapse = ""))) {
called_by_rlang_warn <- !is.null(call <- sys.call(-1)) && is.call(call) && (
identical(call[[1]], quote(warn)) || identical(call[[1]], quote(rlang::warn))
)
env <- if (called_by_rlang_warn) parent.frame(2) else parent.frame()
on.exit(eval(quote(browser()), env), add = TRUE)
}
.Internal(warning(call., immediate., noBreaks., .makeMessage(..., domain = domain)))
}
}
unlockBinding("warning", baseenv())
assign("warning", value = `*tmp_warning*`, envir = baseenv())
lockBinding("warning", baseenv())
#rm(`*tmp_warning*`)
'
code <- gsub("PATTERN", deparse(pattern), code)
code <- gsub("CLASS", deparse(class), code)
con <- textConnection(code)
source(con, .GlobalEnv)
}
test <- function() {
foo <- 1
print(foo)
warning("some warning")
foo <- foo + 1
print(foo)
rlang::warn("other warning", "foo")
foo
}
test()
It debugs all warnings by default but we can restrict on class and regex
We need the undebug function that reset the warnings to original .
This belongs in the same package as keep_browsing()
keep_browsing <- function(env = parent.frame()) rlang::eval_bare(quote(on.exit(eval.parent(quote(browser())), add = TRUE)), env)
We might have debug_stop()
too, which avoids the failure and set browser(), and its little sister ignore_stop()
, that does the same without browser()
from debugverse.
Let's have ignore_warning()
and ignore_message()
too
We might have a helper package_stack(ignore.base = TRUE)
that displays which packages call which at any point by looking at sys.calls()
.
A wrapper around package_stack()
, browse_package_stack()
, might prompt the user to choose a package at any point and add on.exit(browser())
calls in the right places.
debug_warning()
, debug_message()
and debug_stop()
might use browse_package_stack()
so we can easily debug at the right place, which might often be the currently developed package. We could make this additional prompt optional but I'm not sure if it costs much.
{browse}, {browser}, {inspect} are available, {debug} and {debugr} are not
I think I wouldn't mind debug_warning()
-> browse::browse_warning()
, debugging is many things, browsing is really what we're doing.
A general browse(fun, at = NULL, if = NULL, after = NULL, called_by = NULL)
function might provide additional flexibility to the original debug
function.
We might add some _once
variants too.
from debugverse.
Related Issues (20)
- A getNamespaceExports that behaves well with devtools
- check that variables are not named like function names
- debug_in
- debug tryCatch and try
- error reprex with constructive
- NSE arg check
- act on last error
- insert comments "calls" and "called by"
- log_next()
- better list manipulation HOT 1
- git pull requests and tickets stored in repo
- Ctrl+Enter but takes default args if they're not assigned yet
- "log" anything, incl objects, to a safe place
- {revise} package HOT 5
- {wat} package for R puzzles
- clean_rprofile()
- attach an env with active binding for choices
- recover() AFTER error
- nse_mock
- An encapsulated object system that uses context so we have autocomplete without evaluation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from debugverse.