yaml_load: Read YAML data

View source: R/yaml.R

yaml_loadR Documentation

Read YAML data

Description

If the yaml package is installed, use yaml::yaml.load() to read the data. If not, use a simple parser instead, which only supports a limited number of data types (see “Examples”). In particular, it does not support values that span across multiple lines (such as multi-line text).

Usage

yaml_load(
  x,
  ...,
  handlers = NULL,
  envir = parent.frame(),
  use_yaml = loadable("yaml")
)

Arguments

x

A character vector of YAML data.

..., handlers

Arguments to be passed to yaml::yaml.load().

envir

The environment in which R expressions in YAML are evaluated. To disable the evaluation, use envir = FALSE.

use_yaml

Whether to use the yaml package.

Value

An R object (typically a list).

Note

R expressions in YAML will be returned as expressions when they are not evaluated. This is different with yaml::yaml.load(), which returns character strings for expressions.

Examples

# test the simple parser without using the yaml package
read_yaml = function(...) xfun::yaml_load(..., use_yaml = FALSE)
read_yaml("a: 1")
read_yaml("a: 1\nb: \"foo\"\nc: null")
read_yaml("a:\n  b: false\n  c: true\n  d: 1.234\ne: bar")
read_yaml("a: !expr paste(1:10, collapse = \", \")")
read_yaml("a: [1, 3, 4, 2]")
read_yaml("a: [1, \"abc\", 4, 2]")
read_yaml("a: [\"foo\", \"bar\"]")
read_yaml("a: [true, false, true]")
# the other form of array is not supported
read_yaml("a:\n  - b\n  - c")
# and you must use the yaml package
if (loadable("yaml")) yaml_load("a:\n  - b\n  - c")

yihui/xfun documentation built on April 29, 2024, 12:16 p.m.