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

Option to print the number of columns #6663

Open
Tracked by #1523
KyleHaynes opened this issue Dec 14, 2024 · 5 comments
Open
Tracked by #1523

Option to print the number of columns #6663

KyleHaynes opened this issue Dec 14, 2024 · 5 comments

Comments

@KyleHaynes
Copy link
Contributor

KyleHaynes commented Dec 14, 2024

Could data.table:::print.data.table look to gain an option to print the number of columns?

Happy to put a PR in if so.

If yes, things to consider

  • Parameter name (e.g. ncols to match nrows?)
  • Option name (e.g. getOption("datatable.print.ncols")
  • Label (e.g. ncol)
  • Position (header vs footer?)
  • Don't print if 0 rows (print.data.table already communicates this)
  • On or off by default?

e.g.

> options("datatable.print.ncols" = TRUE)
> iris
Key: <Petal.Width>
ncol: 5
     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
            <num>       <num>        <num>       <num>    <char>
  1:          4.9         3.1          1.5         0.1    setosa
  2:          4.8         3.0          1.4         0.1    setosa
  3:          4.3         3.0          1.1         0.1    setosa
  4:          5.2         4.1          1.5         0.1    setosa
  5:          4.9         3.6          1.4         0.1    setosa
 ---
146:          6.3         3.4          5.6         2.4 virginica
147:          6.7         3.1          5.6         2.4 virginica
148:          6.3         3.3          6.0         2.5 virginica
149:          7.2         3.6          6.1         2.5 virginica
150:          6.7         3.3          5.7         2.5 virginica
@KyleHaynes
Copy link
Contributor Author

Closing, this should go in #1523

@MichaelChirico
Copy link
Member

Re-opened -- #1523 is more of an umbrella issue. I think this will keep more attention as a standalone issue.

I think it's a good idea -- it can be hard to tell the exact number of columns on tables that require wrapping, especially multiple times.

Perhaps the row with Key: should be turned into a row for various metadata, the second element being # columns.

OTOH, Index:(/Indices:) already gets its own row, so Number of columns: fits in better on its own line.

Parameter name (e.g. ncols to match nrows?)

No, nrows is quite different. This parameter is more related to print.keys and show.indices (hmm, why do they have such similar-but-different names 😆). Maybe show.ncols instead, although I wonder whether this should just be on-by-default.

Most important will be the interaction with trunc.cols, which already records the number of columns:

DT=data.table(a=1,b=2,c=3)
setkey(DT)
setindex(DT,a,b)
setindex(DT,a,c)
setindex(DT,b,c)
DT
# Key: <a, b, c>
# Indices: <a__b>, <a__c>, <b__c>
#        a     b     c
#    <num> <num> <num>
# 1:     1     2     3
options(width=10)
print(DT, trunc.cols=TRUE)
# Key: <a, b, c>
# Indices: <a__b>, <a__c>, <b__c>, <b__a>, <c__a>, <c__b>
#        a
#    <num>
# 1:     1
# 2 variable(s) not shown: [b <num>, c <num>]

@TysonStanley WDYT about something like:

  • if trunc.cols=FALSE, record Number of columns: <n>
  • if trunc.cols=TRUE, record Number of columns: <n>, of which <k> are not shown: [...]

That would mean moving the note from the footer to the header.

@rikivillalba
Copy link
Contributor

Imho for coherence, the output should be similar to:

print(data.table(a=integer(0)))
# Empty data.table (0 rows and 1 cols): a
print(data.table())
# Null data.table (0 rows and 0 cols)

@KyleHaynes
Copy link
Contributor Author

Imho for coherence, the output should be similar to:

print(data.table(a=integer(0)))
# Empty data.table (0 rows and 1 cols): a
print(data.table())
# Null data.table (0 rows and 0 cols)

Agree! I'm not proposing any changes to those current messages.

@KyleHaynes
Copy link
Contributor Author

ITMT:

# install.packages("devtools")
devtools::install_github("KyleHaynes/data.table", build_vignettes=FALSE, ref="print_col_nums")
require(data.table)
dt = data.table(iris)
dt
# Number of columns: 5
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#             <num>       <num>        <num>       <num>    <fctr>
#   1:          5.1         3.5          1.4         0.2    setosa
#   2:          4.9         3.0          1.4         0.2    setosa
#   3:          4.7         3.2          1.3         0.2    setosa
#   4:          4.6         3.1          1.5         0.2    setosa
#   5:          5.0         3.6          1.4         0.2    setosa
#  ---
# 146:          6.7         3.0          5.2         2.3 virginica
# 147:          6.3         2.5          5.0         1.9 virginica
# 148:          6.5         3.0          5.2         2.0 virginica
# 149:          6.2         3.4          5.4         2.3 virginica
# 150:          5.9         3.0          5.1         1.8 virginica

setkey(dt)
setindex(dt,Species,Sepal.Width)
dt
# Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species>
# Index: <Species__Sepal.Width>
# Number of columns: 5
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#             <num>       <num>        <num>       <num>    <fctr>
#   1:          4.3         3.0          1.1         0.1    setosa
#   2:          4.4         2.9          1.4         0.2    setosa
#   3:          4.4         3.0          1.3         0.2    setosa
#   4:          4.4         3.2          1.3         0.2    setosa
#   5:          4.5         2.3          1.3         0.3    setosa
#  ---
# 146:          7.7         2.6          6.9         2.3 virginica
# 147:          7.7         2.8          6.7         2.0 virginica
# 148:          7.7         3.0          6.1         2.3 virginica
# 149:          7.7         3.8          6.7         2.2 virginica
# 150:          7.9         3.8          6.4         2.0 virginica

data.table()
# Null data.table (0 rows and 0 cols)
data.table(as.character())
# Empty data.table (0 rows and 1 cols): V1
options(width=10)
print(dt, trunc.cols=TRUE)
# Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species>
# Index: <Species__Sepal.Width>
# Number of columns: 5, of which 5 are not shown: [Sepal.Length <num>, Sepal.Width <num>, Petal.Length <num>, Petal.Width <num>, Species <fctr>]

options(width=200)
options("datatable.show.ncols" = FALSE)
# Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species>
# Index: <Species__Sepal.Width>
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#             <num>       <num>        <num>       <num>    <fctr>
#   1:          4.3         3.0          1.1         0.1    setosa
#   2:          4.4         2.9          1.4         0.2    setosa
#   3:          4.4         3.0          1.3         0.2    setosa
#   4:          4.4         3.2          1.3         0.2    setosa
#   5:          4.5         2.3          1.3         0.3    setosa
#  ---
# 146:          7.7         2.6          6.9         2.3 virginica
# 147:          7.7         2.8          6.7         2.0 virginica
# 148:          7.7         3.0          6.1         2.3 virginica
# 149:          7.7         3.8          6.7         2.2 virginica
# 150:          7.9         3.8          6.4         2.0 virginica

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants