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

suggest: align on a character #139

Closed
r2evans opened this issue Jan 18, 2019 · 6 comments · Fixed by #1058
Closed

suggest: align on a character #139

r2evans opened this issue Jan 18, 2019 · 6 comments · Fixed by #1058

Comments

@r2evans
Copy link

r2evans commented Jan 18, 2019

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="=").

@rich-iannone
Copy link
Member

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.

@r2evans
Copy link
Author

r2evans commented Jan 31, 2019

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?

@elong0527
Copy link

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.

@elong0527
Copy link

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

@billdenney
Copy link
Contributor

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).

@cportner
Copy link

Has there been any progress on using something like dcolumn or at least allowing for a flexible heading/footer to substitute in some user-provided text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment