diff --git a/.Rbuildignore b/.Rbuildignore index ce8fda2..1b8288a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -8,3 +8,4 @@ ^docs$ ^README\.Rmd$ ^README-.*\.png$ +^appveyor\.yml$ diff --git a/README.Rmd b/README.Rmd index 1d00af2..2596654 100644 --- a/README.Rmd +++ b/README.Rmd @@ -16,6 +16,7 @@ library(magrittr) # magrittr [![Travis-CI Build Status](https://travis-ci.org/tidyverse/magrittr.svg?branch=master)](https://travis-ci.org/tidyverse/magrittr) +[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/tidyverse/magrittr?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/magrittr) [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/magrittr)](https://cran.r-project.org/package=magrittr) ## Overview diff --git a/README.md b/README.md index c495adb..6379b5d 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,31 @@ -magrittr -========================================================= -[![Travis-CI Build Status](https://travis-ci.org/tidyverse/magrittr.svg?branch=master)](https://travis-ci.org/tidyverse/magrittr) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/magrittr)](https://cran.r-project.org/package=magrittr) +# magrittr -Overview --------- +[![Travis-CI Build +Status](https://travis-ci.org/tidyverse/magrittr.svg?branch=master)](https://travis-ci.org/tidyverse/magrittr) +[![AppVeyor build +status](https://ci.appveyor.com/api/projects/status/github/tidyverse/magrittr?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/magrittr) +[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/magrittr)](https://cran.r-project.org/package=magrittr) -The magrittr package offers a set of operators which make your code more readable by: +## Overview -- structuring sequences of data operations left-to-right (as opposed to from the inside and out), -- avoiding nested function calls, -- minimizing the need for local variables and function definitions, and -- making it easy to add steps anywhere in the sequence of operations. +The magrittr package offers a set of operators which make your code more +readable by: -The operators pipe their left-hand side values forward into expressions that appear on the right-hand side, i.e. one can replace `f(x)` with `x %>% f()`, where `%>%` is the (main) pipe-operator. When coupling several function calls with the pipe-operator, the benefit will become more apparent. Consider this pseudo example: + - structuring sequences of data operations left-to-right (as opposed + to from the inside and out), + - avoiding nested function calls, + - minimizing the need for local variables and function definitions, + and + - making it easy to add steps anywhere in the sequence of operations. + +The operators pipe their left-hand side values forward into expressions +that appear on the right-hand side, i.e. one can replace `f(x)` with `x +%>% f()`, where `%>%` is the (main) pipe-operator. When coupling several +function calls with the pipe-operator, the benefit will become more +apparent. Consider this pseudo example: ``` r the_data <- @@ -25,12 +35,16 @@ the_data <- head(100) ``` -Four operations are performed to arrive at the desired data set, and they are written in a natural order: the same as the order of execution. Also, no temporary variables are needed. If yet another operation is required, it is straight-forward to add to the sequence of operations wherever it may be needed. +Four operations are performed to arrive at the desired data set, and +they are written in a natural order: the same as the order of execution. +Also, no temporary variables are needed. If yet another operation is +required, it is straight-forward to add to the sequence of operations +wherever it may be needed. -If you are new to magrittr, the best place to start is the [pipes chapter](http://r4ds.had.co.nz/pipes.html) in R for data science. +If you are new to magrittr, the best place to start is the [pipes +chapter](http://r4ds.had.co.nz/pipes.html) in R for data science. -Installation ------------- +## Installation ``` r # The easiest way to get magrittr is to install the whole tidyverse: @@ -44,35 +58,45 @@ install.packages("magrittr") devtools::install_github("tidyverse/magrittr") ``` -Usage ------ +## Usage ### Basic piping -- `x %>% f` is equivalent to `f(x)` -- `x %>% f(y)` is equivalent to `f(x, y)` -- `x %>% f %>% g %>% h` is equivalent to `h(g(f(x)))` + - `x %>% f` is equivalent to `f(x)` + - `x %>% f(y)` is equivalent to `f(x, y)` + - `x %>% f %>% g %>% h` is equivalent to `h(g(f(x)))` -Here, "equivalent" is not technically exact: evaluation is non-standard, and the left-hand side is evaluated before passed on to the right-hand side expression. However, in most cases this has no practical implication. +Here, “equivalent” is not technically exact: evaluation is non-standard, +and the left-hand side is evaluated before passed on to the right-hand +side expression. However, in most cases this has no practical +implication. ### The argument placeholder -- `x %>% f(y, .)` is equivalent to `f(y, x)` -- `x %>% f(y, z = .)` is equivalent to `f(y, z = x)` + - `x %>% f(y, .)` is equivalent to `f(y, x)` + - `x %>% f(y, z = .)` is equivalent to `f(y, z = x)` ### Re-using the placeholder for attributes -It is straight-forward to use the placeholder several times in a right-hand side expression. However, when the placeholder only appears in a nested expressions magrittr will still apply the first-argument rule. The reason is that in most cases this results more clean code. +It is straight-forward to use the placeholder several times in a +right-hand side expression. However, when the placeholder only appears +in a nested expressions magrittr will still apply the first-argument +rule. The reason is that in most cases this results more clean code. -`x %>% f(y = nrow(.), z = ncol(.))` is equivalent to `f(x, y = nrow(x), z = ncol(x))` +`x %>% f(y = nrow(.), z = ncol(.))` is equivalent to `f(x, y = nrow(x), +z = ncol(x))` -The behavior can be overruled by enclosing the right-hand side in braces: +The behavior can be overruled by enclosing the right-hand side in +braces: -`x %>% {f(y = nrow(.), z = ncol(.))}` is equivalent to `f(y = nrow(x), z = ncol(x))` +`x %>% {f(y = nrow(.), z = ncol(.))}` is equivalent to `f(y = nrow(x), z += ncol(x))` ### Building (unary) functions -Any pipeline starting with the `.` will return a function which can later be used to apply the pipeline to values. Building functions in magrittr is therefore similar to building other values. +Any pipeline starting with the `.` will return a function which can +later be used to apply the pipeline to values. Building functions in +magrittr is therefore similar to building other values. ``` r f <- . %>% cos %>% sin @@ -82,7 +106,11 @@ f <- function(.) sin(cos(.)) ### Pipe with exposition of variables -Many functions accept a data argument, e.g. `lm` and `aggregate`, which is very useful in a pipeline where data is first processed and then passed into such a function. There are also functions that do not have a data argument, for which it is useful to expose the variables in the data. This is done with the `%$%` operator: +Many functions accept a data argument, e.g. `lm` and `aggregate`, which +is very useful in a pipeline where data is first processed and then +passed into such a function. There are also functions that do not have a +data argument, for which it is useful to expose the variables in the +data. This is done with the `%$%` operator: ``` r iris %>% @@ -94,11 +122,12 @@ data.frame(z = rnorm(100)) %$% ts.plot(z) ``` -![](man/figures/exposition-1.png) +![](man/figures/exposition-1.png) ### Compound assignment pipe operations -There is also a pipe operator which can be used as shorthand notation in situations where the left-hand side is being "overwritten": +There is also a pipe operator which can be used as shorthand notation in +situations where the left-hand side is being “overwritten”: ``` r iris$Sepal.Length <- @@ -106,10 +135,13 @@ iris$Sepal.Length <- sqrt() ``` -To avoid the repetition of the left-hand side immediately after the assignment operator, use the `%<>%` operator: +To avoid the repetition of the left-hand side immediately after the +assignment operator, use the `%<>%` operator: ``` r iris$Sepal.Length %<>% sqrt ``` -This operator works exactly like `%>%`, except the pipeline assigns the result rather than returning it. It must be the first pipe operator in a longer chain. +This operator works exactly like `%>%`, except the pipeline assigns the +result rather than returning it. It must be the first pipe operator in a +longer chain. diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..c6c1438 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,45 @@ +# DO NOT CHANGE the "init" and "install" sections below + +# Download script file from GitHub +init: + ps: | + $ErrorActionPreference = "Stop" + Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" + Import-Module '..\appveyor-tool.ps1' + +install: + ps: Bootstrap + +cache: + - C:\RLibrary + +# Adapt as necessary starting from here + +build_script: + - travis-tool.sh install_deps + +test_script: + - travis-tool.sh run_tests + +on_failure: + - 7z a failure.zip *.Rcheck\* + - appveyor PushArtifact failure.zip + +artifacts: + - path: '*.Rcheck\**\*.log' + name: Logs + + - path: '*.Rcheck\**\*.out' + name: Logs + + - path: '*.Rcheck\**\*.fail' + name: Logs + + - path: '*.Rcheck\**\*.Rout' + name: Logs + + - path: '\*_*.tar.gz' + name: Bits + + - path: '\*_*.zip' + name: Bits diff --git a/man/figures/exposition-1.png b/man/figures/exposition-1.png index 2c693ed..f78ec70 100644 Binary files a/man/figures/exposition-1.png and b/man/figures/exposition-1.png differ