knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) set.seed(47)
pipecleaner is a utility R package to debug pipelines using the magrittr %>%
pipe. Its debug_pipeline
launches the debugging browser on the input pipeline
in a form that allows the user to step through the successive calls of the
pipeline, examining the output of each successive element.
pipecleaner is not currently on CRAN, but can be installed with
# install.packages("remotes") remotes::install_github("alistaire47/pipecleaner")
To debug a pipeline, call debug_pipeline
on the raw code or a character
vector of code. If no input is supplied and it is called
from RStudio, it will use whatever
code is highlighed in the source editor as input.
debug_pipeline
can also be called via
an RStudio add-in by highlighting
the pipeline to debug and then selecting "Debug pipeline in browser" from the
"Addins" menu.
Once called, debug_pipeline
will reassemble the pipeline into a function that
can be debugged in the browser and call the debugger. Each line adds another
call from the pipeline and prints and the output so the user can see the status
of the data passed through the pipeline by stepping through the function.
The data is also stored to a variable called dot[N]
in each line,
where [N]
is the index of the call, making it easy to compare input and
output data of a step in the pipeline and try out new code formulations in the
console.
All together, it looks like this:
library(magrittr) library(pipecleaner) debug_pipeline( x = 1:5 %>% rev %>% {. * 2} %>% sample(replace = TRUE) )
Occasionally it is necessary to restructure code from a piped to an unpiped
form. Now burst_pipes
makes this sort of restructuring simple:
burst_pipes( x = 1:5 %>% rev %>% {. * 2} %>% .[3] %>% rnorm(1, ., sd = ./10) )
More specific names can be specified as a character vector:
burst_pipes( x <- 1:5 %>% rev %>% {. * 2} %>% .[3] %>% rnorm(1, ., sd = ./10), names = c("reversed", "doubled", "third", "x") )
burst_pipes
can also be called via a pair of RStudio add-ins, which replace
the highlighted code with its restructured form. The "Burst pipes" add-in
creates names; the "Burst pipes and set names" add-in allows custom names to be
set.
pipecleaner should successfully debug most pipelines. However, due to its structure, it does have known limitations:
%>%
pipe is handled, not more exotic pipes like %$%
. For the
moment, this is unlikely to change absent significant demand.purrr::map
—are ignored; the whole call is treated as one step. View()
after each step in the pipeline.debug_pipe
, which is a wrapper around browser
that returns its input, allowing it to be inserted within a pipeline.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.