Comments (12)
Maybe writing a local anytime_safe(....)
that calls anytime(...)
and checks the result for NAs and then proceeds with a warning as you desire. It still strikes me as something I would not want to add to anytime
directly.
I do grant you that as.Date()
also hollers:
> as.Date("2023-09-08")
[1] "2023-09-08"
> as.Date("2023-19-08")
Error in charToDate(x) :
character string is not in a standard unambiguous format
>
The problem is that the absence of NA is not a guarantee the right happened. The ambiguity with non-ISO inputs is one example:
> anytime::anydate("12/13/2023")
[1] "2023-12-13"
> anytime::anydate("13/12/2023")
[1] NA
>
Maybe the first one was 'wrong' too because it simply silently converted under the wrong guess of format? It's a hard problem.
from anytime.
Not grealy in love with this yet but we now warn when a NA is seen:
edd@rob:~/git/anytime(feature/na_warning)$ Rscript -e 'library(anytime); anydate(c("13/13/2023", format(Sys.Date()), "44/44/4444"))'
[1] NA "2023-09-09" NA
Warning messages:
1: In anytime_cpp(x = x, tz = tz, asUTC = asUTC, asDate = TRUE, useR = useR, :
Input conversion of '13/13/2023' resulted in 'NA' results.
2: In anytime_cpp(x = x, tz = tz, asUTC = asUTC, asDate = TRUE, useR = useR, :
Input conversion of '44/44/4444' resulted in 'NA' results.
edd@rob:~/git/anytime(feature/na_warning)$
from anytime.
Yeppers. I showed two occassions in my example for two reasons:
- it can be useful to see a NA for each failed conversion
- it can be tedious to see several
and I was leaning also towards to latter so I have to think about about just added a state variable to ensure we print at most one.
(Also the overall format is not "great" but it is the right thing to do this via the warnings()
interface as it has extra handles for extra features.)
from anytime.
Changed to this:
edd@rob:~/git/anytime(feature/na_warning)$ Rscript -e 'library(anytime); anydate(c("13/13/2023", format(Sys.Date()), "44/44/4444"))'
[1] NA "2023-09-13" NA
Warning message:
In anytime_cpp(x = x, tz = tz, asUTC = asUTC, asDate = TRUE, useR = useR, :
Input conversion of '13/13/2023' resulted in 'NA' results.
edd@rob:~/git/anytime(feature/na_warning)$
So it will warn once only. Probably good enough.
from anytime.
But isn't the presence of NA the warning already? You can always wrap a function around from your that checks with any(is.na(...))
over the result set.
from anytime.
You might not notice the NA without a warning. I've caught a lot of mistakes from lubridate
's warnings.
from anytime.
What if the first one gave a message as to how the format was guessed, much like how read_csv()
gives messages on which column types were guessed?
A message like that and a warning when an input becomes NA won't guarantee the right thing happened, but they will help show what's happening.
from anytime.
Maybe I am slowly coming around to your view. I'll need a moment to look at the code. There are frankly too many entry and exit points but maybe I can limit it to 'what happens in anytime_cpp()'
- respect an
options()
at startup to no do this and act as before - before returning check for any NAs in the result set and warn if any found
One problem is that anytime()
and utctime()
and anydate()
and utcdate()
do not have a common 'single' core.
So maybe the simple safe_anytime()
wrapper is easiest.
from anytime.
You are suggesting behavior in terms of tidyverse packages and functions (reference to lubridate
, reference to read_csv
) and I should get off my chest that I do not consider these to be normative. Only Base R is.
The rest are opionionated add-ons, including my packages. So if lubridate
does what you want, but all means use and enjoy it! It is a package that appears to make a lot of people happy enough. I wrote anytime
for myself and it suits me (and seemingly a few other people too).
from anytime.
This is more of a humble suggestion than a personal request/issue. I agree a simple wrapper is a great solution instead of asking for features. I am just thinking of future users who might not be comfortable with wrappers.
I admit I'm a biased fan of tidyverse
!
from anytime.
You do raise a fair point, and it would be a little easier if we had one input and output. Consider for example that I also dispatch (via S3) to as.Date()
or as.POSIXct()
if I already get that as input. (Arguably a corner case as the staple is conversion from character.) Consider now we have proper input in that class, but it contains a NA. Similar to
> as.Date(Sys.Date()+c(0:2,NA,4))
[1] "2023-09-08" "2023-09-09" "2023-09-10" NA "2023-09-12"
>
If we add a check to the C++ parts of anytime()
then our anydate()
would do the same on that input, not warn, yet return a NA. That is .... kinda not great as it remains silent if we add a NA test to the from-character conversion.
And adding it everywhere seems ... heavy. So tough one.
from anytime.
That looks great. It should help researchers (like my former self) with manually entered dates. The only feedback I can add is that avoiding the repetition of In anytime_cpp
.. would make it even more user-friendly. I can't comment on your previous post, but it seems you solved it. Thank you, Dirk.
from anytime.
Related Issues (20)
- Time is silently scrubbed when using certain string date time formats HOT 3
- Add argument for default MM/DD to add to just YYYY inputs HOT 1
- timedatectl problem on HPC? HOT 2
- Failed to activate service 'org.freedesktop.timedate1' on Google Cloud VM HOT 9
- month year specification HOT 7
- Could anydate support nanotime ? HOT 4
- Returning NA value HOT 3
- Feature requests: more flexibly find date substring in a non-date string; and process additional incomplete date substrings HOT 11
- European vs US date formats HOT 3
- Feature request: function to return which format was recognized. HOT 3
- Anytime errors with length 1 NA HOT 6
- Chinese date format suggestion HOT 3
- just yyyy-mm-dd hh:mm:ss but not AEST suffix HOT 5
- anytime() sometimes returns the wrong date HOT 2
- Inconsistent handling of vectors with unknown values HOT 1
- Trivial conversion to NA HOT 1
- time is always one-hour early HOT 3
- `"30081993"` is not understood by `"%d%m%Y"` HOT 2
- Question on the performance of `anydate()` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anytime.