Convert a YAML string into R objects

Parse a YAML string and return R objects.

manip, programming, data
yaml.load(string, as.named.list = TRUE, handlers = NULL)
  yaml.load_file(input, ...)
the YAML string to be parsed
whether or not to return a named list for maps (TRUE by default)
named list of custom handler functions for YAML types (see Details).
a filename or connection
arguments to pass to yaml.load

Use yaml.load to load a YAML string. For files and connections, use yaml.load_file, which calls yaml.load with the contents of the specified file or connection.

Sequences of uniform data (i.e. a sequence of integers) are converted into vectors. If the sequence is not uniform, it's returned as a list. Maps are converted into named lists by default, and all the keys in the map are converted to strings. If you don't want the keys to be coerced into strings, set as.named.list to FALSE. When it's FALSE, a list will be returned with an additional attribute named 'keys', which is a list of the un-coerced keys in the map (in the same order as the main list).

You can specify custom handler functions via the handlers argument. This argument must be a named list of functions, where the names are the YAML types (i.e., 'int', 'float', 'seq', etc). The functions you provide will be passed one argument. Custom handler functions for string types (all types except sequence and map) will receive a character vector of length 1. Custom sequence functions will be passed a list of objects. Custom map functions will be passed the object that the internal map handler creates, which is either a named list or a list with a 'keys' attribute (depending on as.named.list). ALL functions you provide must return an object. See the examples for custom handler use.

This function uses the YAML parser provided by libyaml, which conforms to the YAML 1.1 specification.


  • If the root YAML object is a map, a named list or list with an attribute of 'keys' is returned. If the root object is a sequence, a list or vector is returned, depending on the contents of the sequence. A vector of length 1 is returned for single objects.


YAML: http://yaml.org

libyaml: http://pyyaml.org/wiki/LibYAML

See Also


  • yaml.load
  • yaml.load_file
library(yaml) yaml.load("- hey - hi - hello") yaml.load("foo: 123 bar: 456") yaml.load("- foo - bar - 3.14") yaml.load("foo: bar 123: 456", as.named.list = FALSE) # reading from a file (uses readLines internally) cat("foo: 123", file="foo.yml", sep="\n") yaml.load_file('foo.yml') unlink("foo.yml") # tidy up # custom scalar handler my.float.handler <- function(x) { as.numeric(x) + 123 } yaml.load("123.456", handlers=list("float#fix"=my.float.handler)) # custom sequence handler yaml.load("- 1 - 2 - 3", handlers=list(seq=function(x) { as.integer(x) + 3 })) # custom map handler yaml.load("foo: 123", handlers=list(map=function(x) { x$foo <- x$foo + 123; x })) # handling custom types yaml.load("!sqrt 555", handlers=list(sqrt=function(x) { sqrt(as.integer(x)) })) yaml.load("!foo - 1 - 2", handlers=list(foo=function(x) { as.integer(x) + 1 })) yaml.load("!bar one: 1 two: 2", handlers=list(foo=function(x) { x$one <- "one"; x })) # loading R expressions doc <- yaml.load("inc: !expr function(x) x + 1") doc$inc(1)
Documentation reproduced from package yaml, version 2.1.1, License: BSD

Community examples

Looks like there are no examples yet.