You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In as.yaml(), setting column.major=FALSE appears to prevent the application of Date handlers. For a while, I thought I was specifying my handler functions incorrectly, but I don't believe that is the case. I used these references for the handlers:
I've included a minimal example showing the difference in date handling when column.major=TRUE (default) vs. when column.major=FALSE. This interaction doesn't appear when using the verbatim_logical handler, making the Date behaviour entirely unexpected.
library(yaml)
library(purrr)
## Working date handlingdate_handler<-list(Date=function(x) as.character(x))
my_list<-list(date= as.Date(c("2012-10-10", "2014-03-28")))
yaml::as.yaml(my_list, handlers=date_handler)
#> [1] "date:\n- '2012-10-10'\n- '2014-03-28'\n"## instead of a list, use tibble and df (with column.major = TRUE by default)my_tbl<-tibble::tibble(date= as.Date(c("2012-10-10", "2014-03-28")))
yaml::as.yaml(my_tbl, handlers=date_handler) |> cat()
#> date:#> - '2012-10-10'#> - '2014-03-28'## but the date_handler doesn't apply with column.major = FALSE?yaml::as.yaml(my_tbl, handlers=date_handler, column.major=FALSE) |> cat()
#> - date: 15623.0#> - date: 16157.0## Same issue with data.framesmy_df<- as.data.frame(my_tbl)
yaml::as.yaml(my_df, handlers=date_handler)
#> [1] "date:\n- '2012-10-10'\n- '2014-03-28'\n"yaml::as.yaml(my_df, handlers=date_handler, column.major=FALSE)
#> [1] "- date: 15623.0\n- date: 16157.0\n"## What if convert the tibble to list first?# purrr::transpose also does the weird number conversionpurrr::transpose(my_tbl)
#> [[1]]#> [[1]]$date#> [1] 15623#> #> #> [[2]]#> [[2]]$date#> [1] 16157# but not if you do as.list() first
as.list(my_tbl) |>purrr::list_transpose(simplify=FALSE)
#> [[1]]#> [[1]]$date#> [1] "2012-10-10"#> #> #> [[2]]#> [[2]]$date#> [1] "2014-03-28"# what about logical handlers?my_tbl$bool<- c(TRUE, FALSE)
verbatim_logical<-function(x) {
result<- tolower(as.logical(x))
class(result) <-"verbatim"return(result)
}
yml_handlers<- c(date_handler, list(logical=verbatim_logical))
# Logical handlers work fine either way, but dates depend on column.majormy_tbl|>yaml::as.yaml(handlers=yml_handlers, column.major=FALSE)
#> [1] "- date: 15623.0\n bool: true\n- date: 16157.0\n bool: false\n"my_tbl|>yaml::as.yaml(handlers=yml_handlers, column.major=TRUE)
#> [1] "date:\n- '2012-10-10'\n- '2014-03-28'\nbool:\n- true\n- false\n"
When running in column.major = FALSE mode. To encode the row it picks that rows element from the each column to encode the row as a list. The problem being that when the element from the picked up the element loses it class information. Thus the element is no longer a Date but instead is a numeric (which is the base class of the Date type). That is why the Date handler doesn't do anything when column.major = FALSE. Boolean is fine because it is a intrinsic data type.
In
as.yaml()
, settingcolumn.major=FALSE
appears to prevent the application of Datehandlers
. For a while, I thought I was specifying my handler functions incorrectly, but I don't believe that is the case. I used these references for the handlers:I've included a minimal example showing the difference in date handling when
column.major=TRUE
(default) vs. whencolumn.major=FALSE
. This interaction doesn't appear when using theverbatim_logical
handler, making the Date behaviour entirely unexpected.Created on 2024-03-18 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: