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

Key `n` is mapped to `FALSE` in simple example

Open ramiromagno opened this issue 1 year ago • 9 comments

{yaml} package version is 2.3.6.

library(yaml)

x <- "
n: 100
"
  yaml.load(x)
#> $`FALSE`
#> [1] 100

ramiromagno avatar Oct 18 '22 17:10 ramiromagno

Okay, I just realised there are some special values in YAML. n stands for "not", is it? It seems that quoting it works as I expected. Is that the way to escape its special meaning?

I'd prefer to not force the user to quote the data in the YAML file. I can I somehow tell yaml.load_file() to treat n literally?

ramiromagno avatar Oct 18 '22 17:10 ramiromagno

This is a quirk of yaml 1.1. It's removed for yaml 1.2, but efforts to get that up and going aren't going so fast.

spgarbet avatar Oct 18 '22 18:10 spgarbet

It might be possible to write a custom boolean handler. I've not tried that.

The parser returns a "bool#yes" and "bool#no" for these tag types. It might be possible to modify this behavior.

Try writing a custom handler and post your code here. If that works, I'll see if it can become an option to ignore in the parser output.

spgarbet avatar Oct 18 '22 18:10 spgarbet

This seems to work:

yaml.load("'n': false\ntrue: 200", 
                handlers=list(boolean=
                    function(x) if(x %in% c('n','y')) x else tolower(x)=='true'))

spgarbet avatar Oct 18 '22 18:10 spgarbet

Thanks for your answers. But it seems that only works because the input has n quoted, i.e. 'n'.

ramiromagno avatar Oct 18 '22 19:10 ramiromagno

That slipped through in my example. I just tried it and upstream it's emitted a T/F for this, so I don't see an easy way. It'll require compiler modification.

spgarbet avatar Oct 19 '22 00:10 spgarbet

Okay, thanks for trying. I'll leave this issue open, but feel free to close it if you think this functionality is unlikely to be supported.

ramiromagno avatar Oct 19 '22 00:10 ramiromagno

It is the plurality of issues opened. The other being requests for yaml 1.2 (which doesn't have the n/y issue). I've started an experiment to use the libfyaml library and see if it solves both problems.

spgarbet avatar Oct 19 '22 14:10 spgarbet