various errors when RHS of := is closure/function
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·