various errors when RHS of := is closure/function

Open
#5,829 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the closure assignments shown for one, two, and 101 rows, then inspect the := assignment and data.table print paths involved in the reported errors. Done should mean closure RHS values are handled consistently, with a clear error before malformed columns or inconsistent printing occur.

Written by the indexing model from the issue text.

Description

beginner-task documentation message

Hi! I encountered an error today (invalid type/length) which was informative enough for me to figure out and fix my issue (providing a closure/function on RHS of :=), but I wonder if we should recover more gracefully, for the benefit of newbies? In the code below we see that the assignment errors, and then there is a new column with value NULL, which causes a "malformed" error:

> myDT=data.table(x=1:101)
> myDT
         x
     <int>
  1:     1
  2:     2
  3:     3
  4:     4
  5:     5
 ---      
 97:    97
 98:    98
 99:    99
100:   100
101:   101
> myDT[, y := data.table]
Error in `[.data.table`(myDT, , `:=`(y, data.table)) : 
  invalid type/length (closure/101) in vector allocation
> myDT
Error in `[.data.table`(x, i, , ) : 
  Column 2 is NULL; malformed data.table.
> str(myDT)
Classes 'data.table' and 'data.frame':	101 obs. of  2 variables:
 $ x: int  1 2 3 4 5 6 7 8 9 10 ...
 $ y: NULL
 - attr(*, ".internal.selfref")=<externalptr> 

This malformed error message does not happen when there are fewer rows, because the more complex print logic is not called, see below:

> myDT2=data.table(x=1:2)
> myDT2[, y := data.table]
Error in `[.data.table`(myDT2, , `:=`(y, data.table)) : 
  invalid type/length (closure/2) in vector allocation
> myDT2
       x      y
   <int> <NULL>
1:     1   NULL
2:     2   NULL

When there is 1 row, the assignment succeeds (actually assigning the closure/function rather than NULL), but the print errors, see below:

> myDT1=data.table(x=1)
> myDT1[, y := data.table]
> myDT1
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent
> str(myDT1)
Classes 'data.table' and 'data.frame':	1 obs. of  2 variables:
 $ x: num 1
 $ y:function (..., keep.rownames = FALSE, check.names = FALSE, key = NULL, stringsAsFactors = FALSE)  
 - attr(*, ".internal.selfref")=<externalptr> 

Overall I would have expected more consistent and user-friendly behavior from := -- before assigning a new column (with value NULL or otherwise) would be great to stop early with an error message like "RHS of := is closure, which is almost certainly a mistake. If you really want to store a function in a data table, then please put it in a list column"

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.