Skip to content
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

Append a "select all" DT[] to translation if the last verb is mutate #146

Closed
pnacht opened this issue Jan 29, 2020 · 1 comment
Closed
Labels
feature a feature request or enhancement

Comments

@pnacht
Copy link

pnacht commented Jan 29, 2020

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.

@hadley hadley added the feature a feature request or enhancement label Apr 21, 2020
@hadley
Copy link
Member

hadley commented Jan 25, 2021

Minimal reprex:

library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

dt <- lazy_dt(data.frame(x = 1))
dt %>% mutate(b = 1) 
#> Source: local data table [?? x 2]
#> Call:   copy(`_DT1`)[, `:=`(b = 1)]
#> 
#>       x     b
#>   <dbl> <dbl>
#> 1     1     1
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt %>% mutate(b = 1) %>% data.table::as.data.table()

Created on 2021-01-25 by the reprex package (v0.3.0.9001)

Seems reasonable to just do this as part of as.data.table()

@hadley hadley closed this as completed in e39cd56 Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants