Skip to content

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

Description

@pnacht

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.

Activity

  1. hadley commented on Jan 25, 2021

    @hadley
    Member

    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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions