Concise structure for chainable paths
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")
::install_github("krzjoa/path.chain") devtools
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.
library(magrittr)
library(path.chain)
# Create an example file stucture
<- create_temp_dir("files")
tmp create_sample_dir(tmp, override = TRUE)
#> [1] TRUE
# Sample structure we've already created looks as follows
::dir_tree(tmp)
fs#> /tmp/RtmpRSpDrI/files
#> ├── data
#> │ ├── example1.RData
#> │ ├── example2.RData
#> │ └── persons.csv
#> └── docs
#> └── schema.txt
# Loading stucture
<- path_chain(tmp)
file.structure $data$example1.RData
file.structure#> [1] "files/data/example1.RData"
# Loading stucture with naming convention
<- path_chain(tmp, naming = naming_k)
file.structure $kData$kExample1
file.structure#> [1] "files/data/example1.RData"
# Saving file structure
%>%
file.structure as_config(root.name = "kRoot", wrap = "kDirs") %>% # Required by `{config}` package
::write_yaml(temp_path("config.yaml")) yaml
default:
kDirs:
kRoot: files/
kData:
kRoot: data/
kExample1: kExample1
kExample2: kExample2
kPersons: kPersons
kDocs:
kRoot: docs/
kSchema: kSchema
<- config::get("kDirs", "default", temp_path("config.yaml")) %>%
k.dirs as_path_chain()
class(k.dirs)
#> [1] "path_chain"
$kData$.
k.dirs#> [1] "files/data/"
$kData$kExample1
k.dirs#> [1] "files/data/example1.RData"
on_path_not_exists(~ print("Path {.x} not exists"))
<- function(x) if (!grepl("\\.fst", x)) print("Invalid file")
is_path_valid on_validate_path(is_path_valid)
<- path_link("fileA.RData")
level2.b <- path_link("fileB.fst")
level2.a <- path_link("data", list(level2.a = level2.a , level2.b = level2.b))
level1 <- path_link("files", list(level1))
root
$data$level2.a
root#> [1] "Path {.x} not exists"
#> [1] "files/data/fileB.fst"
$data$level2.b
root#> [1] "Path {.x} not exists"
#> [1] "Invalid file"
#> [1] "files/data/fileA.RData"