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

Avoid repetitive calls to print() [default] inside print.data.table() #6089

Closed
MichaelChirico opened this issue Apr 12, 2024 · 4 comments · Fixed by #6091
Closed

Avoid repetitive calls to print() [default] inside print.data.table() #6089

MichaelChirico opened this issue Apr 12, 2024 · 4 comments · Fixed by #6091
Assignees

Comments

@MichaelChirico
Copy link
Member

          we really shouldn't need all this repetition. but that's not relevant to this PR.

Originally posted by @MichaelChirico in #6087 (comment)

As illustrated in the linked PR, it's a pain to maintain since we have to remember to update 4 separate call sites to print.default(), all with the same signature. This should be unified.

@joshhwuu
Copy link
Member

What do we think of changing each call to print.default into a do.call with a list of printing arguments? This way, if we ever need to add additional arguments we can just update the argument list, instead of going to every call and adding the extra argument.

We could also make a function wrapper that calls print.default, but we would have to pass in the arguments to this wrapper anyways, which kind of defeats the purpose.

@MichaelChirico
Copy link
Member Author

We could also make a function wrapper that calls print.default, but we would have to pass in the arguments to this wrapper anyways, which kind of defeats the purpose.

Not if the wrapper defined within the print.data.table() body, e.g.

print.data.table = function(...) {
  # ...
  print_default = function(x) print(x, quote=quote, na.print=na.print, right=right)
  # ...
  if (...) {
    print_default()
  } else {
    print_default()
  }
  # ...
}

The third option is if we can refactor the code to avoid needing 4 call sites to begin with, e.g.

cut_colnames(print(toprint, right=TRUE, quote=quote, na.print=na.print))
} else {
print(toprint, right=TRUE, quote=quote, na.print=na.print)

Should we do if (col.names != "none") cut_colnames=identity? Then we can run cut_colnames(print(...)) in both branches.

Can we combine the printdots and !printdots cases as well? Needs a bit more careful thought.

@joshhwuu
Copy link
Member

joshhwuu commented Apr 13, 2024

Looking at it now, quite a bit of the code in printdots and !printdots branches can be factored out:

if (col.names == "none") {
cut_colnames(print(toprint, right=TRUE, quote=quote, na.print=na.print))
} else {
print(toprint, right=TRUE, quote=quote, na.print=na.print)
}
if (trunc.cols && length(not_printed) > 0L)
# prints names of variables not shown in the print
trunc_cols_message(not_printed, abbs, class, col.names)

and

if (col.names == "none") {
cut_colnames(print(toprint, right=TRUE, quote=quote, na.print=na.print))
} else {
print(toprint, right=TRUE, quote=quote, na.print=na.print)
}
if (trunc.cols && length(not_printed) > 0L)
# prints names of variables not shown in the print
trunc_cols_message(not_printed, abbs, class, col.names)

Would it be beneficial to factor out these two big chunks as a function in the print.data.table body, or even as a helper, and then handle:

Should we do if (col.names != "none") cut_colnames=identity? Then we can run cut_colnames(print(...)) in both branches.

in the factored code?

@MichaelChirico
Copy link
Member Author

It seems reasonable to me to make the print.data.table() body simpler / relying on helpers. We have pretty good tests of output already, so I think you can feel comfortable refactoring as long as you pass tests.

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

Successfully merging a pull request may close this issue.

2 participants