Skip to content

Commit cfcd238

Browse files
committed
enhanced error message
1 parent 2f0d12f commit cfcd238

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
10. `data.table()` and `as.data.table()` with `keep.rownames=TRUE` now extract row names from named vectors, matching `data.frame()` behavior. Names from the first named vector in the input are used to create the row names column (default name `"rn"` or custom name via `keep.rownames="column_name"`), [#1916](https://github.com/Rdatatable/data.table/issues/1916). Thanks to @richierocks for the feature request and @Mukulyadav2004 for the implementation.
4848

49+
11. Assigning a raw function to a new column, e.g. `DT[, new_col := mean]`, now throws an informative error. This prevents a common mistake and guides the user to wrap the function in a `list()`, e.g. `DT[, new_col := list(mean)]`, if the intent is to create a list-column of functions. Thanks to @tdhock for the report and @venom1204 for the fix.
50+
4951
### BUG FIXES
5052

5153
1. `fread()` no longer warns on certain systems on R 4.5.0+ where the file owner can't be resolved, [#6918](https://github.com/Rdatatable/data.table/issues/6918). Thanks @ProfFancyPants for the report and PR.

R/data.table.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,13 @@ replace_dot_alias = function(e) {
14111411
}
14121412

14131413
if (!is.null(lhs)) {
1414-
# TODO?: use set() here now that it can add new columns. Then remove newnames and alloc logic above.
1414+
newnames = setdiff(lhs, names(x))
1415+
if (length(newnames) > 0) {
1416+
if (is.function(jval)) {
1417+
stopf("RHS of `:=` is a function. To create a new column of functions, it must be a list column (e.g., wrap the function in `list()`).")
1418+
}
1419+
}
1420+
# TODO?: use set() here now that it can add new columns.Then remove newnames and alloc logic above.
14151421
.Call(Cassign,x,irows,cols,newnames,jval)
14161422
return(suppPrint(x))
14171423
}

inst/tests/tests.Rraw

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21426,3 +21426,8 @@ test(2330.7, as.data.table(list(z), keep.rownames=TRUE), data.table(rn=rep("", 3
2142621426

2142721427
M <- matrix(1:6, nrow=3, dimnames=list(rep("", 3), c("V1", "V2"))) # test of list(M) for empty-rowname'd matrix input
2142821428
test(2330.8, as.data.table(list(M), keep.rownames=TRUE), data.table(rn=rep("", 3), V1=1:3, V2=4:6))
21429+
21430+
#5829 checking the RHS of :=
21431+
DT = data.table(x = 1:3)
21432+
test(2331.1, DT[, y := mean], error = "RHS of `:=` is a function")
21433+
test(2331.2, DT[, y := list(mean)], error = "RHS of `:=` is a length-1 list containing a function")

0 commit comments

Comments
 (0)