magrittr
magrittr copied to clipboard
Debugging a pipe with browser()
Can we make this work?
100 %>% sample(10) %T>% browser() %>% sum()
The primary challenge is that the first argument to browser() is text.
100 %>% sample(10) %T>% { browser() } %>% sum()
more useful in the proposal branch though.
I know we want to minimise aliases, but maybe this would be useful enough to qualify for inclusion:
pipe_browser <- function(x, condition = NULL) {
if (is.null(condition) || isTRUE(condition) {
browser()
}
invisible(x)
}
(Edited to support conditional browsing)
Ooops, except that we need to evaluate browser() in the caller_env()
library(rlang)
pipe_browser <- function(x, condition = NULL) {
if (is.null(condition) || isTRUE(condition)) {
eval_bare(expr(browser(skipCalls = 1L)), env = caller_env())
}
invisible(x)
}
# Check that it works
f <- function(x) {
y <- 10
pipe_browser(y)
}
f(1)