-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
data.table not compatible with ggplot when it was generated from empty data.table #4597
Comments
Thanks for the report - I can reproduce. Even more minimal, the issue occurs without any library(data.table)
library(ggplot2)
dt = data.table()
dt[, x := seq(1,100)]
dt[, y := seq(100,1)]
ggplot(dt)
#> Error in `$<-.data.frame`(x, name, value): replacement has 1 row, data has 0 It appears that the main issue is that our row.names(dt)
# character(0)
setattr(dt, "row.names", 1:100)
ggplot(dt, aes(x, y)) + geom_point()
## success! It seems like we need to either prevent users from starting from an empty data.table or update row.names on assignment when the number of rows is greater than the current number of rows of dt. And while a ## 0 row data table with assignment
dt = data.table(x = integer())
dt[, y := 1:2][]
#Empty data.table (0 rows and 2 cols): x,y
## 1 row data.table with assignment
dt = data.table(x = 1L)
dt[, y := 1:2]
#Error in `[.data.table`(dt, , `:=`(y, 1:2)) :
# Supplied 2 items to be assigned to 1 items of column 'y'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
## 2 row data.table with length(0) assignment
dt = data.table(x = 1:2)
dt[, y := integer()][]
## x y
##1: 1 NA
## 2: 2 NA |
It looks like allowing a Lines 321 to 323 in a347623
The solution seems to be to set row names in assign when dt is a null.data.table. So my next step is figuring out how to do that in C. |
Hi,
I think I found a bug case where data.table does not work with ggplot.
Here's an example:
A workaround was
which doesn't seem convenient.
I'm not certain if it is coming from
data.table
orggplot
, forplot(dt)
just works fine, but I guessed it was fromdata.table
side becausesetDT
somehow resolves the issue. Or was I naive to use emptydata.table
from scratch?The text was updated successfully, but these errors were encountered: