Skip to content

Commit

Permalink
add locum
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMount committed Sep 28, 2019
1 parent c511d8a commit 0374439
Show file tree
Hide file tree
Showing 138 changed files with 2,283 additions and 1,452 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: wrapr
Type: Package
Title: Wrap R Tools for Debugging and Parametric Programming
Version: 1.8.9
Date: 2019-07-24
Version: 1.9.0
Date: 2019-09-28
Authors@R: c(
person("John", "Mount", email = "[email protected]", role = c("aut", "cre")),
person("Nina", "Zumel", email = "[email protected]", role = c("aut")),
Expand Down
103 changes: 16 additions & 87 deletions Examples/Locum/Locum.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,16 @@ to both be an effective [`R`](https://www.r-project.org) function application pi
pipe effects.

Let's take a look at implementing a new effect from a user perspective. The idea we want to implement is delayed evaluation through a
collecting object we call a "locum" or stand-in. The locum is intended to collect operations without executing them, for later use.
collecting object we call a "locum" or stand-in. The locum is intended to collect operations without executing them, for later use. This is similar to a lambda or function abstraction. The [code is now in the `wrapr` package](https://github.com/WinVector/wrapr/blob/master/R/locum.R), but could be implemented by any user as it uses only public or semi-public `wrapr` interfaces.

Let's start loading the `wrapr` package and defining our new `locum` class and its display methods.
Let's start loading the `wrapr` package.

```{r}
library(wrapr)
locum <- function() {
locum <- list(stages = list())
class(locum) <- 'locum'
return(locum)
}
format.locum <- function(x, ...) {
args <- list(...)
locum <- x
start_name <- 'locum()'
if('start' %in% names(args)) {
start_name <- format(args[['start']])
}
stage_strs <- vapply(
locum$stages,
function(si) {
format(si$pipe_right_arg)
}, character(1))
stage_strs <- c(list(start_name),
stage_strs)
return(paste(stage_strs,
collapse = " %.>%\n "))
}
as.character.locum <- function(x, ...) {
return(format(x, ...))
}
print.locum <- function(x, ...) {
cat(format(x, ...))
}
```

Using the `wrapr` `apply_left` interface we can define an object that collects pipe stages from the left.

```{r}
apply_left.locum <- function(
pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
locum <- pipe_left_arg
capture <- list(
pipe_right_arg = force(pipe_right_arg),
pipe_environment = force(pipe_environment),
left_arg_name = force(left_arg_name),
pipe_string = force(pipe_string),
right_arg_name = force(right_arg_name))
locum$stages <- c(locum$stages, list(capture))
return(locum)
}
```

We can now use the `locum` to collect the operations, and then print them.
We can use the `locum` to collect the operations, and then print them.

```{r}
y <- 4
Expand All @@ -86,46 +29,22 @@ p <- locum() %.>%
print(p)
```

We can use `wrapr`'s `apply_right` interface to define how the `locum` itself operates on values.

```{r}
apply_right.locum <- function(
pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
force(pipe_left_arg)
locum <- pipe_right_arg
for(s in locum$stages) {
pipe_left_arg <- pipe_impl(
pipe_left_arg,
s$pipe_right_arg,
s$pipe_environment,
pipe_string = s$pipe_string)
}
return(pipe_left_arg)
}
```

And we can now replace the `locum` with the value we want to apply the pipeline to.
We can now replace the `locum` with the value we want to apply the pipeline to.

```{r}
5 %.>% p
```

This yields the same answer as the following function application.


```{r}
atan2(cos(sin(5)), 4)
```

We can also add later intended arguments to the pipeline formatting.

```{r}
print(p, 'start' = 5)
print(p, 'start' = 4)
```

We can do some fun things, such as combining `locum` pipelines.
Expand All @@ -134,10 +53,20 @@ We can do some fun things, such as combining `locum` pipelines.
p1 <- locum() %.>% sin(.)
p2 <- locum() %.>% cos(.)
p1 %.>% p2
p12 <- p1 %.>% p2
p12
```

```{r}
4 %.>% p12
```

```{r}
cos(sin(4))
```



The idea is: `wrapr` dot arrow pipe is designed for expansion through the `apply_right`, `apply_left` `S3` interfaces, and the `apply_right_S4` `S4` interface. And users can access the implementation through the `pipe_impl` function. [This example](https://github.com/WinVector/wrapr/blob/master/Examples/Locum/Locum.md) and [the formal article](https://journal.r-project.org/archive/2018/RJ-2018-042/index.html) should give users a good place to start. [`rquery`](https://github.com/WinVector/rquery), [`rqdatatable`](https://github.com/WinVector/rqdatatable), and [`cdata`](https://github.com/WinVector/cdata) already use the extension interfaces to implement interesting features.


116 changes: 24 additions & 92 deletions Examples/Locum/Locum.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,19 @@ effects.
Let’s take a look at implementing a new effect from a user perspective.
The idea we want to implement is delayed evaluation through a collecting
object we call a “locum” or stand-in. The locum is intended to collect
operations without executing them, for later use.
operations without executing them, for later use. This is similar to a
lambda or function abstraction. The [code is now in the `wrapr`
package](https://github.com/WinVector/wrapr/blob/master/R/locum.R), but
could be implemented by any user as it uses only public or semi-public
`wrapr` interfaces.

Let’s start loading the `wrapr` package and defining our new `locum`
class and its display methods.
Let’s start loading the `wrapr` package.

``` r
library(wrapr)


locum <- function() {
locum <- list(stages = list())
class(locum) <- 'locum'
return(locum)
}


format.locum <- function(x, ...) {
args <- list(...)
locum <- x
start_name <- 'locum()'
if('start' %in% names(args)) {
start_name <- format(args[['start']])
}
stage_strs <- vapply(
locum$stages,
function(si) {
format(si$pipe_right_arg)
}, character(1))
stage_strs <- c(list(start_name),
stage_strs)
return(paste(stage_strs,
collapse = " %.>%\n "))
}


as.character.locum <- function(x, ...) {
return(format(x, ...))
}


print.locum <- function(x, ...) {
cat(format(x, ...))
}
```

Using the `wrapr` `apply_left` interface we can define an object that
collects pipe stages from the left.

``` r
apply_left.locum <- function(
pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
locum <- pipe_left_arg
capture <- list(
pipe_right_arg = force(pipe_right_arg),
pipe_environment = force(pipe_environment),
left_arg_name = force(left_arg_name),
pipe_string = force(pipe_string),
right_arg_name = force(right_arg_name))
locum$stages <- c(locum$stages, list(capture))
return(locum)
}
```

We can now use the `locum` to collect the operations, and then print
them.
We can use the `locum` to collect the operations, and then print them.

``` r
y <- 4
Expand All @@ -96,31 +39,7 @@ print(p)
## cos(.) %.>%
## atan2(., y)

We can use `wrapr`’s `apply_right` interface to define how the `locum`
itself operates on values.

``` r
apply_right.locum <- function(
pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
force(pipe_left_arg)
locum <- pipe_right_arg
for(s in locum$stages) {
pipe_left_arg <- pipe_impl(
pipe_left_arg,
s$pipe_right_arg,
s$pipe_environment,
pipe_string = s$pipe_string)
}
return(pipe_left_arg)
}
```

And we can now replace the `locum` with the value we want to apply the
We can now replace the `locum` with the value we want to apply the
pipeline to.

``` r
Expand All @@ -140,10 +59,10 @@ atan2(cos(sin(5)), 4)
We can also add later intended arguments to the pipeline formatting.

``` r
print(p, 'start' = 5)
print(p, 'start' = 4)
```

## 5 %.>%
## 4 %.>%
## sin(.) %.>%
## cos(.) %.>%
## atan2(., y)
Expand All @@ -154,13 +73,26 @@ We can do some fun things, such as combining `locum` pipelines.
p1 <- locum() %.>% sin(.)
p2 <- locum() %.>% cos(.)

p1 %.>% p2
p12 <- p1 %.>% p2
p12
```

## locum() %.>%
## sin(.) %.>%
## cos(.)

``` r
4 %.>% p12
```

## [1] 0.7270351

``` r
cos(sin(4))
```

## [1] 0.7270351

The idea is: `wrapr` dot arrow pipe is designed for expansion through
the `apply_right`, `apply_left` `S3` interfaces, and the
`apply_right_S4` `S4` interface. And users can access the implementation
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# wrapr 1.9.0 2019-09-28

* Add locum ( https://github.com/WinVector/wrapr/blob/master/Examples/Locum/Locum.md ).

# wrapr 1.8.9 2019-07-24

* More unit tests.
Expand Down
32 changes: 19 additions & 13 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@

### Windows

devtools::check_win_devel()
* using R Under development (unstable) (2019-07-05 r76784)
* using platform: x86_64-w64-mingw32 (64-bit)
Status: OK

rhub::check_for_cran()
507#> * using R Under development (unstable) (2019-09-18 r77193)
508#> * using platform: x86_64-w64-mingw32 (64-bit)
509#> * using session charset: ISO8859-1
510#> * using option '--as-cran'
511#> * checking for file 'wrapr/DESCRIPTION' ... OK
512#> * checking extension type ... Package
513#> * this is package 'wrapr' version '1.9.0'
514#> * package encoding: UTF-8
515#> * checking CRAN incoming feasibility ... Note_to_CRAN_maintainers
516#> Maintainer: 'John Mount '
558#> * checking sizes of PDF files under 'inst/doc' ... NOTE
559#> Unable to find GhostScript executable to run checks on size reduction
574#> Status: 1 NOTE
Note: is property of the check installation, not the package.

### MacOS

R CMD check --as-cran wrapr_1.8.9.tar.gz
R CMD check --as-cran wrapr_1.9.0.tar.gz
* using R version 3.6.0 (2019-04-26)
* using platform: x86_64-apple-darwin15.6.0 (64-bit)
* using session charset: UTF-8
* using option ‘--as-cran’
* checking for file ‘wrapr/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘wrapr’ version ‘1.8.9
* this is package ‘wrapr’ version ‘1.9.0
* package encoding: UTF-8
* checking CRAN incoming feasibility ... Note_to_CRAN_maintainers
Maintainer: ‘John Mount <[email protected]>’
* checking top-level files ... WARNING
Conversion of ‘README.md’ failed:
pandoc: Could not fetch https://www.r-pkg.org/badges/version/wrapr
TlsException (HandshakeFailed (Error_Protocol ("expecting server hello, got alert : [(AlertLevel_Fatal,HandshakeFailure)]",True,HandshakeFailure)))
Status: 1 WARNING
Link WARNING is spurious, URL https://www.r-pkg.org/badges/version/wrapr is correct and working.
Status: OK

## Downstream dependencies

Expand Down
Loading

0 comments on commit 0374439

Please sign in to comment.