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

control over < > for print.tbl_df method #73

Closed
edzer opened this issue Jan 4, 2018 · 17 comments
Closed

control over < > for print.tbl_df method #73

edzer opened this issue Jan 4, 2018 · 17 comments
Projects

Comments

@edzer
Copy link

edzer commented Jan 4, 2018

The standard way to print units of measure is between square brackets, as in [km/h]. When I do this in a tibble header, I get <[km/h]>, which looks odd (example below) and takes unnecessary space. Is there a way to get rid of the < and >? Should I raise this issue with tibble?

suppressPackageStartupMessages(library(units))
mt = mtcars
mt$mpg = set_units(mt$mpg, km/h)
library(tibble)
(m <- as.tibble(mt))
# # A tibble: 32 x 11
#         mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#  * <[km/h]> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#  1     21.0  6.00   160 110    3.90  2.62  16.5  0     1.00  4.00  4.00
#  2     21.0  6.00   160 110    3.90  2.88  17.0  0     1.00  4.00  4.00
#  3     22.8  4.00   108  93.0  3.85  2.32  18.6  1.00  1.00  4.00  1.00
#  4     21.4  6.00   258 110    3.08  3.22  19.4  1.00  0     3.00  1.00
#  5     18.7  8.00   360 175    3.15  3.44  17.0  0     0     3.00  2.00
#  6     18.1  6.00   225 105    2.76  3.46  20.2  1.00  0     3.00  1.00
#  7     14.3  8.00   360 245    3.21  3.57  15.8  0     0     3.00  4.00
#  8     24.4  4.00   147  62.0  3.69  3.19  20.0  1.00  0     4.00  2.00
#  9     22.8  4.00   141  95.0  3.92  3.15  22.9  1.00  0     4.00  2.00
# 10     19.2  6.00   168 123    3.92  3.44  18.3  1.00  0     4.00  4.00
# # ... with 22 more rows
@hadley
Copy link
Member

hadley commented Jan 8, 2018

You can define your own type_sum() method, but the <> are added by pillar in order to force the display to be consistent.

@edzer
Copy link
Author

edzer commented Jan 8, 2018

A fully unit-ized mtcars generated by

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(units))
library(tibble)
options(width = 100)
mtcars %>%
	mutate(mpg = set_units(mpg, mile/gallon),
	cyl = set_units(cyl),
	disp = set_units(disp, `in`^3),
	hp = set_units(hp, hp),
	drat = set_units(drat),
	wt = set_units(wt, klb),
	qsec = set_units(qsec, s),
	vs = set_units(vs, V/S),
	am = c("aut", "man")[am+1],
	gear = set_units(gear),
	carb = set_units(carb)
	) %>% 
	as.tibble %>%
	print(width = 100)

now looks like this:
u

which is nice! However, the < and > pairs are verbose, distracting, and take unnecessary space. Maybe give an optional parameter to type_sum to remove them?

@hadley
Copy link
Member

hadley commented Jan 8, 2018

In this case, it seems a bit duplicative to have the type in the heading and next to every individual value.

@edzer
Copy link
Author

edzer commented Jan 8, 2018

I agree; I was thinking of making that configurable, and possibly off by default. If left away, the space taken by < and > becomes proportionally more disturbing.

u

@hadley
Copy link
Member

hadley commented Jan 8, 2018

I wonder if it's worth never applying the <> - colour + italics might be sufficient (although then it would have to display differently if colour not available, which is somewhat undesirable)

@edzer
Copy link
Author

edzer commented Jan 8, 2018

(I updated my comment above to show the output without units behind each value)

@krlmlr
Copy link
Member

krlmlr commented Jan 10, 2018

I wonder if we could parse the value returned by type_sum(): If it already has parentheses (like {} or [] or <>), we use them. If the value is surrounded by a pair of spaces, we don't add angle brackets.

  • "chr" -> "<chr>"
  • "[km/h]" -> "[km/h]"
  • " Column label " -> "Column label"

In strengejacke/sjlabelled#3 (comment) I was suggesting a

custom column header. We don't have callbacks for this in pillar yet, but I'm happy to provide them. Same idea, but you're overriding a different method. This gives support for multiline headers and even more flexibility, e.g., we could support indicating a minimum width for the data, or word wrapping.

But I'm no longer sure how useful a custom multiline column header would be, given the very limited space available in a terminal.

@krlmlr
Copy link
Member

krlmlr commented Apr 9, 2018

@edzer @etiennebr: Did we discuss that an attribute in the value returned by a type_sum() method could take care of controlling the brackets, and if it's missing, we use the default "<" and ">"?

@krlmlr krlmlr added this to To Do in krlmlr Apr 9, 2018
@etiennebr
Copy link

Yes, we discussed it. If I remember correctly we discussed making something like format_full_type generic so it could be called per class (would need a class for labels, I guess) and offer more styling control for each class. But looking at it now, it doesn't seem like the simplest solution.

@krlmlr krlmlr closed this as completed in aa7761e Apr 16, 2018
krlmlr automation moved this from To Do to Done Apr 16, 2018
@krlmlr
Copy link
Member

krlmlr commented Apr 16, 2018

Thanks for the reminder, the new format_type_sum() generic still feels like the right way. Are the examples useful?

CC @strengejacke.

@edzer
Copy link
Author

edzer commented Jun 15, 2018

This works without brackets, but it seems to break if I specify square brackets in the type_sum, as in

type_sum.units <- function(x, ...) {
  structure(paste0("[", as.character(units(x)), "]"), class = "type_sum_units")
}

In this case, the last two characters are bitten off.

edzer added a commit to r-quantities/units that referenced this issue Jun 15, 2018
@edzer
Copy link
Author

edzer commented Jun 15, 2018

library(pillar)
type_sum.accel <- function(x) {
  structure("kg m/s^2", class = "type_sum_accel")
}

format_type_sum.type_sum_accel <- function(x) {
  style_subtle(x)
}

accel <- structure(9.81, class = "accel")

pillar(accel)
#> kg m/s  
#> 9.81

Created on 2018-07-14 by the reprex package (v0.2.0).

@krlmlr
Copy link
Member

krlmlr commented Jul 14, 2018

Thanks for the heads-up, I'll work on a fix.

@krlmlr krlmlr reopened this Jul 14, 2018
@krlmlr
Copy link
Member

krlmlr commented Jul 14, 2018

What's a good way to turn your output into runnable code? reprex::reprex_rescue() or reprex::reprex_clean() didn't work for me, I edited by hand (and updated your code). It would have been easier if this example was created with reprex::reprex(), which is on CRAN (again).

@krlmlr
Copy link
Member

krlmlr commented Jul 14, 2018

🤦‍♂️🤦‍♂️ It's just the example for format_type_sum()...

@krlmlr krlmlr closed this as completed in a8f3589 Jul 14, 2018
@edzer
Copy link
Author

edzer commented Jul 15, 2018

Thanks a lot, @krlmlr!

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
krlmlr
  
Done
Development

No branches or pull requests

4 participants