vubiostat / r-yaml

R package for converting objects to and from YAML
http://biostat.app.vumc.org/wiki/Main/YamlR
Other
163 stars 38 forks source link

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

Open ramiromagno opened 2 years ago

ramiromagno commented 2 years ago

{yaml} package version is 2.3.6.

library(yaml)

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

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?

spgarbet commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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'))
ramiromagno commented 2 years ago

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

spgarbet commented 2 years ago

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.

ramiromagno commented 2 years ago

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.

spgarbet commented 2 years ago

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.

r2evans commented 1 year ago

Related on SO, for the record (if it helps): https://stackoverflow.com/questions/75595253/rmarkdownyaml-front-matter-converts-some-parameter-names-to-true