This package supplies the following RStudio add-ins:
%.>%
” inserts “%.>%
” (wrapr
’s “dot arrow pipe”“)(.)
” inserts “(.)
” (“argument stand-in”)->.;
” inserts “->.;
” (“Bizarro pipe”):=
” inserts “:=
” (wrapr
’s “named map builder”)%>.%
” inserts “%>.%
” (wrapr
’s “to dot pipe”“)%>%
” inserts “%>%
” (“Magrittr pipe”, also usually available as a separate RStudio editor binding as Shift-ALT-m
)%<>%
” inserts “%<>%
” (“Magrittr compound pipe”)%<-%
” inserts “%<-%
” (“Zeallot assign”)The above are useful when bound to keyboard shortcuts in RStudio.
The R
code to install is below.
We first ensure that you have the latest CRAN release versions of:
install.packages(c("devtools", "htmltools", "shiny",
"miniUI", "formatR", "wrapr"))
Then we install this package itself from GitHub:
devtools::install_github("WinVector/addinexamples")
Finally bind “Insert %.>%
” to F9
(which has a right-facing glyph on some Mac keyboards) via Tools->Addins->BrowseAddins->KeyboardShortCuts
.
Once you have installed and configured all of the above you can press F9
to “Insert %.>%
” where you are typing. “%.>%
” is the wrapr
“dot pipe” (an alternative to the magrittr
%>%
pipe). This allows easy conversion of nested function application into left to right sequential pipe notation.
For example:
sin(sqrt(exp(4)))
## [1] 0.893855
library("wrapr")
4 %.>%
exp(.) %.>%
sqrt(.) %.>%
sin(.)
## [1] 0.893855
Dot pipe insists on explicit marking of function arguments with “.
”. If you also bind “Insert (.)
” to F10
typing the above pipelines can become very fast and efficient.
Basically:
F10
. This gives you the template for function arguments which you either leave alone (if the function only takes one argument) or edit to add in the additional arguments you need. This replaces pressing space after you type in a function name.F9
when you are done with a pipeline step and then enter or return.Try the above in the RStudio editor and the RStudio console, it works really well both places.
Bizarro pipe can be used the same way as we just used dot pipe (but does not require any package for implementation, as it is an emergent behavior of pre-existing base-R
semantics):
4 ->.;
exp(.) ->.;
sqrt(.) ->.;
sin(.)
## [1] 0.893855
Be aware that the value of a Bizarro pipeline comes off the right bottom end of the pipeline (not the top left end as with dot pipe). So to pick up the Bizarro pipeline value you need to use a right assignment of the form ->
at the end of the pipeline (or use {}
). This is, however, one of the features that makes Bizarro pipe superior for step debugging.
Both of these pipes also work with more complicated function signatures, and with dplyr
:
# assuming you have dplyr installed
suppressPackageStartupMessages(library("dplyr"))
starwars %.>%
group_by(., name) %.>%
summarize(., mean_height = mean(height)) %.>%
ungroup(.) %.>%
left_join(data_frame(name = c("Han Solo",
"Luke Skywalker")),
.,
by = 'name') %.>%
arrange(., desc(name))
## # A tibble: 2 x 2
## name mean_height
## <chr> <dbl>
## 1 Luke Skywalker 172.
## 2 Han Solo 180.
Another set of good choices for binding are:
Alt-Enter
for “Insert %.>%
” (or even Alt-;
)Alt-Space
for “Insert (.)
”Alt-=
for “Insert :=
”.That way you are treating the argument list as a space-like separator and the pipe symbol as a line-end-like separator.
Three user controllable add-ins are registered as part of this package: insert usr1
, insert usr2
, and insert usr3
. The user at run-time can change what function is called when the add-in is activated. For example to re-bind the first user add-in to insert :=
run the following R
code:
options("addinexamplesWV.usrFn1" = function() { rstudioapi::insertText(" := ") })
This allows changing settings without rebuilding the package.