r-lib / clock

A Date-Time Library for R
https://clock.r-lib.org
Other
97 stars 4 forks source link

error when run examples #290

Closed Yunuuuu closed 2 years ago

Yunuuuu commented 2 years ago

Hi, thanks for your all efforts in developing this R package for working with date-times, I'm learning this package but went to wrong when run example code in clock FAQ using following code:

year_month_day(2019, 2, 28:31)  %>% 
    invalid_resolve(invalid = "next") %>% 
    as_naive_time() %>% 
    add_days(x, 1L)

which generated such error information:

Error in `stop_vctrs()`:
! Can't convert `n` <date> to <integer>.
Run `rlang::last_error()` to see where the error occurred.

rlang::last_error() produced following

<error/vctrs_error_incompatible_type>
Error in `stop_vctrs()`:
! Can't convert `n` <date> to <integer>.        
Backtrace:
  1. ... %>% add_days(x, 1L)
  3. clock:::add_days.clock_time_point(., x, 1L)
  4. clock:::time_point_plus_duration(...)      
  5. clock:::time_point_arith_duration(x, n, precision_n, names, duration_plus)
  6. clock:::duration_collect_n(n, precision_n)
  7. clock:::duration_helper(n, precision, retain_names 
= TRUE)
  8. vctrs::vec_cast(n, integer(), x_arg = n_arg)       
  9. vctrs `<fn>`()
 10. vctrs::vec_default_cast(...)
 11. vctrs::stop_incompatible_cast(...)
 12. vctrs::stop_incompatible_type(...)
 13. vctrs:::stop_incompatible(...)
 14. vctrs:::stop_vctrs(...)
Run `rlang::last_trace()` to see the full context.  

here is my sessionInfo output:

[R]> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)  
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936

attached base packages:
[1] stats     graphics  grDevices utils     datasets    
[6] methods   base

other attached packages:
[1] magrittr_2.0.2   clock_0.6.0.9000

loaded via a namespace (and not attached):
 [1] lubridate_1.8.0      fansi_1.0.2
 [3] RevoUtilsMath_11.0.0 tzdb_0.2.0
 [5] utf8_1.2.2           crayon_1.4.2
 [7] lifecycle_1.0.1      jsonlite_1.7.3
 [9] pillar_1.7.0         rlang_1.0.0
[11] cli_3.1.1            vctrs_0.3.8
[13] generics_0.1.2       ellipsis_0.3.2
[15] tools_4.1.2          glue_1.6.1
[17] compiler_4.1.2
DavisVaughan commented 2 years ago

are you sure it isn't just a copy paste error? it seems like you have an extra x in add_days()`

library(clock)
library(magrittr)

year_month_day(2019, 2, 28:31)  %>% 
  invalid_resolve(invalid = "next") %>% 
  as_naive_time() %>% 
  add_days(x, 1L) # <- right here you dont need `x`
#> Error in is_duration(x): object 'x' not found

year_month_day(2019, 2, 28:31)  %>% 
  invalid_resolve(invalid = "next") %>% 
  as_naive_time() %>% 
  add_days(1L)
#> <time_point<naive><day>[4]>
#> [1] "2019-03-01" "2019-03-02" "2019-03-02" "2019-03-02"

Created on 2022-02-08 by the reprex package (v2.0.1)

Yunuuuu commented 2 years ago

Oh! sorry for the stupid mistakes. I have checked this for a long time but didn't find this typos!!! Thanks for your help @DavisVaughan!