data.table's := syntax has a side-effect of messing with printing (see Bug Fixes in data.table 1.9.6):
suppressPackageStartupMessages(library(data.table))
z <- data.table(a = 1)[, b := 1]
z
z
#> a b
#> 1: 1 1
Created on 2020-01-29 by the reprex package (v0.3.0)
Note how the first time we ask to print z, nothing happens. Only after the second print does anything happen.
The solution is to add a final [] to the query:
suppressPackageStartupMessages(library(data.table))
z <- data.table(a = 1)[, b := 1][]
z
#> a b
#> 1: 1 1
This behavior is also present in dtplyr whenever a mutate is the last verb:
suppressPackageStartupMessages({
library(data.table)
library(dplyr)
library(dtplyr)
})
z <- data.table(a = 1) %>%
lazy_dt() %>%
mutate(b = 1) %>%
as.data.table()
z
z
#> a b
#> 1: 1 1
An obvious workaround is to add a select(everything()) after the mutate.
While this is arguably best resolved from the data.table side, it isn't consistent with dplyr, where such a query should print from the get-go. In order to facilitate potential migration from dplyr to dtplyr, it'd be nice to solve this here.
I'm assuming the performance impact of DT[] is negligible, so a trivial solution would be to append a [] at the end of all translations, though a more elegant solution (if plausible?) would only do so when the last verb is a mutate.
data.table's
:=syntax has a side-effect of messing with printing (see Bug Fixes in data.table 1.9.6):Created on 2020-01-29 by the reprex package (v0.3.0)
Note how the first time we ask to print
z, nothing happens. Only after the second print does anything happen.The solution is to add a final
[]to the query:This behavior is also present in
dtplyrwhenever amutateis the last verb:suppressPackageStartupMessages({ library(data.table) library(dplyr) library(dtplyr) }) z <- data.table(a = 1) %>% lazy_dt() %>% mutate(b = 1) %>% as.data.table() z z #> a b #> 1: 1 1An obvious workaround is to add a
select(everything())after the mutate.While this is arguably best resolved from the
data.tableside, it isn't consistent withdplyr, where such a query should print from the get-go. In order to facilitate potential migration fromdplyrtodtplyr, it'd be nice to solve this here.I'm assuming the performance impact of
DT[]is negligible, so a trivial solution would be to append a[]at the end of all translations, though a more elegant solution (if plausible?) would only do so when the last verb is amutate.