Title: | Settings and File I/O using a Configuration YAML File |
---|---|
Description: | R data pipelines commonly require reading and writing data to versioned directories. Each directory might correspond to one step of a multi-step process, where that version corresponds to particular settings for that step and a chain of previous steps that each have their own versions. This package creates a configuration object that makes it easy to read and write versioned data, based on YAML configuration files loaded and saved to each versioned folder. |
Authors: | Nathaniel Henry [aut, cre] |
Maintainer: | Nathaniel Henry <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-11-03 07:15:32 UTC |
Source: | CRAN |
Automatically read a file based on extension
autoread(file, ...)
autoread(file, ...)
file |
Full path to be read |
... |
Other arguments to be passed to the particular loading function |
The object loaded by the file
get_file_reading_functions()
autowrite()
Automatically write an object to a file based on extension
autowrite(x, file, ...)
autowrite(x, file, ...)
x |
Object to be saved |
file |
Full path to save the object to |
... |
Other arguments to be passed to the particular saving function |
Invisibly passes TRUE if the file saves successfully
get_file_writing_functions()
autoread()
R6 Class representing a configuration object
R6 Class representing a configuration object
The special sublist directories
is structured to contain three items for each
directory name:
versioned
: a T/F value specifying whether the directory is versioned
path
: the full path to the top level of that directory.
files
: A named list referencing file paths within that directory.
If the directory is versioned, a version must be set in the versions
sublist of the
config list. versions
is itself a named list where each key corresponds to a
versioned folder in directories
and the value gives the particular folder version
(for example, a timestamp) that corresponds to the particular run.
config_list
The list representation of the Config object
new()
Create a new Config object
Config$new(config_list, versions = NULL)
config_list
either a list or a filepath to a YAML file containing that list
versions
(default NULL) A named list containing versions for versioned
directories. If passed, used to define or update items in config_list$versions
.
print()
Print the list representation of the Config object
Config$print()
get()
Get a subset of the config_list
Config$get(...)
...
Nested indices (character or numeric) down the config list
If no parameters are passed, returns the entire config_list
A subset of the list. If the item is NULL or missing, returns an error
get_dir_path()
Construct a directory path from the config object
Config$get_dir_path( dir_name, custom_version = NULL, fail_if_does_not_exist = FALSE )
dir_name
Directory name
custom_version
(character, default NULL) A custom version that will be
applied to this folder, rather than pulling from config_list$versions[[dir]]
.
Only applies to versioned folders.
fail_if_does_not_exist
(logical, default FALSE) should this method return an error if the directory in question does not already exist?
Works differently for versioned and non-versioned directories. See the class description for more information.
The full path to the directory
get_file_path()
Construct a file path from the config object
Config$get_file_path( dir_name, file_name, custom_version = NULL, fail_if_does_not_exist = FALSE )
dir_name
Directory name
file_name
File name within that directory
custom_version
(character, default NULL) A custom version that will be
applied to this folder, rather than pulling from config_list$versions[[dir]]
.
Only applies to versioned folders.
fail_if_does_not_exist
(logical, default FALSE) should this method return an error if the directory in question does not already exist?
Looks for the file path under:
config_list$directories[[dir_name]]$files[[file_name]]
The full path to the file Read a file based on the config
read()
Config$read(dir_name, file_name, ..., custom_version = NULL)
dir_name
Directory name
file_name
File name within that directory
...
Optional file reading arguments to pass to autoread()
custom_version
(character, default NULL) A custom version that will be
applied to this folder, rather than pulling from config_list$versions[[dir]]
.
Only applies to versioned folders. If passed, this argument must always be
explicitly named.
The object loaded by autoread()
Write an object to file based on the config
write()
Config$write(x, dir_name, file_name, ..., custom_version = NULL)
x
Object to write
dir_name
Directory name
file_name
File name within that directory
...
Optional file writing arguments to pass to autowrite()
custom_version
(character, default NULL) A custom version that will be
applied to this folder, rather than pulling from config_list$versions[[dir]]
.
Only applies to versioned folders. If passed, this argument must always be
explicitly named.
Invisibly passes TRUE if successful Convenience function: write the config list to a folder as 'config.yaml'
write_self()
Config$write_self(dir_name, ..., custom_version = NULL)
dir_name
Directory name
...
Optional file writing arguments to pass to autowrite()
custom_version
(character, default NULL) A custom version that will be
applied to this folder, rather than pulling from config_list$versions[[dir]]
.
Only applies to versioned folders. If passed, this argument must always be
explicitly named.
Invisibly passes TRUE if successful
clone()
The objects of this class are cloneable with this method.
Config$clone(deep = FALSE)
deep
Whether to make a deep clone.
Constructs a list of all file-reading functions based on extension
get_file_reading_functions()
get_file_reading_functions()
Named list where the names are file extensions, and the values are functions that read a file. All functions have ... arguments that can be used to extend the basic function.
autoread()
get_file_writing_functions()
Constructs a list of all file-reading functions based on extension
get_file_writing_functions()
get_file_writing_functions()
Named list where the names are file extensions, and the values are functions that read a file. All functions have ... arguments that can be used to extend the basic function.
autoread()
get_file_reading_functions()
Indexing function for a list
pull_from_list(x, ..., fail_if_null = TRUE)
pull_from_list(x, ..., fail_if_null = TRUE)
x |
List to pull items from |
... |
List indices to pull. Can be either numeric or (preferably) a character. |
fail_if_null |
(logical, default TRUE). Returns an informative error message if the list index is NULL. This function must always be named. |
Use the ...
arguments to index the list. Not passing any ...
arguments
will return the entire list. The indexing will fail if either of two conditions are
met:
The index (which can be numeric or a key) does not exist in the list
If the index exists but the value of the item is NULL, and fail_if_null
is TRUE