Skip to content

Commit

Permalink
functions.rmd: Writing pipeable functions (hadley#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindbrook authored and hadley committed May 3, 2017
1 parent 25d0c9b commit 413e407
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions functions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,9 @@ This tends to make the code easier to understand, because you don't need quite s

### Writing pipeable functions

If you want to write your own pipeable functions, thinking about the return value is important. There are two main types of pipeable functions: transformation and side-effect.
If you want to write your own pipeable functions, it's important to think about the return value. Knowing the return value's object type will mean that your pipeline will "just work". For example, with dplyr and tidyr the object type is the data frame.

In __transformation__ functions, there's a clear "primary" object that is passed in as the first argument, and a modified version is returned by the function. For example, the key objects for dplyr and tidyr are data frames. If you can identify what the object type is for your domain, you'll find that your functions just work with the pipe.

__Side-effect__ functions are primarily called to perform an action, like drawing a plot or saving a file, not transforming an object. These functions should "invisibly" return the first argument, so they're not printed by default, but can still be used in a pipeline. For example, this simple function that prints out the number of missing values in a data frame:
There are two basic types of pipeable functions: transformations and side-effects. With __transformations__, an object is passed to the function's first argument and a modified object is returned. With __side-effects__, the passed object is not transformed. Instead, the function performs an action on the object, like drawing a plot or saving a file. Side-effects functions should "invisibly" return the first argument, so that while they're not printed they can still be used in a pipeline. For example, this simple function prints the number of missing values in a data frame:

```{r}
show_missings <- function(df) {
Expand Down

0 comments on commit 413e407

Please sign in to comment.