From fab93a93979997aae6066a219e6f3f2c7168e707 Mon Sep 17 00:00:00 2001 From: Nitish Jha <151559388+Nj221102@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:57:01 +0530 Subject: [PATCH] Export masks for NSE-only constructs `.`, `J`, `patterns` and `measure` (#6125) * masking function * Update data.table.R * Removed mask for pattern and measure * exporting pattern and measure * added news entry * editing news item * updating news * updating news item * updated news item * Update NEWS.md * Update NEWS.md * updating news * delete comment * updated test and mask for J * improved the error message * updated error messages * improving error message * Update NEWS.md Co-authored-by: Michael Chirico * small changes * Update vignettes/datatable-importing.Rmd Co-authored-by: Michael Chirico * Update NEWS.md Co-authored-by: Michael Chirico * grammar * updating error message * updating example --------- Co-authored-by: nitish jha Co-authored-by: Michael Chirico --- NAMESPACE | 2 +- NEWS.md | 2 ++ R/data.table.R | 8 ++++++++ inst/tests/tests.Rraw | 2 +- vignettes/datatable-importing.Rmd | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index b9872ee7e..08061952f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -27,7 +27,7 @@ export(tstrsplit) export(frank) export(frankv) export(address) -export(.SD,.N,.I,.GRP,.NGRP,.BY,.EACHI) +export(.SD,.N,.I,.GRP,.NGRP,.BY,.EACHI, ., J, measure, patterns) export(rleid) export(rleidv) export(rowid) diff --git a/NEWS.md b/NEWS.md index 42af53f91..af87e3eeb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -100,6 +100,8 @@ 16. `print.data.table` gains new argument `show.indices` and option `datatable.show.indices` that allows the user to print a `data.table`'s indices as columns without having to modify the `data.table` itself. Thanks @MichaelChirico for the report and @joshhwuu for the PR. +17. `.`, `J`, `measure`, and `patterns` are now exported for use within `[` and `melt()`, for consistency with other NSE exports like `.N` and `:=`, [#5604](https://github.com/Rdatatable/data.table/issues/5604). Package developers can now import these names to avoid `R CMD check` `NOTE`s about them being undefined variables. Thanks to @MichaelChirico and @ylelkes for the suggestions and @Nj221102 for implementing. + ## TRANSLATIONS 1. Fix a typo in a Mandarin translation of an error message that was hiding the actual error message, [#6172](https://github.com/Rdatatable/data.table/issues/6172). Thanks @trafficfan for the report and @MichaelChirico for the fix. diff --git a/R/data.table.R b/R/data.table.R index 7975d2a3a..fcca49560 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -2770,6 +2770,14 @@ address = function(x) .Call(Caddress, eval(substitute(x), parent.frame())) stopf('Check that is.data.table(DT) == TRUE. Otherwise, :=, `:=`(...) and let(...) are defined for use in j, once only and in particular ways. See help(":=").') } +J = function(...) { + stopf("J() called outside of [.data.table. J() is only intended for use in i.") +} + +. = function(...) { + stopf(".() called outside of [.data.table. .() is only intended as an alias for list() inside DT[...].") +} + let = function(...) `:=`(...) setDF = function(x, rownames=NULL) { diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1329279a8..b0b37ec8a 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -2186,7 +2186,7 @@ if (ncol(DT)==2L) setnames(DT,c("A","B")) # else don't stop under torture with s test(714, DT[,z:=6:10], data.table(A=1:5,B=5,z=6:10)) # Test J alias is now removed outside DT[...] from v1.8.7 (to resolve rJava::J conflict) -test(715, J(a=1:3,b=4), error=base_messages$missing_function("J")) +test(715, J(a=1:3,b=4), error="J() called outside of [.data.table. J() is only intended for use in i.") # Test get in j DT = data.table(a=1:3,b=4:6) diff --git a/vignettes/datatable-importing.Rmd b/vignettes/datatable-importing.Rmd index 99dc7f5c2..9f5d089b9 100644 --- a/vignettes/datatable-importing.Rmd +++ b/vignettes/datatable-importing.Rmd @@ -118,7 +118,7 @@ aggr = function (x) { } ``` -The case for `data.table`'s special symbols (`.SD`, `.BY`, `.N`, `.I`, `.GRP`, `.NGRP`, and `.EACHI`; see `?.N`) and assignment operator (`:=`) is slightly different. You should import whichever of these values you use from `data.table`'s namespace to protect against any issues arising from the unlikely scenario that we change the exported value of these in the future, e.g. if you want to use `.N`, `.I`, and `:=`, a minimal `NAMESPACE` would have: +The case for `data.table`'s special symbols (e.g. `.SD` and `.N`) and assignment operator (`:=`) is slightly different (see `?.N` for more, including a complete listing of such symbols). You should import whichever of these values you use from `data.table`'s namespace to protect against any issues arising from the unlikely scenario that we change the exported value of these in the future, e.g. if you want to use `.N`, `.I`, and `:=`, a minimal `NAMESPACE` would have: ```r importFrom(data.table, .N, .I, ':=')