| 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.2.0.9000 |
| Built: | 2026-09-25 12:00:13 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 starts every document with a document
start (---) marker. Both functions honor a yaml_tag attribute on values
(see examples).
Long single-line strings and multiline strings containing unindented,
single-line paragraphs separated by exactly one blank line are
automatically wrapped when a line would be wider than width columns and
has a safe word boundary. Wrapped strings use a YAML folded block scalar:
>- preserves no final newline, while > preserves exactly one. Paragraph
breaks and all other string content round-trip through parse_yaml()
unchanged. Strings that cannot be folded losslessly use a literal or quoted
representation, and mapping keys are never wrapped.
Literal blocks use explicit indentation indicators when needed to preserve leading spaces or tabs. They keep physical blank lines empty and preserve later-indented lines.
format_yaml(value, multi = FALSE, width = 80L) write_yaml(value, path = NULL, multi = FALSE, append = FALSE, width = 80L)format_yaml(value, multi = FALSE, width = 80L) write_yaml(value, path = NULL, multi = FALSE, append = FALSE, width = 80L)
value |
Any R object composed of lists, atomic vectors, and scalars. |
multi |
When |
width |
Target maximum line width in columns. Long single-line strings
and unindented paragraphs with safe word boundaries are wrapped.
Individual lines may still exceed |
path |
Scalar string file path to write YAML to when using
|
append |
When |
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. Tilde prefixes ( |
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.
YAML 1.2 requires mapping keys to be unique, but parse_yaml() and
read_yaml() do not currently reject duplicates. When a key is repeated
with the same spelling and quoting style, only its last value is retained.
Keys written differently, such as key and "key", may remain as separate
entries even when they produce the same R name. A future release may warn or
error for these cases, so this behavior should not be relied upon.
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))