Skip to content

%within% failed inelegantly when a non interval argument is presented #829

@ghost

Description

%within% fails inelegantly when given a non interval second argument,

library(lubridate)

date1 <- lubridate::ymd("2019-01-01")
date2 <- lubridate::ymd("2020-01-01")


# typo folllows: 

date_range <- lubridate::ymd(date1, date2) 

# this  should have read:
# date_range <- lubridate::interval(date1, date2)
# but ymd appropriately just runs over the two dates and products a vector in date_range. 
# sensible enough, even appropriate, but unfortunately 
# date_range as a vector of 2 dates looks very much like 
# date_range as an interval  
# when you print them, and it is easy (trust me, there is an hour of my life missing because of this) 
# to miss that little detail....
 
# if date_range is a vector of dates then 

lubridate::ymd("2019-06-01") %within% date_range

#  fails cryptically with: 
# Error in .setupMethodsTables(fdef, initialize = TRUE) : 
#  no slot of name "group" for this object of class "derivedDefaultMethod"

# I suggest %within% should test if the interval component of function  is truly an interval.
# There is no reason for it to accept a date or other class of object
#  %within%  works fine when  e.g. date1=date2
# no clear need for %within% to allow a date or other class as interval argument

# %wtihin% also allows and fails cryptically for use cases such as:


lubridate::ymd("2019-06-01") %within% 5
lubridate::ymd("2019-06-01") %within% "A"

So, the request is that %wtihin% test and report when call is something llike e.g. var1 %wtihin% var2
%within% should fail or at least report when class(var2)~= interval

Metadata

Metadata

Assignees

No one assigned

    Labels

    reprexneeds a minimal reproducible example

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions