Skip to content

Commit 698c557

Browse files
committed
extend description of an issue from the past
1 parent e7199c0 commit 698c557

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

vignettes/datatable-programming.Rmd

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,43 @@ print(j)
397397
DT[, j, env = list(j = j)]
398398
```
399399

400-
### Common mistakes
400+
### Good practices
401+
402+
When using meta-programming interfaces, not only data.table `env` argument, user is also in control of how call stack will look like. We control what will go into errors call stack, or extra call stack reporting routines. It is either reference to named function or it an anonymous function.
403+
404+
```{r report_from_where_and_add_one, eval = FALSE}
405+
report_from_where_and_add_one = function(x) {
406+
print(sys.calls())
407+
invisible(x + 1L)
408+
}
409+
do.call(report_from_where_and_add_one, list(x = 1L))
410+
# [[1]]
411+
# do.call(report_from_where_and_add_one, list(x = 1L))
412+
413+
# [[2]]
414+
# (function(x) {
415+
# print(sys.calls())
416+
# x + 1L
417+
# })(x = 1L)
418+
do.call("report_from_where_and_add_one", list(x = 1L))
419+
# [[1]]
420+
# do.call("report_from_where_and_add_one", list(x = 1L))
421+
422+
# [[2]]
423+
# report_from_where_and_add_one(x = 1L)
424+
```
425+
426+
```{r raise_exception, eval = FALSE}
427+
raise_exception = function(x) {
428+
stop("a1")
429+
}
430+
do.call(raise_exception, list(x = 1L))
431+
# Error in (function (x) : a1
432+
do.call("raise_exception", list(x = 1L))
433+
# Error in raise_exception(x = 1L) : a1
434+
```
435+
436+
And data.table use case...
401437

402438
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.
403439

0 commit comments

Comments
 (0)