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

Debugging a pipe with browser() #172

Open
hadley opened this issue Jul 2, 2018 · 3 comments
Open

Debugging a pipe with browser() #172

hadley opened this issue Jul 2, 2018 · 3 comments
Labels
feature a feature request or enhancement

Comments

@hadley
Copy link
Member

hadley commented Jul 2, 2018

Can we make this work?

100 %>% sample(10) %T>% browser() %>% sum()

The primary challenge is that the first argument to browser() is text.

@hadley hadley added the feature a feature request or enhancement label Jul 2, 2018
@hadley hadley mentioned this issue Jul 2, 2018
@smbache
Copy link
Member

smbache commented Jul 2, 2018

100 %>% sample(10) %T>% { browser() } %>% sum()

more useful in the proposal branch though.

@hadley
Copy link
Member Author

hadley commented Jul 3, 2018

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)

@hadley
Copy link
Member Author

hadley commented Jul 3, 2018

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)

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

No branches or pull requests

2 participants