From 413e4072423733ef25ed0b2a5f2f38a8407f02d2 Mon Sep 17 00:00:00 2001 From: lindbrook Date: Wed, 3 May 2017 05:52:35 -0700 Subject: [PATCH] functions.rmd: Writing pipeable functions (#475) --- functions.Rmd | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index f65be0e..2c92713 100644 --- a/functions.Rmd +++ b/functions.Rmd @@ -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) {