Skip to content

Mutated data.tables are not printed when enclosed in parentheses. #7360

Open

Description

@eliocamp

The walrus operator suppresses printing, but calling print explicitly does work.

library(data.table)
dt <- data.table(x = 1)

print(dt[, a := "a"])
#>        x      a
#>    <num> <char>
#> 1:     1      a

However, I woudl've assumed that enclosing the expression in parentheses would also work. It doesn't!

(dt[, a := "a"])

Created on 2025-10-05 with reprex v2.1.1

Activity

  1. jangorecki commented on Oct 5, 2025

    @jangorecki
    Member

    Agree this is how base R normally does.
    As a workaround for now you can use

    dt[, a := "a"][]

    Instead

  2. ben-schwen commented on Oct 25, 2025

    @ben-schwen
    Member

    Isn't (dt[, a := "a"]) just syntactic sugar for dt[, a := "a"]?

  3. jangorecki commented on Oct 25, 2025

    @jangorecki
    Member

    But it prints (a <- 1)

  4. aitap commented on Oct 25, 2025

    @aitap
    Member

    As far as R is concerned, unlike base R assignments like a <- 1, which are properly invisible, dt[, a := "a"] is not an assignment and thus is visible and must be printed:

    withVisible(dt[, a := "a"])
    # $value
    # 
    # $visible
    # [1] TRUE

    R then calls print() and dispatches to print.data.table(), which contains special code to refuse being auto-printed. To print.data.table, dt[, a := "a"] and (dt[, a := "a"]) look the same when auto-printing.

    Unfortunately, we also lack the ability to force the result of [.data.table to be invisible (see #6566 and links from there for more context).

  5. tdhock commented on Oct 27, 2025

    @tdhock
    Member

    "This is a feature not a bug!"
    we don't want to print for dt[, a := "a"], which silences printing, and is the same as (dt[, a := "a"]). It is normal/expected to have to add [] to get printing.

  6. jangorecki commented on Oct 27, 2025

    @jangorecki
    Member

    @tdhock but it is inconsistent to <- assignment which normally also does not print, unless wrapped in (

  7. MichaelChirico commented on Oct 28, 2025

    @MichaelChirico
    Member

    Isn't this related to #6631? After which adding [] to force print should be more rare?

  8. eliocamp commented on Oct 29, 2025

    @eliocamp
    ContributorAuthor

    From a user intent perspective, I would assume that (dt[, a := "a"]) indicates that the user wants the result to be printed, like if they did print(dt[, a := "a"]).

    From @aitap post before, it seems that it's simply not possible to distinguish between (dt[, a := "a"]) (which should print) and dt[, a := "a"] (which shouldn't print). If that's the case, then feel free to close this issue :).

  9. ecoRoland2 commented on Oct 29, 2025

    @ecoRoland2

    From @aitap post before, it seems that it's simply not possible to distinguish between (dt[, a := "a"]) (which should print) and dt[, a := "a"] (which shouldn't print).

    Is this documented somewhere?

    IMHO, print(dt[, a := "a"]) is much better coding style.

  10. MichaelChirico commented on Oct 29, 2025

    @MichaelChirico
    Member

    In fact, implicit_assignment_linter() lints this in the similar common use case of (x <- ...) "assign with print side-effect", though this has some pushback:

    r-lib/lintr#2916

    It is very common to find this in R's own manuals:

    grep -Er "^\(.*" ~/svn/r-devel --include=*.Rd | wc -l
    # 337
    

    And yes, it's documented in ?"(":

    (... has visibility set, so will auto-print if used at top-level.

  11. ecoRoland2 commented on Oct 29, 2025

    @ecoRoland2

    No, I mean if it is documented by data.table that ( doesn't auto-print when using := assignment.

  12. aitap commented on Nov 3, 2025

    @aitap
    Member

    The := assignment making the result "invisible" and requiring [] to make it printable is mentioned in the "Reference semantics" vignette and in a comment in the ?data.table examples.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions