Gt: suggest: align on a character

Created on 18 Jan 2019  Â·  5Comments  Â·  Source: rstudio/gt

Nice presentation at rstudio::conf2019.

Is it possible to align by decimal point (period or comma, by locale)? That is, your output rendered something like the left, where it might be nice to see it aligned as on the right:

          raw        dot_align
10.234 x 10^1    10.234 x 10^1
 1.234 x 10^2     1.234 x 10^2
        2.222     2.222       
 1.234 x 10^3     1.234 x 10^3

It might be nice to generalize a little so that we may align on, say, =:

  right_align             char_align
  x = 3.14159            x = 3.14159   
another_x = 7    another_x = 7 

This might be something like cols_align(align=".") or cols_align(align="=").

[2] Intermediate [3] High [2] Medium ★ Enhancement

Most helpful comment

I would love to have this feature as I use align on decimal place all the time in tables to ensure clarity of what may be different orders of magnitude being represented.

One important thing to ensure is handled consistently is when there is more than one of the character in the input data. I assume that an option to align on first or last occurrence would be needed (or at least to ensure documentation clarity if the alignment will only occur on the first or last occurrence).

All 5 comments

This is a very good suggestion! I'd have to see how well it would work across the different output types and between fixed-width and variable fonts.

If by "output types" you mean HTML, LaTeX, and RTF, I was thinking the same thing. I personally would prioritize the first two over the third, since LaTeX is very academically motivated/used, and the second for blogs and such (and perhaps pagedown). I don't know the frequency of RTF use and since I don't use it myself, I can neither promote nor refute its difficulty or pay-off.

Fixed-Width would likely be the simplest, to be honest, since all you do is find the first such character and space-pad the rest.

LaTeX:

HTML:

RTF: sorry, I've never really used RTF, and though I could google it, I'm not certain how good a solution it might be. Would it be acceptable (if not feasible) to support it in 2-of-3 and warn when attempting to use it here?

For fixed fonts, I wrote some code below

library(stringr)

col_align_char <- function(x, align = "."){
  xx <- strsplit(x, align, fixed = TRUE)
  x_head <- unlist(lapply(xx, function(x) x[1]))
  x_rest <- unlist(lapply(xx, function(x) paste0(x[-1], collapse = align)) )

  # add space 
  n_head <- max(nchar(x_head))
  n_rest <- max(nchar(x_rest))

  x_head1 <- str_pad(x_head, n_head)
  x_rest1 <- str_pad(x_rest, n_rest, side = "right")

  x_align <- ifelse(nchar(x_rest) > 0, 
                    paste( x_head1, x_rest1, sep = align ), 
                    paste( x_head1, x_rest1, sep = " " ))

  x_align
}

data.frame(x = col_align_char( c("1.2", "33.5", "33333.2","333.5.27",".123","123")))
as.matrix(col_align_char(x))

For variable fonts, could we split the string by char to two columns. Then display the first column right adjusted and the second column left adjusted with no column border? I can test this idea in RTF if you agreed it is a proper way to handle this.

For RTF, decimal alignment can be solved by using \qj\tqdec\txN at the same place for \ql \qc and \qr.

https://groups.google.com/forum/#!topic/comp.soft-sys.sas/ATYKvb30z64

I would love to have this feature as I use align on decimal place all the time in tables to ensure clarity of what may be different orders of magnitude being represented.

One important thing to ensure is handled consistently is when there is more than one of the character in the input data. I assume that an option to align on first or last occurrence would be needed (or at least to ensure documentation clarity if the alignment will only occur on the first or last occurrence).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rich-iannone picture rich-iannone  Â·  6Comments

jthomasmock picture jthomasmock  Â·  3Comments

DickStartz picture DickStartz  Â·  4Comments

tbradley1013 picture tbradley1013  Â·  3Comments

jestarr picture jestarr  Â·  3Comments