Skip to content

Commit

Permalink
Manual wrap two painful blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 4, 2019
1 parent 5768f0f commit 610cebd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Evaluation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ There are two basic ways to overcome this challenge:
\@ref(non-standard-ast)). For modelling functions this means that the
captured call is suboptimal:

```{r}
```{r, eval = FALSE}
resample_lm1 <- function(formula, data, env = caller_env()) {
formula <- enexpr(formula)
resample_data <- resample(data, n = nrow(data))
Expand All @@ -1003,6 +1003,10 @@ There are two basic ways to overcome this challenge:
eval(lm_call, env)
}
resample_lm1(y ~ x, data = df)$call
#> lm(y ~ x, data = <data.frame>)
#> lm(formula = y ~ x, data = list(x = c(3L, 7L, 4L, 4L,
#> 2L, 7L, 2L, 1L, 8L, 9L), y = c(13.21, 27.04, 18.63,
#> 18.63, 10.99, 27.04, 10.99, 7.83, 28.14, 32.72)))
```

1. Alternatively you can create a new environment that inherits from the
Expand All @@ -1028,14 +1032,16 @@ There are two basic ways to overcome this challenge:

1. Why does this function fail?

```{r, error = TRUE}
```{r, eval = FALSE}
lm3a <- function(formula, data) {
formula <- enexpr(formula)
lm_call <- expr(lm(!!formula, data = data))
eval(lm_call, caller_env())
}
lm3a(mpg ~ disp, mtcars)$call
#> Error in as.data.frame.default(data, optional = TRUE):
#> cannot coerce class ‘"function"’ to a data.frame
```

1. When model building, typically the response and data are relatively
Expand Down

0 comments on commit 610cebd

Please sign in to comment.