I am writing a function which requires time. If time is not in the correct format, I would like to stop executing the function. However, it seems when I use tryCatch, although the message shows, the result is still saved and the function finishes.
Here is what I have
date_bad <- "date"
date_good <- "2022-03-25"
tryCatch(
date_check <- lubridate::ymd(date),
warning = function(w) { stop("Hi! date is not in the YYYY-MM-DD format.") }
)
# if warning above stop executing, otherwise continue
print(date_check)