Skip to content

feat : added condition to check the RHS of := #7161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@

12. `print(..., col.names = 'none')` now correctly adapts column widths to the data content, ignoring the original column names and producing a more compact output, [#6882](https://github.com/Rdatatable/data.table/issues/6882). Thanks to @brooksambrose for the report and @venom1204 for the PR.

13. 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.

### NOTES

1. The following in-progress deprecations have proceeded:
Expand Down
8 changes: 7 additions & 1 deletion R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,13 @@ replace_dot_alias = function(e) {
}

if (!is.null(lhs)) {
# TODO?: use set() here now that it can add new columns. Then remove newnames and alloc logic above.
newnames = setdiff(lhs, names(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this assignment and the one above?

newnames=setdiff(lhs, names_x)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both compute setdiff(lhs, names(x)), but the one above uses the cached names_x. I’ll update the later one to use names_x as well for consistency and to avoid unnecessary recomputation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i don't understand, why recalculate newnames at all if it's just the same value as above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks. The second newnames assignment was redundant. I will take care of it.

if (length(newnames) > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this error not happen for the case of overwriting an existing column?

if (is.function(jval)) {
Copy link
Member

@MichaelChirico MichaelChirico Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will catch the following case (which you should add as a new regression test):

DT=data.table(a=1:10)
DT[,c('a', 'b'):=.(1:10, mean)]

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()`).")
}
}
# TODO?: use set() here now that it can add new columns.Then remove newnames and alloc logic above.
.Call(Cassign,x,irows,cols,newnames,jval)
return(suppPrint(x))
}
Expand Down
5 changes: 5 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -21426,3 +21426,8 @@ test(2330.7, as.data.table(list(z), keep.rownames=TRUE), data.table(rn=rep("", 3

M <- matrix(1:6, nrow=3, dimnames=list(rep("", 3), c("V1", "V2"))) # test of list(M) for empty-rowname'd matrix input
test(2330.8, as.data.table(list(M), keep.rownames=TRUE), data.table(rn=rep("", 3), V1=1:3, V2=4:6))

#5829 checking the RHS of :=
DT = data.table(x = 1:3)
test(2331.1, DT[, y := mean], error = "RHS of `:=` is a function")
test(2331.2, DT[, y := function(x) x], error = "RHS of `:=` is a function")
Loading