Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add appveyor config file and badge to readme #170

Merged
merged 1 commit into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
^docs$
^README\.Rmd$
^README-.*\.png$
^appveyor\.yml$
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ library(magrittr)
# magrittr <img src="man/figures/logo.png" align="right" />

[![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
Expand Down
98 changes: 65 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->
magrittr <img src="man/figures/logo.png" align="right" />
=========================================================

[![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 <img src="man/figures/logo.png" align="right" />

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 <-
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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 %>%
Expand All @@ -94,22 +122,26 @@ 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 <-
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.
45 changes: 45 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -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
Binary file modified man/figures/exposition-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.