-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
122 lines (97 loc) · 4.15 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
tags: [r]
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(47)
```
# pipecleaner
[![Travis build status](https://travis-ci.org/alistaire47/pipecleaner.svg?branch=master)](https://travis-ci.org/alistaire47/pipecleaner)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/agx2ht7cpwrnrk0k?svg=true)](https://ci.appveyor.com/project/alistaire47/pipecleaner)
[![Coverage status](https://codecov.io/gh/alistaire47/pipecleaner/branch/master/graph/badge.svg)](https://codecov.io/github/alistaire47/pipecleaner?branch=master)
[![CRAN status](https://www.r-pkg.org/badges/version/pipecleaner)](https://cran.r-project.org/package=pipecleaner)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
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.
## Installation
pipecleaner is not currently on CRAN, but can be installed with
``` r
# install.packages("remotes")
remotes::install_github("alistaire47/pipecleaner")
```
## Debugging pipelines
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](https://www.rstudio.com/products/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](https://rstudio.github.io/rstudioaddins/) 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:
```{r debug_pipeline}
library(magrittr)
library(pipecleaner)
debug_pipeline(
x = 1:5 %>%
rev %>%
{. * 2} %>%
sample(replace = TRUE)
)
```
## Bursting pipes
Occasionally it is necessary to restructure code from a piped to an unpiped
form. Now `burst_pipes` makes this sort of restructuring simple:
```{r burst_pipes}
burst_pipes(
x = 1:5 %>%
rev %>%
{. * 2} %>%
.[3] %>%
rnorm(1, ., sd = ./10)
)
```
More specific names can be specified as a character vector:
```{r}
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.
## Limitations
pipecleaner should successfully debug most pipelines. However, due to its
structure, it does have known limitations:
- Only the `%>%` pipe is handled, not more exotic pipes like `%$%`. For the
moment, this is unlikely to change absent significant demand.
- Nested pipelines—e.g. piping within an anonymous function in
`purrr::map`—are ignored; the whole call is treated as one step.
## Related
- [ViewPipeSteps](https://github.com/daranzolin/ViewPipeSteps) is a similar
project that calls `View()` after each step in the pipeline.
- magrittr itself contains `debug_pipe`, which is a wrapper around `browser`
that returns its input, allowing it to be inserted within a pipeline.