Learn R Programming

path.chain

Concise structure for chainable paths

Installation

path.chain is available on CRAN, so you can install it using simply:

install.packages('path.chain')

Dev version is available on GitHub:

# install.packages("devtools")
devtools::install_github("krzjoa/path.chain")

Example

If you are using RStudio, you know that among many excellent features of this IDE there is a path autocompletion.

However, you can also meet situations, when that may be not enough. Most of all, I mean bigger projects, where you store a complex file structure in the config file. You can handle such configuration YAML file using the library named config. You may encounter a situation, when you’ll want to save current directory structure in this config.

Basic usage

library(magrittr)
library(path.chain)

# Create an example file stucture
tmp <- create_temp_dir("files")
create_sample_dir(tmp, override = TRUE)
#> [1] TRUE

# Sample structure we've already created looks as follows
fs::dir_tree(tmp)
#> /tmp/RtmpRSpDrI/files
#> ├── data
#> │   ├── example1.RData
#> │   ├── example2.RData
#> │   └── persons.csv
#> └── docs
#>     └── schema.txt

# Loading stucture
file.structure <- path_chain(tmp)
file.structure$data$example1.RData
#> [1] "files/data/example1.RData"

# Loading stucture with naming convention
file.structure <- path_chain(tmp, naming = naming_k)
file.structure$kData$kExample1
#> [1] "files/data/example1.RData"

# Saving file structure
file.structure %>% 
  as_config(root.name = "kRoot", wrap = "kDirs") %>%  # Required by `{config}` package
  yaml::write_yaml(temp_path("config.yaml"))
default:
  kDirs:
    kRoot: files/
    kData:
      kRoot: data/
      kExample1: kExample1
      kExample2: kExample2
      kPersons: kPersons
    kDocs:
      kRoot: docs/
      kSchema: kSchema

Loading config file

k.dirs <- config::get("kDirs", "default", temp_path("config.yaml")) %>% 
  as_path_chain()

class(k.dirs)
#> [1] "path_chain"

k.dirs$kData$.
#> [1] "files/data/"
k.dirs$kData$kExample1
#> [1] "files/data/example1.RData"

Path validation

on_path_not_exists(~ print("Path {.x} not exists"))
is_path_valid <- function(x) if (!grepl("\\.fst", x)) print("Invalid file")
on_validate_path(is_path_valid)

level2.b <- path_link("fileA.RData")
level2.a <- path_link("fileB.fst")
level1   <- path_link("data", list(level2.a = level2.a , level2.b = level2.b))
root     <- path_link("files", list(level1))

root$data$level2.a
#> [1] "Path {.x} not exists"
#> [1] "files/data/fileB.fst"
root$data$level2.b
#> [1] "Path {.x} not exists"
#> [1] "Invalid file"
#> [1] "files/data/fileA.RData"

Copy Link

Version

Install

install.packages('path.chain')

Monthly Downloads

206

Version

1.0.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Krzysztof Joachimiak

Last Published

October 8th, 2024

Functions in path.chain (1.0.0)

temp_path

Construct path to file in a temporary directory
create_temp_dir

Create temporary diectory and return its name
on_path_not_exists

Function called if path does not exists
create_sample_dir

Create sample directory
as_config

Prepare list to be saved as config .yaml file
file_path

Construct path to file without doubled separators
path_link

Creates a link of path chain - a directory or a file
path.chain-package

path.chain: Concise Structure for Chainable Paths
naming_k

Naming convention, which adds k prefix for each key, capitalizes and removes file extension
as_path_chain

Create chainable path
$.path_chain

Access path_chain object
as.list

Convert object of type `path_chain` to list
path_children

Get children nodes, i.e. all the suddiectories in the given directory
full_path_chain

Full path chain
print

Print path_chain object
on_validate_path

Function called to validate path correctness
path_chain

Get directory structure and create path_chain object