Description
Hi,
While coercing a duration into a character work, the output of the coercion into a character can't be coerced back into a duration.
(x <- duration(1, "years")) # [1] "31557600s (~1 years)"
(y <- as.character(x)) # [1] "31557600s (~1 years)"
(z <- as.duration(y)) # [1] NA
I understand that the basic purpose of the parser is different and that this behavior is expected. However, I just encountered a situation where this is problematic:
df <- tibble(x = duration(1, "years")) # x is a duration
tmp <- tempfile(); write_csv(df, tmp) # write the df to file
df2 <- read_csv(tmp) # x is now a character which can't be easily coerced
Now, I get that one could just convert a duration as a numeric before writing, via mutate(across(where(is.duration), as.numeric))
or the "old" way mutate_if(is.duration, as.numeric)
. But it seems like an unnecessary burden to the user, especially since this behavior is not, to the best of my knowledge, documented.
I will also file a bug report in readr
. While I understand that readr
can't be expected to know whether to coerce to numeric or character for every object class out there, I would have expected that classes from the tidyverse
packages should be supported seamlessly. However, it seems that the "responsibility" of providing a parser for the duration class should be on lubridate
.
Thoughts?