r-yaml icon indicating copy to clipboard operation
r-yaml copied to clipboard

Convert yes/no to true/false

Open royfrancis opened this issue 2 years ago • 6 comments

write_yaml() seems to be converting boolean values to yes/no. How can I leave this as it is. ie: true/false?

yaml 2.3.7

royfrancis avatar Sep 15 '23 10:09 royfrancis

This is the #1 requested issue on this package. The root of the problem is the c library it's based on is an implementation of the YAML specification for 1.1 (1.2 is the current spec). Switching out the library is non-trivial. Please search the issues on this package as it's been discussed multiple times. Short answer: use a handler.

spgarbet avatar Sep 15 '23 13:09 spgarbet

I can see that it has been discussed many times but I don't see a solution other than the word "handler". I don't see any documentation or any explanation for how handler works. Is there a reproducible example somewhere?

royfrancis avatar Sep 15 '23 13:09 royfrancis

Here's an example:

yaml::as.yaml(c(TRUE, TRUE), handlers = list(logical = function(x) {
   value <- ifelse(x, "true", "false")
   structure(value, class = "verbatim")
}))

kevinushey avatar Oct 03 '23 17:10 kevinushey

@cderv and @kevinushey please look over https://github.com/vubiostat/r-yaml/pull/133 and see if this fits your needs?

spgarbet avatar Oct 31 '23 13:10 spgarbet

LGTM. I used almost the same function in quarto R package for handle this problem.

cderv avatar Oct 31 '23 15:10 cderv

LGTM, but honestly I think it'd be better to just document this handler rather than provide and export it, just because the set of exported yaml functions is (probably intentionally) fairly small, and this function doesn't really fit with the other family of functions.

kevinushey avatar Oct 31 '23 16:10 kevinushey