File: read_yaml.R

package info (click to toggle)
r-cran-yaml 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 732 kB
  • sloc: ansic: 9,425; sh: 14; makefile: 7
file content (38 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
`read_yaml` <-
function(file, fileEncoding = "UTF-8", text, error.label, ...) {
  if (missing(file) && !missing(text)) {
    if (missing(error.label)) {
      error.label <- NULL
    }
    file <- textConnection(text, encoding = "UTF-8")
    on.exit(close(file))
  }
  else if (is.character(file)) {
    if (missing(error.label)) {
      error.label <- file
    }
    file <- if (nzchar(fileEncoding))
      file(file, "rt", encoding = fileEncoding)
    else file(file, "rt")
    on.exit(close(file))
  }
  else if (inherits(file, "connection")) {
    if (missing(error.label)) {
      # try to guess filename
      s <- try(summary(file), silent = TRUE)
      if (!inherits(s, "try-error") && is.list(s) && "description" %in% names(s)) {
        error.label <- s$description
      }
    }

    if (!isOpen(file, "rt")) {
      open(file, "rt")
      on.exit(close(file))
    }
  } else {
    stop("'file' must be a character string or connection")
  }

  string <- paste(readLines(file), collapse="\n")
  yaml.load(string, error.label = error.label, ...)
}