various errors when RHS of := is closure/function

Aperta
#5,829 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
r
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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"

Lingua principale
R
Stelle
3.9k
Fork
1.1k
Merge medio
14h 4m
PR unite (30g)
4

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Rdatatable/data.table

Tutte le issue di Rdatatable/data.table

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.