What is YAML?
YAML is a human-readable data serialization language. With it, you can create easily readable documents that can be consumed by a variety of programming languages.
For example, here’s a map of baseball teams per league:
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta BravesAnd a data dictionary specification:
Parsing YAML
yaml.load() parses a YAML document from a string:
yaml.load("
- 1
- 2
- 3
")
#> [1] 1 2 3yaml.load_file() and read_yaml() read YAML
from a file or connection.
Scalars
A YAML scalar is the basic building block of YAML documents. The parser automatically determines the type:
Sequences
A YAML sequence is a list of elements. If all elements are uniform,
yaml.load() returns a vector of that type. Otherwise, it
returns a list:
Maps
A YAML map is a list of paired keys and values. By default,
yaml.load() returns a named list:
yaml.load("
one: 1
two: 2
three: 3
")
#> $one
#> [1] 1
#>
#> $two
#> [1] 2
#>
#> $three
#> [1] 3Since YAML map keys can be almost anything (not just strings), you
can preserve the data type of keys with
as.named.list = FALSE. This creates a keys
attribute instead of coercing keys to strings:
yaml.load("
1: one
2: two
", as.named.list = FALSE)
#> [[1]]
#> [1] "one"
#>
#> [[2]]
#> [1] "two"
#>
#> attr(,"keys")
#> attr(,"keys")[[1]]
#> [1] 1
#>
#> attr(,"keys")[[2]]
#> [1] 2Custom handlers
You can customize parsing with handler functions. Handlers are passed
to yaml.load() as a named list, where each name is the YAML
type to handle:
# Add 100 to all integers
yaml.load("123", handlers = list(int = function(x) as.integer(x) + 100))
#> [1] 223Sequence handlers
Sequence handlers receive a list and can transform it:
yaml.load("
- 1
- 2
- 3
", handlers = list(seq = function(x) sum(as.numeric(x))))
#> [1] 6Map handlers
Map handlers receive a named list (or a list with a keys
attribute):
yaml.load("
a:
- 1
- 2
b:
- 3
- 4
", handlers = list(map = function(x) as.data.frame(x)))
#> a b
#> 1 1 3
#> 2 2 4Emitting YAML
as.yaml() converts R objects to YAML strings:
write_yaml() writes the result directly to a file or
connection.
Formatting options
indent.mapping.sequence
By default, sequences within a mapping are not indented:
Set indent.mapping.sequence = TRUE to indent them:
column.major
Controls how data frames are converted. When TRUE
(default), data frames are emitted column-wise:
x <- data.frame(a = 1:2, b = 3:4)
cat(as.yaml(x, column.major = TRUE))
#> a:
#> - 1
#> - 2
#> b:
#> - 3
#> - 4When FALSE, data frames are emitted row-wise:
Verbatim text
Character vectors with class "verbatim" are not quoted
except when required by the YAML specification: