Search code examples
ryaml

Custom YAML converts TRUE/FALSE to yes/no


I would like to create a custom YAML with the as.yaml function. Somehow it convert the TRUE/FALSE values to yes/no. Here is some reproducible code:

library(yaml)

x <- list(test = list(a = TRUE))
yaml <- as.yaml(x)
cat(yaml, sep = "\n")
#> test:
#>   a: yes

As you can see the TRUE value is now yes in the yaml. If we use "TRUE" instead of TRUE it is converted to "TRUE" but I don't want it to be in quotes. My expected output looks like this:

test:
  a: TRUE

So I was wondering if anyone knows why this happens and how to fix this?


Solution

  • As mentioned by @CherryDT in the comments, we could use handlers to convert the yes/no to true/false like this:

    library(yaml)
    
    x <- list(test = list(a = TRUE))
    yaml <- as.yaml(x, handlers = list(logical=verbatim_logical))
    cat(yaml, sep = "\n")
    #> test:
    #>   a: true
    

    Created on 2024-06-13 with reprex v2.1.0