Title: | Concise Structure for Chainable Paths |
---|---|
Description: | Provides path_chain class and functions, which facilitates loading and saving directory structure in YAML configuration files via 'config' package. The file structure you created during exploration can be transformed into legible section in the config file, and then easily loaded for further usage. |
Authors: | Krzysztof Joachimiak [aut, cre] , Peter Wurm [ctb] |
Maintainer: | Krzysztof Joachimiak <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-12-08 07:15:53 UTC |
Source: | CRAN |
The aim of this package is to provide tools, which allow us to represent directory structure as nested R objects. It can be easily saved as .yaml files so that we can later load it and use in our project.
Krzysztof Joachimiak
Useful links:
Report bugs at https://github.com/krzjoa/path.chain/issues
Access path_chain object
## S3 method for class 'path_chain' node$child
## S3 method for class 'path_chain' node$child
node |
path_chain |
child |
nested path_chain name |
path_chain or character, if path indicates leaf of structure tree
#' If we want to create our chain manually, we have start from the leaves level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) # Print root path root$. # Print file path using chaining root$data$level2.a
#' If we want to create our chain manually, we have start from the leaves level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) # Print root path root$. # Print file path using chaining root$data$level2.a
This function is provided to keep compatibility with '{config}' package, which requires existence of default key. Additionally, we can at once wrap our structure with some other keys, in order to not to mix directory structure with different keys.
as_config(x, config = "default", wrap = "dirs", ...) ## S3 method for class 'path_chain' as_config(x, config = "default", wrap = "dirs", ..., root.name = "root.dir") ## S3 method for class 'list' as_config(x, config = "default", wrap = "dirs", ...)
as_config(x, config = "default", wrap = "dirs", ...) ## S3 method for class 'path_chain' as_config(x, config = "default", wrap = "dirs", ..., root.name = "root.dir") ## S3 method for class 'list' as_config(x, config = "default", wrap = "dirs", ...)
x |
list with directory structure |
config |
configuration name |
wrap |
key name to wrap directory structure |
... |
additional arguments (not used at the moment) |
root.name |
key for root directory (for path_chain only) |
list compatible with '{config}' package
library(magrittr) # Initializing sample directory tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) full_path_chain(tmp, "kRoot", naming_k) %>% list(kDirs = .) %>% list(default = .) %>% yaml::write_yaml(temp_path("config.yaml")) # We can simply use such function full_path_chain(tmp, "kRoot", naming_k) %>% as_config("default", "kDirs") %>% yaml::write_yaml(temp_path("config.yaml"))
library(magrittr) # Initializing sample directory tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) full_path_chain(tmp, "kRoot", naming_k) %>% list(kDirs = .) %>% list(default = .) %>% yaml::write_yaml(temp_path("config.yaml")) # We can simply use such function full_path_chain(tmp, "kRoot", naming_k) %>% as_config("default", "kDirs") %>% yaml::write_yaml(temp_path("config.yaml"))
This function always treats first object in the nested list as a subdirectory root path
as_path_chain(nested.list, root.name = "kRoot")
as_path_chain(nested.list, root.name = "kRoot")
nested.list |
'list' object with nested lists/strings inside |
root.name |
key for root directory |
path_chain object
library(magrittr) # Manually created nested list nested.list <- list(kRoot = "root", "file1.txt", list("subdir", "file2.csv")) chainable.path <- as_path_chain(nested.list) class(chainable.path) chainable.path$. chainable.path$subdir$files2.csv # Nested list from config file tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) path_chain(tmp, naming = naming_k) %>% as.list(root.name = "kRoot") %>% as_config("default", "kDirs") %>% yaml::write_yaml(temp_path("config.yaml")) chainable.path <- config::get("kDirs", "default", temp_path("config.yaml")) %>% as_path_chain() class(chainable.path) chainable.path$. chainable.path$kData$kExample1
library(magrittr) # Manually created nested list nested.list <- list(kRoot = "root", "file1.txt", list("subdir", "file2.csv")) chainable.path <- as_path_chain(nested.list) class(chainable.path) chainable.path$. chainable.path$subdir$files2.csv # Nested list from config file tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) path_chain(tmp, naming = naming_k) %>% as.list(root.name = "kRoot") %>% as_config("default", "kDirs") %>% yaml::write_yaml(temp_path("config.yaml")) chainable.path <- config::get("kDirs", "default", temp_path("config.yaml")) %>% as_path_chain() class(chainable.path) chainable.path$. chainable.path$kData$kExample1
Convert object of type 'path_chain' to list
## S3 method for class 'path_chain' as.list(x, ..., root.name = "root.dir")
## S3 method for class 'path_chain' as.list(x, ..., root.name = "root.dir")
x |
a path_chain object |
... |
elipsis for API consistency, does nothing |
root.name |
key for root directory; default: 'root.dir' |
tmp <- create_temp_dir("files") create_sample_dir(tmp) path.chain <- path_chain(tmp) as.list(path.chain)
tmp <- create_temp_dir("files") create_sample_dir(tmp) path.chain <- path_chain(tmp) as.list(path.chain)
Creates sample nested directory to test and learn path.chain package
create_sample_dir(path = "files", override = FALSE)
create_sample_dir(path = "files", override = FALSE)
path |
path for the new sample folder |
override |
boolean: override folder if it already exists |
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) list.files(tmp, all.files = TRUE, recursive = TRUE, include.dirs = TRUE) fs::dir_tree(tmp)
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) list.files(tmp, all.files = TRUE, recursive = TRUE, include.dirs = TRUE) fs::dir_tree(tmp)
Create temporary diectory and return its name
create_temp_dir( ..., warn = FALSE, recursive = FALSE, fsep = .Platform$file.sep )
create_temp_dir( ..., warn = FALSE, recursive = FALSE, fsep = .Platform$file.sep )
... |
arbitrary character objects |
warn |
warn, if folder already exists |
recursive |
ogical. Should elements of the path other than the last be created? If true, like the Unix command mkdir -p |
fsep |
the path separator to use |
# Simply create and return temporal directory create_temp_dir() # Create temp dir and return concatenated path # Keep in mind, that 'files' and 'report_2020.xls' will not be created. create_temp_dir("files", "report_2020.xls")
# Simply create and return temporal directory create_temp_dir() # Create temp dir and return concatenated path # Keep in mind, that 'files' and 'report_2020.xls' will not be created. create_temp_dir("files", "report_2020.xls")
Construct path to file without doubled separators
file_path(..., fsep = .Platform$file.sep)
file_path(..., fsep = .Platform$file.sep)
... |
character vectors |
fsep |
the path separator to use |
character file path
file.path("files/", "data/", "cars.RData") file_path("files/", "data/", "cars.RData")
file.path("files/", "data/", "cars.RData") file_path("files/", "data/", "cars.RData")
'full_path_chain' represents another approach to creating chainable paths In contrast to 'path_chain', this functon creates just a list with nested list with full paths as a leaves.
full_path_chain(path = ".", root.name = ".", naming = basename)
full_path_chain(path = ".", root.name = ".", naming = basename)
path |
root path |
root.name |
naming convention for root directory |
naming |
naming function |
list of lists and character objects
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) chainable.path <- full_path_chain(tmp) chainable.path
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) chainable.path <- full_path_chain(tmp) chainable.path
Naming convention, which adds k prefix for each key, capitalizes and removes file extension
naming_k(path)
naming_k(path)
path |
full path or its element |
library(magrittr) naming_k("path/to/myfile.txt") # Using with full_path_chain tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) full.path.chain <- full_path_chain(tmp, naming = naming_k) full.path.chain tmp <- create_temp_dir("files") create_sample_dir(tmp) # Using with path_chain / create_path_chain path.chain <- path_chain(tmp, naming = naming_k) path.chain %>% as.list()
library(magrittr) naming_k("path/to/myfile.txt") # Using with full_path_chain tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) full.path.chain <- full_path_chain(tmp, naming = naming_k) full.path.chain tmp <- create_temp_dir("files") create_sample_dir(tmp) # Using with path_chain / create_path_chain path.chain <- path_chain(tmp, naming = naming_k) path.chain %>% as.list()
Function called if path does not exists
on_path_not_exists(fun)
on_path_not_exists(fun)
fun |
a function, one-side formula or NULL; if missing, returns value of the path.chain.on.path.not.exists option |
# We'll create an options backup for this example old.options <- options() on_path_not_exists(print) on_path_not_exists() options(old.options)
# We'll create an options backup for this example old.options <- options() on_path_not_exists(print) on_path_not_exists() options(old.options)
Function called to validate path correctness
on_validate_path(fun)
on_validate_path(fun)
fun |
a function; if missing, returns value of the path.chain.on.path.not.exists option |
# We'll create an options backup for this example old.options <- options() is_path_valid <- function(x) grepl("\\.fst", x) on_validate_path(is_path_valid) on_validate_path() options(old.options)
# We'll create an options backup for this example old.options <- options() is_path_valid <- function(x) grepl("\\.fst", x) on_validate_path(is_path_valid) on_validate_path() options(old.options)
Returns 'path_chain' object, which reflects structure of the folder passed with 'path' param
path_chain(path, naming = basename, levels = +Inf, only.directories = FALSE)
path_chain(path, naming = basename, levels = +Inf, only.directories = FALSE)
path |
root of the directory structure |
naming |
function which defines naming convention |
levels |
number of hierarchy levels that recursion should go deep; defaults to +Inf |
only.directories |
boolean to ignore files and only considers directories. |
path_chain object
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) chainable.path <- path_chain(tmp) chainable.path$data$persons.csv # With customized naming convention chainable.path <- path_chain(tmp, naming = naming_k) chainable.path$kData$kPersons
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) fs::dir_tree(tmp) chainable.path <- path_chain(tmp) chainable.path$data$persons.csv # With customized naming convention chainable.path <- path_chain(tmp, naming = naming_k) chainable.path$kData$kPersons
Get children nodes, i.e. all the suddiectories in the given directory
path_children(path.chain)
path_children(path.chain)
path.chain |
object of 'path_chain' class |
a list of 'path_chain' objects
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) path.chain <- path_chain(tmp) path_children(path.chain)
tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) path.chain <- path_chain(tmp) path_children(path.chain)
It returns basic package's object: an object representing a link in the chain. Each link has the path_chain class - it can represents a one-element path chain
path_link(node = NULL, children = NULL)
path_link(node = NULL, children = NULL)
node |
Current node name; character |
children |
list of children - path_chains |
path_chain object
# If we want to create our chain manually, we have start from the leaves level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) # Print root path root$. # Print file path using chaining root$data$level2.a
# If we want to create our chain manually, we have start from the leaves level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) # Print root path root$. # Print file path using chaining root$data$level2.a
Print path_chain object
## S3 method for class 'path_chain' print(x, ...)
## S3 method for class 'path_chain' print(x, ...)
x |
'path_chain' object |
... |
elipsis for API consistency, does nothing |
level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) print(root) tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) chainable.path <- path_chain(tmp) print(chainable.path)
level2.b <- path_link("fileA.RData") level2.a <- path_link("fileB.RData") level1 <- path_link("data", list(level2.a = level2.a , level2.b = level2.b)) root <- path_link("files", list(level1)) print(root) tmp <- create_temp_dir("files") create_sample_dir(tmp, override = TRUE) chainable.path <- path_chain(tmp) print(chainable.path)
Construct path to file in a temporary directory
temp_path(..., fsep = .Platform$file.sep)
temp_path(..., fsep = .Platform$file.sep)
... |
arbitrary character objects |
fsep |
the path separator to use. |
Be careful: if you call this function, it only creates a path for temporary file/dir. All the rest has to be created on your own, e.g. calling dir.create function.
a path
temp_path("files", "report.csv")
temp_path("files", "report.csv")