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

Resolution on an hms object #6134

Open
muschellij2 opened this issue Oct 11, 2024 · 0 comments · May be fixed by r-lib/scales#469
Open

Resolution on an hms object #6134

muschellij2 opened this issue Oct 11, 2024 · 0 comments · May be fixed by r-lib/scales#469

Comments

@muschellij2
Copy link

I found a problem with the resolution function. I expected it to fail on difftime objects uniformly but it does not. This is related to the scales package:

Here is the code to reproduce the bug:

Error

Getting errors with resolution if the object is a difftime (hms) object and there is not a zero in the range.
In my case that means if midnight 00:00:00 is included as an hms value, I get no error, but do otherwise.

Load up Libraries

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(hms)
#> 
#> Attaching package: 'hms'
#> The following object is masked from 'package:lubridate':
#> 
#>     hms
library(ggplot2)

Set a Time Near midnight for demonstration purpuoses

The issue doesn’t exist if midnight is somewhere in the time data, so we can set the data near there.

the_time = structure(1728619188, class = c("POSIXct", "POSIXt"))
the_time
#> [1] "2024-10-10 23:59:48 EDT"

Passing in a vector with 1 unique POSIXt value

If we pass in the the_time or duplicate copies of it, we get a resolution of 1 second.
This is somewhat expected due to

if (is.integer(x) || zero_range(range(x, na.rm = TRUE))) {

will call scales::zero_range and the range will be 0 since it’s will
exit at https://github.com/r-lib/scales/blob/6e5e4c352b36b0903ebcc484264e2e741cd93922/R/bounds.R#L377.

resolution(the_time)
#> [1] 1
resolution(c(the_time, the_time))
#> [1] 1

POSIXt fails generally

All the rest fail for POSIXt as that if is never satisfied:

resolution(c(the_time, the_time + 1))
#> Error in Math.POSIXt(x): 'abs' not defined for "POSIXt" objects
resolution(c(the_time, the_time + 60*60*25))
#> Error in Math.POSIXt(x): 'abs' not defined for "POSIXt" objects

Non-identical data that does not cross midnight

Create a sequence that does not cross midnight (aka range has a 0), still error.

resolution(as_hms(seq(the_time, the_time + 1, by = 1)))
#> Error in `/.difftime`((x[1] - x[2]), m): second argument of / cannot be a "difftime" object

The error happens at https://github.com/r-lib/scales/blob/6e5e4c352b36b0903ebcc484264e2e741cd93922/R/bounds.R#L397 and m in this case is a difftime object.

Non-identical data that does cross midnight

Create a sequence that crosses midnight, range has a 0 in there, so no error.
The error is “resolved” because https://github.com/r-lib/scales/blob/6e5e4c352b36b0903ebcc484264e2e741cd93922/R/bounds.R#L391 exits with FALSE.

the_time + 13
#> [1] "2024-10-11 00:00:01 EDT"
resolution(as_hms(seq(the_time, the_time + 13, by = "1 sec")))
#> [1] 1

Simpler Example

Much simpler example (but doesn’t show why resolution affected).
This is due to 2 different ranges

range = structure(c(1234, 7380), class = "difftime", units = "secs")
range_with_zero = structure(c(0, 7380), class = "difftime", units = "secs")
scales:::zero_range(range)
#> Error in `/.difftime`((x[1] - x[2]), m): second argument of / cannot be a "difftime" object
scales:::zero_range(range_with_zero)
#> [1] FALSE

Created on 2024-10-11 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.0 (2024-04-24)
#>  os       macOS Sonoma 14.4.1
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2024-10-11
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] CRAN (R 4.4.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.4.1)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate      1.0.0   2024-09-17 [1] CRAN (R 4.4.1)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.4   2024-04-25 [1] CRAN (R 4.4.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.4.0)
#>  ggplot2     * 3.5.1   2024-04-23 [1] CRAN (R 4.4.0)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.4.0)
#>  gtable        0.3.5   2024-04-22 [1] CRAN (R 4.4.0)
#>  hms         * 1.1.3   2023-03-21 [1] CRAN (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr         1.48    2024-07-07 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  munsell       0.5.1   2024-04-01 [1] CRAN (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.28    2024-08-17 [1] CRAN (R 4.4.1)
#>  rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
#>  timechange    0.3.0   2024-01-18 [1] CRAN (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.4.0)
#>  xfun          0.47    2024-08-17 [1] CRAN (R 4.4.1)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant