Skip to content

obj_print*() gain max argument - #1482

Open
krlmlr wants to merge 21 commits into
mainfrom
f-1355-obj-print-max
Open

obj_print*() gain max argument#1482
krlmlr wants to merge 21 commits into
mainfrom
f-1355-obj-print-max

Conversation

@krlmlr

@krlmlr krlmlr commented Oct 27, 2021

Copy link
Copy Markdown
Member

This argument is also accepted by base::print(), but undocumented.

Closes #1355.

Comment thread R/utils.R Outdated
Comment thread R/print-str.R Outdated
@krlmlr
krlmlr requested a review from DavisVaughan October 27, 2021 16:42
@DavisVaughan

Copy link
Copy Markdown
Member

also accepted by base::print(), but undocumented.

max is actually documented in ?print.default, if that is what you were referring to

Comment thread R/print-str.R Outdated
obj_print_data(x, ...)
obj_print_footer(x, ...)
obj_print <- function(x, ..., max = NULL) {
max <- local_max_print(max)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this whole local function + _dispatch() redirection is a bit complicated. Can you help me understand why we need it? In clock I was able to get print() to work correctly after I already sliced x by passing max all the way through https://github.com/r-lib/clock/blob/0a07f630bc5859e33b34b85380fafe028cfed5eb/R/calendar.R#L21, can we do that here instead?

Then I'd imagine we just pass max through to each of the three helpers, they'd each validate max using some helper like this one https://github.com/r-lib/clock/blob/f3b79db9226fd4af09b1b7175bdfbb4d225386fb/R/utils.R#L263, and then use it as required

If it has something to do with making it easier for subclasses who implement obj_print_data() methods, I'm not sure we should worry about that too much, since if you implement a obj_print_data() method then you'd already have to handle the slicing yourself anyways, so you may as well handle the collection of the max argument too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With local_max_print() and _dispatch we:

  • ensure correctness of the max arg with a consistent error message
  • free the implementer from dealing with these details
  • correctly handle the case when max is set to a value larger than getOption("max.print")
  • avoid querying getOption("max.print") in every helper (we need it at least in the data and in the footer)

I rather like this pattern. Slicing really is just one line of code, handling max is like 15 in an extra helper function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(*) if we had a vec_head()

We could also handle the slicing ourselves, but then we'd need to pass a size argument. Also, some vctrs classes might be able to print without slicing explicitly.

Comment thread R/print-str.R Outdated
Comment thread R/print-str.R
obj_print_data.default <- function(x, ...) {
if (length(x) == 0)
obj_print_data.default <- function(x, ..., max) {
if (!vec_is(x)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see many people using obj_print() if they don't have a vector class, am I missing a use case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should max be passed through if we do keep this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obj_print() is called by vctrs for non-vector classes, IIRC vctrs_scalar .

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm but I feel like that is mostly for testing, and we don't export it. Any thoughts on this @lionel- ?

Comment thread R/print-str.R Outdated
Comment thread R/print-str.R Outdated
Comment thread R/print-str.R
Comment thread R/type-list-of.R Outdated
Comment thread R/type-list-of.R
Good catch @DavisVaughan!

Co-authored-by: Davis Vaughan <davis@rstudio.com>
Comment thread R/print-str.R Outdated
@krlmlr
krlmlr requested a review from DavisVaughan October 29, 2021 14:43
@lionel-

lionel- commented Feb 3, 2022

Copy link
Copy Markdown
Member

I don't understand why we don't slice in the generic?

@krlmlr

krlmlr commented Aug 19, 2022

Copy link
Copy Markdown
Member Author

@lionel-: IIRC, to slice in the generic, we would have to pass both the sliced vector and the original lengths to the detail methods. Do you prefer this solution?

@lionel-

lionel- commented Aug 19, 2022

Copy link
Copy Markdown
Member

Don't we pass the whole vector?

@krlmlr

krlmlr commented Aug 19, 2022

Copy link
Copy Markdown
Member Author

Yes, right now we pass the whole vector.

Are you proposing the following logic:

  • we slice in the generic
  • the methods only ever see a truncated version of the vector
  • obj_print_footer() is no longer responsible for printing the informative line
  • the generic prints the informative line after all methods have been called

?

I rather like that.

@lionel-

lionel- commented Aug 19, 2022

Copy link
Copy Markdown
Member

yup exactly

@krlmlr
krlmlr requested a review from lionel- August 21, 2022 12:43
Comment thread R/print-str.R
local_options(max.print = max, .frame = frame)
}

structure(max, max_print = max_print)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return a list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need both max and max_print? Could we take max = getOption("max.print") in the generic instead, to simplify things?

Comment thread R/print-str.R

if (max > max_print) {
# Avoid truncation in case we're forwarding to print()
local_options(max.print = max, .frame = frame)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is there any downside to unconditionally setting max.print to simplify things?

  2. Can you please try and move the local_options() to the generic and rename this function to as_max_print(). This would simplify things.

@krlmlr krlmlr changed the title obj_print*() gain max argument obj_print*() gain max argument Oct 23, 2024
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

Successfully merging this pull request may close these issues.

Obey max argument and max.print option in obj_print()?

3 participants