Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

read_yaml

Read a YAML file


Description

Read a YAML document from a file and create an R object from it

Usage

read_yaml(file, fileEncoding = "UTF-8", text, error.label, ...)

Arguments

file

either a character string naming a file or a connection open for writing

fileEncoding

character string: if non-empty declares the encoding used on a file (not a connection) so the character data can be re-encoded. See file.

text

character string: if file is not supplied and this is, then data are read from the value of text via a text connection. Notice that a literal string can be used to include (small) data sets within R code.

error.label

a label to prepend to error messages (see Details).

...

arguments to pass to yaml.load

Details

This function is a convenient wrapper for yaml.load and is a nicer alternative to yaml.load_file.

You can specify a label to be prepended to error messages via the error.label argument. If error.label is missing, read_yaml will make an educated guess for the value of error.label by either using the specified filename (when file is a character vector) or using the description of the supplied connection object (via the summary function). If text is used, the default value of error.label will be NULL.

Value

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.

Author(s)

Jeremy Stephens <jeremy.f.stephens@vumc.org>

References

YAML: http://yaml.org

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

See Also

Examples

## Not run: 
  # reading from a file connection
  filename <- tempfile()
  cat("test: data\n", file = filename)
  con <- file(filename, "r")
  read_yaml(con)
  close(con)

  # using a filename to specify input file
  read_yaml(filename)

## End(Not run)

  # reading from a character vector
  read_yaml(text="- hey\n- hi\n- hello")

yaml

Methods to Convert R Data to YAML and Back

v2.2.1
BSD_3_clause + file LICENSE
Authors
Jeremy Stephens [aut, cre], Kirill Simonov [aut], Yihui Xie [ctb], Zhuoer Dong [ctb], Hadley Wickham [ctb], Jeffrey Horner [ctb], reikoch [ctb], Will Beasley [ctb], Brendan O'Connor [ctb], Gregory R. Warnes [ctb]
Initial release
2020-01-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.