These function extracts all YAML fragments from a file and then
calls yaml::yaml.load()
to parse them. It then returns a list
of the parsed fragments.
load_yaml_fragments(
text,
file,
yamlFragments = NULL,
select = ".*",
delimiterRegEx = "^---$",
ignoreOddDelimiters = FALSE,
encoding = "UTF-8",
silent = TRUE
)
Arguments
text |
As text or file , you can specify a file to read with
encoding encoding , which will then be read using base::readLines() . If the
argument is named text , whether it is the path to an existing file is checked
first, and if it is, that file is read. If the argument is named file , and it
does not point to an existing file, an error is produced (useful if calling
from other functions). A text should be a character vector where every
element is a line of the original source (like provided by base::readLines() );
although if a character vector of one element and including at least one
newline character (\\n ) is provided as text , it is split at the newline
characters using base::strsplit() . Basically, this behavior means that the
first argument can be either a character vector or the path to a file; and if
you're specifying a file and you want to be certain that an error is thrown if
it doesn't exist, make sure to name it file . |
file |
As text or file , you can specify a file to read with
encoding encoding , which will then be read using base::readLines() . If the
argument is named text , whether it is the path to an existing file is checked
first, and if it is, that file is read. If the argument is named file , and it
does not point to an existing file, an error is produced (useful if calling
from other functions). A text should be a character vector where every
element is a line of the original source (like provided by base::readLines() );
although if a character vector of one element and including at least one
newline character (\\n ) is provided as text , it is split at the newline
characters using base::strsplit() . Basically, this behavior means that the
first argument can be either a character vector or the path to a file; and if
you're specifying a file and you want to be certain that an error is thrown if
it doesn't exist, make sure to name it file . |
yamlFragments |
A character vector of class yamlFragment where
every element corresponds to one line of the YAML fragments, or a list
of multiple such character vectors (of class yamlFragments ). Specify
either yamlFragments (which, if specified, takes precedence over file
and text ), file , or text (file takes precedence over text ). |
select |
A vector of regular expressions specifying object names
to retain. The default (.* ) matches everything, so by default, all
objects are retained. |
delimiterRegEx |
The regular expression used to locate YAML
fragments. |
ignoreOddDelimiters |
Whether to throw an error (FALSE) or
delete the last delimiter (TRUE) if an odd number of delimiters is
encountered. |
encoding |
The encoding to use when calling readLines() . Set to
NULL to let readLines() guess. |
silent |
Whether to be silent (TRUE ) or informative (FALSE ). |
Value
A list of objects, where each object corresponds to one
YAML fragment from the source file or text. If the convention of
the rock
, dct
and justifier
packages is followed, each object
in this list contains one or more named objects (lists), where the
name indicated the type of information contained. Each of those
objects (lists) then contains one or more objects of that type,
such as metadata or codes for rock
, a decentralized construct
taxonomy element for dct
, and a justification for justifier
.
Examples
yum::load_yaml_fragments(text="
---
-
id: firstFragment
---
Outside of YAML
---
-
id: secondFragment
parentId: firstFragment
---
Also outside of YAML");
#> [[1]]
#> [[1]][[1]]
#> [[1]][[1]]$id
#> [1] "firstFragment"
#>
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [[2]][[1]]$id
#> [1] "secondFragment"
#>
#> [[2]][[1]]$parentId
#> [1] "firstFragment"
#>
#>
#>
#> attr(,"class")
#> [1] "yumFromFile" "list"