Write to YAML using package yaml

# writing to a file connection
library(yaml)
con <- file("data.yml", "w")
write_yaml(data.frame(a=1:10, b=letters[1:10], c=11:20), con)
read_yaml("data.yml")
data_li <- read_yaml("data.yml")
data_li
# modify field "a"
data_li$a <- seq(21,30)
data_li
cat(as.yaml(data.frame(a=21:30, b=letters[1:10], c=11:20)))
# writing modified list
# write_yaml(data_li, con)
write_yaml(data.frame(a=21:30, b=letters[1:10], c=11:20), con)
# 
yaml.load_file("data.yml", error.label = "duplicate labels")
  yaml.load("- hey\n- hi\n- hello")
  yaml.load("foo: 123\nbar: 456")
  yaml.load("- foo\n- bar\n- 3.14")
  yaml.load("foo: bar\n123: 456", as.named.list = FALSE)
  # reading from a file (uses readLines internally)
  filename <- tempfile()
  cat("foo: 123", file=filename, sep="\n")
  yaml.load_file(filename)
# handling custom types
yaml.load("!sqrt 555", handlers=list(sqrt=function(x) { sqrt(as.integer(x)) }))
yaml.load("!bar\none: 1\ntwo: 2", handlers=list(foo=function(x) { x$one <- "one"; x }))
yaml.load("!foo\n- 1\n- 2", handlers=list(foo=function(x) { as.integer(x) + 1 }))


f0nzie/blogdownPageBundle documentation built on June 29, 2020, 12:53 a.m.