calendar
calendar copied to clipboard
ic_datetime and vectors of datetime
Given a vector of ICS entries, ic_datetime
does not check for length greater than 1. If an ICS file has more than one event:
calendar::ic_read("~/twoevents.ics")
# Warning in if (!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) { :
# the condition has length > 1 and only the first element will be used
# Warning in if (!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) { :
# the condition has length > 1 and only the first element will be used
# # A tibble: 2 x 8
# DTSTART DTEND SUMMARY CATEGORIES LOCATION UID `X-MICROSOFT-CD~
# <dttm> <dttm> <chr> <chr> <chr> <chr> <chr>
# ...
(It doesn't matter what my ICS file contains, just that it contains more than one.)
This is traced back to https://github.com/ATFutures/calendar/blob/master/R/ic_date.R#L13
ic_datetime <- function(x) {
# TODO (LH): regex check x timestamp
if(!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) {
# stop("time should be in this format: 20180809T160000Z")
warning("Non-standard time string: should be in this format: 20180809T160000Z")
x = ""
}
plain <- gsub("[TZtz]", "", x)
datetime <- as.POSIXct(plain, format = "%Y%m%d%H%M%S")
datetime
}
which is called by ic_dataframe
here:
if(any(datetime_cols)) {
x_df[datetime_cols] <- lapply(x_df[, datetime_cols], ic_datetime)
}
I suggest that ic_datetime
should be vectorized (vice trying to change ic_dataframe
).
Thanks for pointing this out @r2evans and agree with your suggested solution. Unfortunately I lack time to work on this this week, PRs and further suggestions are welcome though.
is it just the warning
part that needs to be vectorised though? Not 100% sure what is going on because, as you can see, it's been a while since I worked on this.
Hi
Writing it down here too because its related to this: I have just send a pull request to vectorize de ic_datetime
function
Thanks and regards
Fantastic, that sounds great @francoscarafia many thanks!