Skip to content

Commit a850eed

Browse files
extend env vignette for common trap, closes #6031 (#6923)
* extend env vignette for common trap, closes #6031 * Wording correction from Ben Co-authored-by: Benjamin Schwendinger <[email protected]> * Another wording improvement by Ben Co-authored-by: Benjamin Schwendinger <[email protected]> --------- Co-authored-by: Benjamin Schwendinger <[email protected]>
1 parent 1ac7c1b commit a850eed

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

vignettes/datatable-programming.Rmd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ print(j)
376376
DT[, j, env = list(j = j)]
377377
```
378378

379+
### Common mistakes
380+
381+
It is important to understand the difference between passing an object and a name that points to an object. See the verbose output of following examples.
382+
383+
```{r obj_vs_objname}
384+
DT[, fun(Petal.Width), env = list(fun = mean), verbose=TRUE]
385+
DT[, fun(Petal.Width), env = list(fun = "mean"), verbose=TRUE]
386+
```
387+
388+
Users will usually want to substitute the function name rather than inserting the actual function body. Therefore, the second approach (passing a character string) is often preferred.
389+
390+
In case of any doubts on the `env` interface functioning, set `verbose = TRUE` to inspect how expressions are resolved internally.
391+
379392
## Retired interfaces
380393

381394
In `[.data.table`, it is also possible to use other mechanisms for variable substitution or for passing quoted expressions. These include `get` and `mget` for inline injection of variables by providing their names as strings, and `eval` that tells `[.data.table` that the expression we passed into an argument is a quoted expression and that it should be handled differently. Those interfaces should now be considered retired and we recommend using the new `env` argument, instead.

0 commit comments

Comments
 (0)