Skip to content

print col.names='none' does not affect column width #6882

Description

@brooksambrose

In print.data.table when setting col.names='none' I expected column widths to be sized according to their contents. Instead, the column widths are unchanged. Incidentally, when class is suppressed the column widths are different. I expect the behavior to be consistent between those two options.

library(data.table)
dt<-data.table(x=3:1,sillylongcolumnname=1:3)
print(dt)
#>        x sillylongcolumnname
#>    <int>               <int>
#> 1:     3                   1
#> 2:     2                   2
#> 3:     1                   3
print(dt,col.names='none')
#> Warning: Column classes will be suppressed when col.names is 'none'
#> 1: 3                   1
#> 2: 2                   2
#> 3: 1                   3

Created on 2025-03-24 with reprex v2.1.0

Activity

  1. venom1204 commented on Apr 9, 2025

    @venom1204
    Contributor

    Hi @tdhock ,
    I'd like to work on this issue — could you please assign this issue to me?

    Here’s my proposed solution:
    I plan to modify the width calculation logic so that:

    • Column widths are based only on actual data content when col.names = "none".
    • Width contributions from column names and class annotations are skipped in that case.
    • Behavior remains unchanged when column names are printed normally.

    This should result in a cleaner and more compact output when column names are omitted.

    Let me know if this approach works, and I’ll move ahead with the implementation. Thanks!

  2. MichaelChirico commented on Apr 9, 2025

    @MichaelChirico
    Member

    Yes, go ahead @venom1204, that's the basic idea.

  3. assigned and unassigned on Apr 10, 2025
  4. venom1204 commented on Apr 17, 2025

    @venom1204
    Contributor

    hi @aitap ,
    After further investigation, I found that the most robust solution is to explicitly set all column names to empty strings in the toprint object when col.names = "none":

    if (col.names == "none") 
      colnames(toprint) <- rep.int("", ncol(toprint))
    

    I plan to add this line in the print.data.table function, right after toprint is constructed (e.g., after
    toprint = x[idx, ] or toprint = x), but before any formatting or width calculations. This ensures all downstream formatting uses empty column names, so column widths are determined solely by the data, fully resolving the issue.

    • All tests, including edge cases, now pass and the output is compact and consistent.

    • Could you confirm if this approach is acceptable? Also, if there are any potential side effects or unrelated areas of the codebase that might be impacted by this change(right now there aren't any according to me)—especially ones I may not be aware of—please let me know. If approved, I’ll proceed with the implementation.

    thank you.

  5. MichaelChirico commented on Apr 17, 2025

    @MichaelChirico
    Member

    Indeed that sounds good. Potentially it could just be included as a condition here:

    if (is.null(names(x)) || !any(nzchar(names(x), keepNA=TRUE)))
    # fixes bug #97 and #545
    colnames(toprint)=rep("", ncol(toprint))

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions