Skip to content
17 changes: 10 additions & 7 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -1403,15 +1403,18 @@ replace_dot_alias = function(e) {
}

if (!is.null(lhs)) {
# We only need to check if we are creating new columns.
newnames = setdiff(lhs, names(x))
if (length(newnames) > 0) {
if (is.function(jval)) {
stopf("RHS of `:=` is a function. To create a new column of functions, it must be a list column (e.g., wrap in `list()`).")
} else if (is.list(jval) && length(jval) == 1 && is.function(jval[[1L]])) {
stopf("RHS of `:=` is a length-1 list containing a function. `data.table` does not automatically recycle lists. To create a list-column, use `rep(list(myfun), .N)` to match the number of rows.")
if (length(cols) == 1L) {
is_new_col <- cols > ncol(x)
if (is_new_col || !is.list(x[[cols]])) {
if (is.function(jval)) {
stopf("RHS of `:=` is a function. To create a new column of functions or assign to a non-list-column, it must be wrapped in a list(), e.g., `:= list(myfun)`.")
}
if (is.list(jval) && length(jval) == 1L && is.function(jval[[1L]]) && nrow(x) > 1L) {
stopf("RHS of `:=` is a length-1 list containing a function. `data.table` does not automatically recycle lists. To create a list-column, use `rep(list(myfun), .N)`.")
Copy link
Member

Choose a reason for hiding this comment

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

this is incorrect.

> data.table(x=1:2, L=list(mean))
       x             L
   <int>        <list>
1:     1 <function[1]>
2:     2 <function[1]>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi
i updated the condition and the tests, can you please review if something is missing/wrong ,when you have time.
thank you.

}
}
}
newnames = setdiff(lhs, names(x))
# 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
7 changes: 6 additions & 1 deletion inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -21161,4 +21161,9 @@ test(2317.9, DT1[DF2, on='a', .(e = x.a + i.e)]$e, 6)
DT = data.table(x = 1:3)

test(2318.1, DT[, y := mean], error = "RHS of `:=` is a function")
test(2318.2, DT[, y := list(mean)], error = "RHS of `:=` is a length-1 list containing a function")
test(2318.2, DT[, y := list(mean)], error = "RHS of `:=` is a length-1 list containing a function")
f = mean
test(2318.3, DT[, y := f], error = "RHS of `:=` is a function")
test(2318.4, DT[, y := identity(mean)], error = "RHS of `:=` is a function")
Copy link
Member

Choose a reason for hiding this comment

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

RHS is not a function here

test(2318.5, data.table(x = 1)[, y := mean], error = "RHS of `:=` is a function")
test(2318.6, data.table(x = 1:2)[, y := mean], error = "RHS of `:=` is a function")
Loading