| Title: | Fast 'YAML' 1.2 Parser and Formatter |
|---|---|
| Description: | A fast, correct, safe, and ergonomic 'YAML' 1.2 parser and generator written in 'Rust'. Convert between 'YAML' and simple 'R' objects with full support for multi-document streams, tags, anchors, and aliases. Offers opt-in handlers for custom tag behavior and round-trips common 'R' data structures. Implements the 'YAML' 1.2.2 specification from the 'YAML' Language Development Team (2021) <https://yaml.org/spec/1.2.2/>. Proudly supported by Posit. |
| Authors: | Tomasz Kalinowski [aut, cre], Posit Software, PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>), Authors of the dependency Rust crates [cph] (See inst/AUTHORS and LICENSE.note for vendored Rust dependency authors and licenses.) |
| Maintainer: | Tomasz Kalinowski <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-05-09 07:52:06 UTC |
| Source: | https://github.com/posit-dev/r-yaml12 |
format_yaml() returns YAML as a character string. write_yaml() writes a
YAML stream to a file or stdout and always emits document start (---)
markers and a final end (...) marker. Both functions honor a yaml_tag
attribute on values (see examples).
format_yaml(value, multi = FALSE) write_yaml(value, path = NULL, multi = FALSE)format_yaml(value, multi = FALSE) write_yaml(value, path = NULL, multi = FALSE)
value |
Any R object composed of lists, atomic vectors, and scalars. |
multi |
When |
path |
Scalar string file path to write YAML to when using |
format_yaml() returns a scalar character string containing YAML.
write_yaml() invisibly returns value.
cat(format_yaml(list(foo = 1, bar = list(TRUE, NA)))) docs <- list("first", "second") cat(format_yaml(docs, multi = TRUE)) tagged <- structure("1 + 1", yaml_tag = "!expr") cat(tagged_yaml <- format_yaml(tagged), "\n") dput(parse_yaml(tagged_yaml)) write_yaml(list(foo = 1, bar = list(2, "baz"))) write_yaml(list("foo", "bar"), multi = TRUE) tagged <- structure("1 + 1", yaml_tag = "!expr") write_yaml(tagged)cat(format_yaml(list(foo = 1, bar = list(TRUE, NA)))) docs <- list("first", "second") cat(format_yaml(docs, multi = TRUE)) tagged <- structure("1 + 1", yaml_tag = "!expr") cat(tagged_yaml <- format_yaml(tagged), "\n") dput(parse_yaml(tagged_yaml)) write_yaml(list(foo = 1, bar = list(2, "baz"))) write_yaml(list("foo", "bar"), multi = TRUE) tagged <- structure("1 + 1", yaml_tag = "!expr") write_yaml(tagged)
parse_yaml() takes strings of YAML; read_yaml() reads from a file path.
parse_yaml(text, multi = FALSE, simplify = TRUE, handlers = NULL) read_yaml(path, multi = FALSE, simplify = TRUE, handlers = NULL)parse_yaml(text, multi = FALSE, simplify = TRUE, handlers = NULL) read_yaml(path, multi = FALSE, simplify = TRUE, handlers = NULL)
text |
Character vector; elements are concatenated with |
multi |
When |
simplify |
When |
handlers |
Named list of R functions with names corresponding to YAML tags; matching handlers transform tagged values. |
path |
Scalar string path to a YAML file'. |
YAML tags without a corresponding handler are preserved in a yaml_tag attribute.
Mappings with keys that are not all simple scalar strings are returned as a named list with a yaml_keys attribute.
When multi = FALSE, returns a parsed R object for the first document.
When multi = TRUE, returns a list of parsed documents.
dput(parse_yaml("foo: [1, 2, 3]")) # homogeneous sequences simplify by default. # YAML null maps to NA in otherwise homogeneous sequences. dput(parse_yaml("foo: [1, 2, 3, null]")) # mixed type sequence never simplify dput(parse_yaml("[1, true, cat]")) # use `simplify=FALSE` to always return sequences as lists. str(parse_yaml("foo: [1, 2, 3, null]", simplify = FALSE)) # Parse multiple documents when requested. stream <- " --- first: 1 --- second: 2 " str(parse_yaml(stream, multi = TRUE)) # Read from a file; keep sequences as lists. path <- tempfile(fileext = ".yaml") writeLines("alpha: [true, null]\nbeta: 3.5", path) str(read_yaml(path, simplify = FALSE))dput(parse_yaml("foo: [1, 2, 3]")) # homogeneous sequences simplify by default. # YAML null maps to NA in otherwise homogeneous sequences. dput(parse_yaml("foo: [1, 2, 3, null]")) # mixed type sequence never simplify dput(parse_yaml("[1, true, cat]")) # use `simplify=FALSE` to always return sequences as lists. str(parse_yaml("foo: [1, 2, 3, null]", simplify = FALSE)) # Parse multiple documents when requested. stream <- " --- first: 1 --- second: 2 " str(parse_yaml(stream, multi = TRUE)) # Read from a file; keep sequences as lists. path <- tempfile(fileext = ".yaml") writeLines("alpha: [true, null]\nbeta: 3.5", path) str(read_yaml(path, simplify = FALSE))