Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jun 22, 2018
1 parent 8cb09ca commit 2c4b54e
Showing 1 changed file with 49 additions and 15 deletions.
64 changes: 49 additions & 15 deletions 12-caution.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,70 @@ You can call `make(..., timeout = 10)` to time out all each target after 10 seco

## Dependencies

### Dependency objects containing functions may change unexpectedly
### Objects that contain functions may trigger unwanted builds

For example, an `R6` class changes whenever a new `R6` object of that class is created.

```{r r6change11}
library(digest)
library(R6)
example_class <- R6Class(
"example_class",
private = list(data = list()),
circle_class <- R6Class(
"circle_class",
private = list(radius = NULL),
public = list(
initialize = function(data = list()) {
private$data <- data
initialize = function(radius){
private$radius <- radius
},
area = function(){
pi * private$radius ^ 2
}
)
)
digest(example_class)
example_object <- example_class$new(data = 1234)
digest(example_class) # example_class changed
digest(circle_class)
circle <- circle_class$new(radius = 5)
digest(circle_class) # example_class changed
rm(circle)
```

`Drake` detects this type strange change and unavoidably rebuilds targets.
Ordinarily, `drake` overreacts to this change and builds targets repeatedly.

```{r r6rebuild11, eval = FALSE}
plan <- drake_plan(example_target = example_class$new(1234))
make(plan) # `example_class` changes because it is referenced.
make(plan) # Builds `example_target` again because `example_class` changed.
```{r r6rebuild11}
clean()
plan <- drake_plan(
circle = circle_class$new(radius = 10),
area = circle$area()
)
make(plan) # `circle_class` changes because it is referenced.
make(plan) # Builds `circle` again because `circle_class` changed.
```

The same behavior affects objects containing functions more broadly, not just `R6` classes. For more on this edge case, see [issue 345](https://github.com/ropensci/drake/issues/345).
The solution is to define your `R6` class inside a function. `drake` does the right thing when it comes to tracking changes to functions.


```{r r6build2}
clean()
new_circle <- function(radius){
circle_class <- R6Class(
"circle_class",
private = list(radius = NULL),
public = list(
initialize = function(radius){
private$radius <- radius
},
area = function(){
pi * private$radius ^ 2
}
)
)
circle_class$new(radius = radius)
}
plan <- drake_plan(
circle = new_circle(radius = 10),
area = circle$area()
)
make(plan)
make(plan)
```


### The `magrittr` dot (`.`) is always ignored.
Expand Down

0 comments on commit 2c4b54e

Please sign in to comment.