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

Why do new_pillar_shaft() and new_pillar_shaft_simple() default min_width to width? #646

Closed
DavisVaughan opened this issue May 2, 2023 · 2 comments

Comments

@DavisVaughan
Copy link
Member

In bench, there is a custom expression class called bench_expr that displays the captured expressions

We have a pillar_shaft() method for this that looks like:

pillar_shaft.bench_expr <- function(x, ...) {
  pillar::new_pillar_shaft_simple(format.bench_expr(x), align = "left", ...)
}

It seems like the way we defined this method is actually the cause of this issue
r-lib/bench#94

i.e. if you have a very long bench expression then it won't get truncated like a normal character column would. It seems like this is because min_width defaults to width, disallowing any truncation.

It seems like I can fix it by doing this:

pillar_shaft.bench_expr <- function(x, ...) {
  pillar::pillar_shaft(format.bench_expr(x), ...)
}

i.e. convert to character and then use the standard pillar_shaft.character() method.

The pillar_shaft.character() method respects pillar.min_chars through get_pillar_option_min_chars() so the min_width ends up being a good default of 3 by doing this.

I wonder if the default of new_pillar_shaft() and new_pillar_shaft_simple() should also respect this option rather than defaulting to width?

@DavisVaughan
Copy link
Member Author

Maybe width is a good default for most types (because, for example, you want to see the full number with a double column), and character vectors are just a bit special?

In that case, my solution above is pretty similar to what is done for factors, so I feel pretty good about it

> pillar:::pillar_shaft.factor
function (x, ...) 
{
    pillar_shaft(as.character(x), ...)
}

Feel free to close if you agree with my thoughts here

@krlmlr
Copy link
Member

krlmlr commented Dec 14, 2024

Thanks. The solution applied in bench looks good to me. With rlang::expr_deparse(width = ...), you could perhaps truncate in a nicer way (if not done already).

@krlmlr krlmlr closed this as completed Dec 14, 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

No branches or pull requests

2 participants