--- title: "Backpipe Operations" author: "Christopher Brown" date: "`r Sys.Date()`" output: rmarkdown::html_vignette: toc: true vignette: > %\VignetteIndexEntry{Backpipe Operations} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` The backpipe package provides a single 'backpipe" operator (`%<%`) that allows the order of operands in a pipe statuement to be inverted. In some situations this promotes more legible, interpretable and debuggable code. Popular packages *magrittr* and *pipeR* do not provide a backward pipe operator. This package files the void by providing a `%<%` for use with *magrittr* and `%<<%` for use with *pipeR*. The package also provides the `backpipe`function for defining backpipe operators for any forward pipe implementation ## Installation ### CRAN (release) install.packages('backpipe') ### Github (devel) library(devtools) install_github("decisionpatterns/backpipe") ## Usage # BASIC USAGE mean %<% 1:5 # magrittr mean %<<% 1:5 # pipeR # CHAINING mean %<% range %<% 1:5 # CHAINING WITH MIXED PIPES # Chained with mixed pipes are performed left-to-right # Although technically possible, this is probably a bad idea. add(1) %<% 1:5 %>% multiply_by(2) 1:5 %>% add(1) %>% multiply_by(2) # same ## Keyboard Shortcut The `insert_backpipe` function can be bound to a keyboard shortcut. We recommend: `CTRL + SHIFT + <` This can be done through RStudio's **Tools > Modify Keyboard Shortcuts** menu. Future versions of this package may do this automatically. ## Common Use Cases **backpipe** can be used to: * Write clearer, more debuggable shinyUI code such that the order of code matches the HTML output. ```` div() %<% p %<% "Hello World" ```` * write tests, assertions, logging statements, etc. where the command is listed first ```` warn %<% "there is a problem here" # OR expect_true() %<% (ans==1) # OR log_warn %<% "This didn't work" ```` ## Motivation `magrittr`, `pipeR` and other pipes allow the developer to create left-to-right operation flows. However, some code is better expressed with a right-to-left syntax and is more common than one might expect. As an example, consider how **shiny** has the developer write HTML-producing code.Let's say you wished to produce the following HTML: