pillar icon indicating copy to clipboard operation
pillar copied to clipboard

Fill available space with numbers, even if this exceeds the requested significant figures

Open krlmlr opened this issue 3 years ago • 3 comments

Should we show "non-significant" digits if there's space, perhaps as an option and in "subtle" style?

Current behavior:

options(digits = 3)

data.frame(x = 10.05 * c(100, 10, 1, 0.1))
#>         x
#> 1 1005.00
#> 2  100.50
#> 3   10.05
#> 4    1.01
tibble::tibble(x = 10.05 * c(100, 10, 1, 0.1))
#> # A tibble: 4 x 1
#>         x
#>     <dbl>
#> 1 1005   
#> 2  100.  
#> 3   10.0 
#> 4    1.01

Created on 2021-04-13 by the reprex package (v1.0.0)

Original reprex by @DasHammett:

library(geosphere)
#> Warning: package 'geosphere' was built under R version 4.0.3
lat <- c(2.19530185019, 
         2.19530185019, 
         2.16478530044, 
         1.13056809668, 
         1.13149200000, 
         1.13066607245, 
         1.13042366000, 
         2.16163929764, 
         2.16017258534, 
         2.19530185019, 
         2.19530185019, 
         1.24425900000, 
         2.19530185019, 
         2.10695278000, 
         2.19838307408, 
         2.16163929764, 
         2.16017258534, 
         1.87536835670, 
         1.88084059295, 
         2.19530185019
         )
lon <- c( 41.4168116699, 
          41.4168116699, 
          41.3745021149, 
          42.4112932739, 
          42.4121110000, 
          42.4114328014, 
          42.4111942700, 
          41.3814433019, 
          41.3780043185, 
          41.4168116699, 
          41.4168116699, 
          42.5150562000, 
          41.4168116699, 
          41.5105799400, 
          41.4402062712, 
          41.3814433019, 
          41.3780043185, 
          42.2349025794, 
          41.9442408387, 
          41.4168116699
          )
origin <- c(1.1952050041416435,42.41829191947389)

df <- data.frame(lat = lat, lon = lon)
df$distance <- distHaversine(origin,df[,c("lat","lon")])

options(digits = 7)
options(pillar.sigfig = 7)

df
#>         lat      lon   distance
#> 1  2.195302 41.41681 138891.335
#> 2  2.195302 41.41681 138891.335
#> 3  2.164785 41.37450 141262.131
#> 4  1.130568 42.41129   5369.016
#> 5  1.131492 42.41211   5281.238
#> 6  1.130666 42.41143   5358.809
#> 7  1.130424 42.41119   5382.372
#> 8  2.161639 41.38144 140476.048
#> 9  2.160173 41.37800 140722.898
#> 10 2.195302 41.41681 138891.335
#> 11 2.195302 41.41681 138891.335
#> 12 1.244259 42.51506  11500.293
#> 13 2.195302 41.41681 138891.335
#> 14 2.106953 41.51058 126115.959
#> 15 2.198383 41.44021 136955.312
#> 16 2.161639 41.38144 140476.048
#> 17 2.160173 41.37800 140722.898
#> 18 1.875368 42.23490  59584.024
#> 19 1.880841 41.94424  77353.483
#> 20 2.195302 41.41681 138891.335


tibble::as_tibble(df)
#> # A tibble: 20 x 3
#>         lat      lon   distance
#>       <dbl>    <dbl>      <dbl>
#>  1 2.195302 41.41681 138891.3  
#>  2 2.195302 41.41681 138891.3  
#>  3 2.164785 41.37450 141262.1  
#>  4 1.130568 42.41129   5369.016
#>  5 1.131492 42.41211   5281.238
#>  6 1.130666 42.41143   5358.809
#>  7 1.130424 42.41119   5382.372
#>  8 2.161639 41.38144 140476.0  
#>  9 2.160173 41.37800 140722.9  
#> 10 2.195302 41.41681 138891.3  
#> 11 2.195302 41.41681 138891.3  
#> 12 1.244259 42.51506  11500.29 
#> 13 2.195302 41.41681 138891.3  
#> 14 2.106953 41.51058 126116.0  
#> 15 2.198383 41.44021 136955.3  
#> 16 2.161639 41.38144 140476.0  
#> 17 2.160173 41.37800 140722.9  
#> 18 1.875368 42.23490  59584.02 
#> 19 1.880841 41.94424  77353.48 
#> 20 2.195302 41.41681 138891.3

Created on 2021-04-12 by the reprex package (v2.0.0)

krlmlr avatar Apr 13 '21 02:04 krlmlr

It is possible to fix the number of decimal digits shown in pillar 1.6.0 with num():

tibble::tibble(x = pillar::num(10.05 * c(100, 10, 1, 0.1), digits = 2))
#> # A tibble: 4 x 1
#>           x
#>   <num:.2!>
#> 1   1005.00
#> 2    100.50
#> 3     10.05
#> 4      1.01

Created on 2021-04-13 by the reprex package (v1.0.0)

krlmlr avatar Apr 13 '21 02:04 krlmlr

Maybe we could support negative numbers for the "pillar.sigfig" option to activate this? We could then also flip the meaning of the digits argument: negative digits always shows trailing zeros, positive digits truncates if allowed.

krlmlr avatar Apr 18 '21 02:04 krlmlr

We need to distinguish between trailing zeros and non-significant digits.

IIUC, https://github.com/r-lib/pillar/issues/97#issuecomment-384420760 suggests showing more significant digits by default, and only highlighting the "really significant" digits. We can prototype in num() and make it a default if it looks good.

krlmlr avatar Jul 26 '21 02:07 krlmlr