-
Notifications
You must be signed in to change notification settings - Fork 66
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
obj_print*()
gain max argument
#1482
base: main
Are you sure you want to change the base?
Changes from 20 commits
6ac33b9
35186b7
dc20934
611f261
4753657
9b806f9
f0877d4
7516e5f
9d0c1fe
ce01ffb
10d5084
667e321
04483a2
0dd0d20
3387df4
c895ff3
6ece3fb
b755345
8ad1bf5
e7fc6eb
7db1d0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,55 @@ | |
#' the `_header()`, `_data()` or `_footer()` components individually. The | ||
#' default methods are built on top of `format()`. | ||
#' | ||
#' @details | ||
#' If you are implementing `obj_print_header()`, `obj_print_data()` or | ||
#' `obj_print_footer()`, your method can assume that the `max` argument | ||
#' is a scalar integer that accurately describes the maximum number of items | ||
#' to print, and that `getOption("max.print")` is set to at least that value. | ||
#' | ||
#' All methods receive `x` unchanged when called from `obj_print()`. | ||
#' `obj_print_data()` should print only the first `max` elements. | ||
#' | ||
#' @param x A vector | ||
#' @param ... Additional arguments passed on to methods. See [print()] and | ||
#' [str()] for commonly used options | ||
#' @param max The maximum number of items to print, defaults to | ||
#' `getOption("print.max")`. | ||
#' @keywords internal | ||
#' @export | ||
obj_print <- function(x, ...) { | ||
obj_print <- function(x, ..., max = NULL) { | ||
if (!vec_is(x)) { | ||
delta <- 0 | ||
x_max <- x | ||
} else { | ||
max <- local_max_print(max) | ||
delta <- vec_size(x) - max | ||
|
||
if (vec_size(x) > max) { | ||
x_max <- vec_slice(x, seq_len(max)) | ||
} else { | ||
x_max <- x | ||
} | ||
} | ||
|
||
obj_print_header(x, ...) | ||
obj_print_data(x, ...) | ||
obj_print_data(x_max, ...) | ||
obj_print_footer(x, ...) | ||
|
||
if (delta > 0) { | ||
max_print <- attr(max, "max_print") | ||
if (is.null(max_print)) { | ||
max_print <- getOption("max.print") | ||
} | ||
|
||
cat_line("... and ", big_mark(delta), " more") | ||
if (max < max_print) { | ||
cat_line("Set `max` to a larger value to show all items.") | ||
} else { | ||
cat_line("Set `options(max.print = )` to a larger value to show all items.") | ||
} | ||
} | ||
|
||
invisible(x) | ||
} | ||
|
||
|
@@ -38,8 +78,14 @@ obj_print_data <- function(x, ...) { | |
|
||
#' @export | ||
obj_print_data.default <- function(x, ...) { | ||
if (length(x) == 0) | ||
if (!vec_is(x)) { | ||
print(x, quote = FALSE) | ||
return(invisible(x)) | ||
} | ||
|
||
if (vec_size(x) == 0) { | ||
return(invisible(x)) | ||
} | ||
|
||
out <- stats::setNames(format(x), names(x)) | ||
print(out, quote = FALSE) | ||
|
@@ -58,6 +104,23 @@ obj_print_footer.default <- function(x, ...) { | |
invisible(x) | ||
} | ||
|
||
local_max_print <- function(max, frame = parent.frame()) { | ||
max_print <- getOption("max.print") | ||
DavisVaughan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (is.null(max)) { | ||
max <- max_print | ||
} | ||
|
||
stopifnot(is_integerish(max, 1L, finite = TRUE), max >= 0, max < 2147483648) | ||
max <- as.integer(max) | ||
|
||
if (max > max_print) { | ||
# Avoid truncation in case we're forwarding to print() | ||
local_options(max.print = max, .frame = frame) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
structure(max, max_print = max_print) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return a list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need both |
||
} | ||
|
||
|
||
# str --------------------------------------------------------------------- | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?There was a problem hiding this comment.
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, IIRCvctrs_scalar
.There was a problem hiding this comment.
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- ?