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

Column spacing is incorrect when printing columns with fractional seconds #102

Closed
DavisVaughan opened this issue Mar 1, 2018 · 2 comments
Labels

Comments

@DavisVaughan
Copy link
Member

DavisVaughan commented Mar 1, 2018

Following discussion on #74. When the user sets digits.secs option, the fractional seconds are printed but the spacing is off because width is not adjusted dynamically. The reprex below outlines the problem and proposes a solution.

# with dev pillar and tibble
library(pillar)
library(tibble)

from <- as.POSIXct("14:03:55", format="%H:%M:%OS", tz="UTC")
to   <- as.POSIXct("14:04:00", format="%H:%M:%OS", tz="UTC")

options(digits.secs = 4)

ex <- tibble(datetime = seq(from, to, by = 0.01))

ex$col <- 100000

# The correct amount of spacing is not used with fractional seconds
print(ex, n = 2)
#> # A tibble: 501 x 2
#>   datetime                col
#>   <dttm>                <dbl>
#> 1 2018-03-01 14:03:55.0000 100000.
#> 2 2018-03-01 14:03:55.0099 100000.
#> # ... with 499 more rows

# Potential solution
# This should really be pillar_shaft.POSIXt but had to do this for testing
# FYI the digits.secs option allows values from 0-6 and defaults to NULL (0)
pillar_shaft.POSIXct <- function (x, ...) 
{
  # Fractional seconds width adjustment
  width <- 19L
  digits.secs <- min(max(getOption("digits.secs"), 0L), 6L) # Clamp between 0-6
  if (digits.secs) {
    width <- width + digits.secs + 1L # The 1L is for the extra decimal point
  } 
  
  date <- format(x, format = "%Y-%m-%d")
  time <- format(x, format = "%H:%M:%OS")
  datetime <- paste0(date, " ", style_subtle(time))
  datetime[is.na(x)] <- NA
  new_pillar_shaft_simple(datetime, width = width, align = "left")
}

print(ex, n = 2)
#> # A tibble: 501 x 2
#>   datetime                     col
#>   <dttm>                     <dbl>
#> 1 2018-03-01 14:03:55.0000 100000.
#> 2 2018-03-01 14:03:55.0099 100000.
#> # ... with 499 more rows

# Even if the user does something crazy everything is alright...

# The default 
options(digits.secs = NULL)
head(ex, n = 1)
#> # A tibble: 1 x 2
#>   datetime                col
#>   <dttm>                <dbl>
#> 1 2018-03-01 14:03:55 100000.

# Negative 
options(digits.secs = -1)
head(ex, n = 1)
#> # A tibble: 1 x 2
#>   datetime                col
#>   <dttm>                <dbl>
#> 1 2018-03-01 14:03:55 100000.

# Really big (max of 6) 
options(digits.secs = 12)
head(ex, n = 1)
#> # A tibble: 1 x 2
#>   datetime                       col
#>   <dttm>                       <dbl>
#> 1 2018-03-01 14:03:55.000000 100000.

Created on 2018-03-01 by the reprex package (v0.1.1.9000).

@krlmlr
Copy link
Member

krlmlr commented Apr 16, 2018

Thanks for raising this, and for the suggested fix.

@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
Projects
None yet
Development

No branches or pull requests

2 participants