Skip to content

Removing (and adding) a column by reference on null data.table need not error #2335

@arunsrinivasan

Description

@arunsrinivasan
require(data.table) # CRAN stable at the moment, will update after testing from home on devel.
dt <- data.table()
dt[, col := NULL]
# Error in `[.data.table`(dt, , `:=`(col, NULL)) : 
#   Cannot use := to add columns to a null data.table (no columns), currently. You can use := 
#        to add (empty) columns to a 0-row data.table (1 or more empty columns), though.

I see this error mentioned in NEWS in 1.8.10! Didn't expect it to be that long ago. There's no issue number mentioned there.

Firstly I feel perhaps a warning would be sufficient instead of an error message?

And more importantly, removing a column on a null data.table could simply return the empty table without any error/warning, or for consistency, with a warning (assuming change to 'warning' goes through). Also the error/warning message should mention about 'removing' cols as well (minor).

The inconvenience with the way it is currently is that, when tables (/files) are being read off disk, and some tables don't exist, a null data.table is returned, and this error results in having an if() check everywhere before adding/removing cols by reference during downstream manipulations, which I find is unnecessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions