joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 70 forks source link

Dynamically determine number of sub-second digits to print #405

Open joshuaulrich opened 10 months ago

joshuaulrich commented 10 months ago

Currently, the number of sub-second values printed depends on the digits.secs global option or the tformat attribute on the xts object being printed. By default, no sub-second digits are printed.

It would be nice if the number of sub-second digits that exist were printed by default. All these example should print with 6 digits because that's the largest (10.000001).

y <- structure(1:10, dim = c(10L, 1L),
    index = structure(c(1.0001, 2.002, 3.03, 4, 5, 6, 7, 8, 9, 10.000001),
                      tclass = c("POSIXct", "POSIXt"), tzone = ""),
    class = c("xts", "zoo"), dimnames = list(NULL, "y"))

print(y, show.rows = 3, max.rows = 5)  # no sub-second digits
##                      y
## 1969-12-31 18:00:01  1
## 1969-12-31 18:00:02  2
## 1969-12-31 18:00:03  3
##                 ...   
## 1969-12-31 18:00:08  8
## 1969-12-31 18:00:09  9
## 1969-12-31 18:00:10 10

print(y, show.rows = 3, max.rows = 5)  # 3 sub-second digits
##                          y
## 1969-12-31 18:00:01.000  1
## 1969-12-31 18:00:02.001  2
## 1969-12-31 18:00:03.030  3
##                     ...   
## 1969-12-31 18:00:08.000  8
## 1969-12-31 18:00:09.000  9
## 1969-12-31 18:00:10.000 10

options(digits.secs = 6)
print(y, show.rows = 3, max.rows = 5)  # 6 sub-second digits
##                             y
## 1969-12-31 18:00:01.000100  1
## 1969-12-31 18:00:02.001999  2
## 1969-12-31 18:00:03.030000  3
##                        ...   
## 1969-12-31 18:00:08.000000  8
## 1969-12-31 18:00:09.000000  9
## 1969-12-31 18:00:10.000002 10

options(digits.secs = op_ds)