Something really odd is going on here. In the code below, I create a variable called temp
. I have to call it twice before I can see what it is. E.g. The first time I call it, the console shows nothing. The second time it shows the data.table
/data.frame
that it is. Can anyone help me understand what's going on here?
library(magrittr)
library(data.table)
myDT <- as.data.table(mtcars)
temp <-
myDT %>%
melt(id.vars = c('cyl', 'mpg', 'hp'),
measure.vars = c('vs','am','gear','carb'),
variable.name = 'Data') %>%
extract( value > 0) %>%
extract( , value := NULL)
What my console is doing (the first call doesn't do anything):
> temp
> temp
cyl mpg hp Data
1: 4 22.8 93 vs
2: 6 21.4 110 vs
3: 6 18.1 105 vs
4: 4 24.4 62 vs
5: 4 22.8 95 vs
...
...
This is the known side-effect of the fix implemented to squash an even bigger bug. It's documented here, as the first item under "BUG FIXES" section of the v1.9.6 release. Quoting from that link:
if (TRUE) DT[,LHS:=RHS] no longer prints, #869 and #1122. Tests added. To get this to work we've had to live with one downside: if a := is used inside a function with no DT[] before the end of the function, then the next time DT or print(DT) is typed at the prompt, nothing will be printed. A repeated DT or print(DT) will print. To avoid this: include a DT[] after the last := in your function. If that is not possible (e.g., it's not a function you can change) then DT[] at the prompt is guaranteed to print. As before, adding an extra [] on the end of a := query is a recommended idiom to update and then print; e.g. > DT[,foo:=3L][]. Thanks to Jureiss and Jan Gorecki for reporting.
As explained there, the solution is to append a trailing []
to the the final :=
-containing operation in your function. Here, that would mean doing the following:
library(magrittr)
library(data.table)
myDT <- as.data.table(mtcars)
temp <-
myDT %>%
melt(id.vars = c('cyl', 'mpg', 'hp'),
measure.vars = c('vs','am','gear','carb'),
variable.name = 'Data') %>%
extract( value > 0) %>%
extract( , value := NULL) %>% `[`
## Following which, this will print the first time
temp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With