nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
179 stars 18 forks source link

Check if input is data.frame #32

Closed ghost closed 8 years ago

ghost commented 8 years ago

I often work with data.frames and data.tables side by side as some commands written for a data.frame get a hickup with data.tables. So does dust(). So maybe a tiny input check for data.frames? Or for (accidentally) inputted data.table do just a quick conversion to data.frame? [Note: this might be related to the considerations in #6.]

DT = data.table(ID = c("b","b","b","a","a","c"), a = 1:6, b = 7:12, c=13:18)
dust(DT)
#> Error in `:=`(row, 1:.N): Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
df <- as.data.frame(DT)
dust(df)
#>   ID a  b  c
#> 1  b 1  7 13
#> 2  b 2  8 14
#> 3  b 3  9 15
#> 4  a 4 10 16
#> 5  a 5 11 17
#> 6  c 6 12 18
nutterb commented 8 years ago

Great catch. It seems that data.tables don't take well to dplyr::gather. I've included a line that will coerce any object that inherits data.table to a data frame. That should take care of it for you.

The patch is in the latex-tables branch. I'll move it up into the main branch when I get some more features put in. You should see this correction on CRAN around the middle of the month.

ghost commented 8 years ago

Great, thank you! [Think that is tidyr::gather (not dyplyr::gather)? Otherwise we should let Hadley know, because dplyr is supposed to work with data.tables too.]

nutterb commented 8 years ago

Yes, it should have been tidyr::gather, and after looking at it some more, it might actually be the combination of dplyr::mutate and data.table. See https://github.com/hadley/dplyr/issues/1431 and https://github.com/Rdatatable/data.table/issues/1366. I'm at a loss for what the issue might be, but for now, I guess I'll have to coerce to a data frame. I'll keep an eye on it so that sometime in the future I might be able to avoid the coercion.