knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

path.chain

CRAN status Documentation Travis build status AppVeyor build status Buy hex
stciker

Concise structure for chainable paths

Installation

# 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.

rstudio

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)

# Sample structure we've already created looks as follows
fs::dir_tree(tmp)

# Loading stucture
file.structure <- path_chain(tmp)
file.structure$data$example1.RData

# Loading stucture with naming convention
file.structure <- path_chain(tmp, naming = naming_k)
file.structure$kData$kExample1

# 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)

k.dirs$kData$.
k.dirs$kData$kExample1

Path validation

old.options <- options()
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
root$data$level2.b
options(old.options)


krzjoa/path.chain documentation built on Oct. 9, 2024, 5:49 a.m.