pathr

pathr is a collection of tools to extract, examine, and reconfigure
elements of file paths. The package is born out of a frustration with
finding the right base R tools to grab certain parts of a path.
Often these functions are located in packages that are base install but
not loaded by default (e.g., tools::file_ext). Additionally, many
names of path manipulation functions in base R are longer and thus
often difficult to remember and require more time to type. Still, other
path manipulation tasks had me building my own custom manipulation tools
via strsplit and file.path. pathr is designed to be a consistent
set of tools that allow the user to solve most path related needs simply
by remembering 7 basic sets of parsing and manipulation tools (the first
seven rows in the table of function usage found in the Function
Usage section). The package is designed to be pipeable
(easily used within a magrittr/pipeR pipeline) but is not
required.
Table of Contents
Functions
Functions typically fall into the task category of (1) parsing, (2)
manipulating, (3) examining, & (4) action. The main functions, task
category, & descriptions are summarized in the table below:
| Function |
Task |
Description |
parse_path |
parsing |
Parse path into elements (sub-directories & files) |
front/back |
manipulate |
Get first/last n elements of a path |
index |
manipulate |
Get indexed elements of a path |
before/after |
manipulate |
Get n elements before/after a regex occurrence |
swap/swap_index/swap_regex |
manipulate |
Replace elements of a path |
file_path |
manipulate |
Combine file paths |
expand_path |
manipulate |
Expand tilde prefixed file path |
normalize |
manipulate |
Make all path separators forward slashes |
win_fix |
manipulate |
Replace single backslash with a forward slash |
file_ext/no_file_ext |
manipulate |
Get/remove file extensions |
tree |
examine |
View path structure as an ASCII style tree (experimental) |
indent_path |
examine |
View path hierarchy as an indented list |
copy_path |
action |
Copy path(s) to clipboard |
open_path |
action |
Open path(s) (directories & file) |
Installation
To download the development version of pathr:
Download the zip ball
or tar ball,
decompress and run R CMD INSTALL on it, or use the pacman package
to install the development version:
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/pathr")
Contact
You are welcome to:
Demonstration
Load the Packages/Data
if (!require("pacman")) install.packages("pacman")
pacman::p_load(pathr, magrittr)
data(files)
set.seed(11); (myfiles <- sample(files, 10))
## [1] "~/Packages/qdap/R/cm_range2long.R"
## [2] "~/Packages/qdap/contributors.geojson"
## [3] "~/Packages/qdap/R/mcsv_r.R"
## [4] "~/Packages/qdap/DESCRIPTION"
## [5] "~/Packages/qdap/README.md"
## [6] "~/Packages/qdap/R/wfm.R"
## [7] "~/Packages/qdap/R/adjacency_matrix.R"
## [8] "~/Packages/qdap/R/xnoy.R"
## [9] "~/Packages/qdap/R/termco.R"
## [10] "~/Packages/qdap/R/bag_o_words.R"
Parsing
The parse_path function simply splits an atomic vector of paths into a
list of paths split on the slash separator. For example, my current
working directory, C:/Users/Tyler/GitHub/pathr, becomes:
getwd() %>%
parse_path()
## [[1]]
## [1] "C:" "Users" "Tyler" "GitHub" "pathr"
While this isn't earth shattering it allows the pathr manipulation
functions to extract, replace, and recombine parts of the path
elements into a sub-path. Here I use path to mean the original
path, C:/Users/Tyler/GitHub/pathr. A path is simply a slash separated
mapping of the location of a file or directory within a hierarchical
order of sub-directories. These sub-directories are the elements of
the path. The final output from one of the manipulation functions is a
sub-path of the original at most the same number of elements as the
original.
In this example I parse a multi-path vector:
myfiles %>%
parse_path()
## [[1]]
## [1] "~" "Packages" "qdap" "R"
## [5] "cm_range2long.R"
##
## [[2]]
## [1] "~" "Packages" "qdap"
## [4] "contributors.geojson"
##
## [[3]]
## [1] "~" "Packages" "qdap" "R" "mcsv_r.R"
##
## [[4]]
## [1] "~" "Packages" "qdap" "DESCRIPTION"
##
## [[5]]
## [1] "~" "Packages" "qdap" "README.md"
##
## [[6]]
## [1] "~" "Packages" "qdap" "R" "wfm.R"
##
## [[7]]
## [1] "~" "Packages" "qdap"
## [4] "R" "adjacency_matrix.R"
##
## [[8]]
## [1] "~" "Packages" "qdap" "R" "xnoy.R"
##
## [[9]]
## [1] "~" "Packages" "qdap" "R" "termco.R"
##
## [[10]]
## [1] "~" "Packages" "qdap" "R"
## [5] "bag_o_words.R"
Manipulating
Once the path has been parsed the individual elements can be extracted
and/or replaced to form sub-paths. In this section I break the
manipulation functions into (1) extracting (2) replacing, (3) combining,
and (4) expanding types. There are a few miscellaneous pathr
functions that are not an extracting, replacing, combining, or expanding
tool which will be discussed at the end of the Manipulating section.
Extracting
Extracting can replace path elements by their numeric index or by their
content relative to a matched regular expression. There are three sets
of extracting functions (1) front/back, (2) index, and (3)
before/after. The first two rely on matching elements to their
numeric position while the latter set uses extraction relative to a
regular expression match.
Front & Back
The front/back set of functions works like head/tail (in fact
these functions are used under the hood). The user can select the first
n elements using front or the last n elements using back. These
functions require that users want either the first or last element of
the path in their sub-path.
Here I replicate base R's dirname & basename functions using the
default settings of front & back as dirname is taking
head(x, -1) elements (or all but the last element) and basename is
taking tail(x, 1) (or the last element).
myfiles %>%
parse_path() %>%
front()
## [1] "~/Packages/qdap/R" "~/Packages/qdap" "~/Packages/qdap/R"
## [4] "~/Packages/qdap" "~/Packages/qdap" "~/Packages/qdap/R"
## [7] "~/Packages/qdap/R" "~/Packages/qdap/R" "~/Packages/qdap/R"
## [10] "~/Packages/qdap/R"
myfiles %>%
dirname()
## [1] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [2] "C:/Users/Tyler/Documents/Packages/qdap"
## [3] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [4] "C:/Users/Tyler/Documents/Packages/qdap"
## [5] "C:/Users/Tyler/Documents/Packages/qdap"
## [6] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [7] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [8] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [9] "C:/Users/Tyler/Documents/Packages/qdap/R"
## [10] "C:/Users/Tyler/Documents/Packages/qdap/R"
myfiles %>%
parse_path() %>%
back()
## [1] "cm_range2long.R" "contributors.geojson" "mcsv_r.R"
## [4] "DESCRIPTION" "README.md" "wfm.R"
## [7] "adjacency_matrix.R" "xnoy.R" "termco.R"
## [10] "bag_o_words.R"
myfiles %>%
basename()
## [1] "cm_range2long.R" "contributors.geojson" "mcsv_r.R"
## [4] "DESCRIPTION" "README.md" "wfm.R"
## [7] "adjacency_matrix.R" "xnoy.R" "termco.R"
## [10] "bag_o_words.R"
But the front/back set is more versatile still as demonstrated
below:
myfiles %>%
parse_path() %>%
front(3)
## [1] "~/Packages/qdap" "~/Packages/qdap" "~/Packages/qdap"
## [4] "~/Packages/qdap" "~/Packages/qdap" "~/Packages/qdap"
## [7] "~/Packages/qdap" "~/Packages/qdap" "~/Packages/qdap"
## [10] "~/Packages/qdap"
myfiles %>%
parse_path() %>%
back(3)
## [1] "qdap/R/cm_range2long.R"
## [2] "Packages/qdap/contributors.geojson"
## [3] "qdap/R/mcsv_r.R"
## [4] "Packages/qdap/DESCRIPTION"
## [5] "Packages/qdap/README.md"
## [6] "qdap/R/wfm.R"
## [7] "qdap/R/adjacency_matrix.R"
## [8] "qdap/R/xnoy.R"
## [9] "qdap/R/termco.R"
## [10] "qdap/R/bag_o_words.R"
Indices
The index function compliments the front/back set by allowing the
user to select the middle elements of a path. Unlike front/back, the
index function does not require elements to be sequential. The user
will get a sub-path equal in length to the length of the inds
argument.
myfiles %>%
parse_path() %>%
index(4)
## [1] "R" "contributors.geojson" "R"
## [4] "DESCRIPTION" "README.md" "R"
## [7] "R" "R" "R"
## [10] "R"
myfiles %>%
parse_path() %>%
index(2:4)
## [1] "Packages/qdap/R"
## [2] "Packages/qdap/contributors.geojson"
## [3] "Packages/qdap/R"
## [4] "Packages/qdap/DESCRIPTION"
## [5] "Packages/qdap/README.md"
## [6] "Packages/qdap/R"
## [7] "Packages/qdap/R"
## [8] "Packages/qdap/R"
## [9] "Packages/qdap/R"
## [10] "Packages/qdap/R"
myfiles %>%
parse_path() %>%
index(c(2, 4))
## [1] "Packages/R" "Packages/contributors.geojson"
## [3] "Packages/R" "Packages/DESCRIPTION"
## [5] "Packages/README.md" "Packages/R"
## [7] "Packages/R" "Packages/R"
## [9] "Packages/R" "Packages/R"
Before & After Regex
The before/after differ from the previous sets of manipulation
functions in that it allows the user to select elements based on their
content rather than numeric position. The user provides a regular
expression to match against. All elements before or after this regex
match will be selected for use in the sub-path. The user may include the
regex matched element by setting include = TRUE.
Here I extract all elements after the element containing the regex
"^qdap$".
myfiles %>%
parse_path() %>%
after("^qdap$")
## [1] "R/cm_range2long.R" "contributors.geojson" "R/mcsv_r.R"
## [4] "DESCRIPTION" "README.md" "R/wfm.R"
## [7] "R/adjacency_matrix.R" "R/xnoy.R" "R/termco.R"
## [10] "R/bag_o_words.R"
The user can include the element that matched the regex as well using
include = TRUE:
myfiles %>%
parse_path() %>%
after("^qdap$", include = TRUE)
## [1] "qdap/R/cm_range2long.R" "qdap/contributors.geojson"
## [3] "qdap/R/mcsv_r.R" "qdap/DESCRIPTION"
## [5] "qdap/README.md" "qdap/R/wfm.R"
## [7] "qdap/R/adjacency_matrix.R" "qdap/R/xnoy.R"
## [9] "qdap/R/termco.R" "qdap/R/bag_o_words.R"
Here I use before to extract all elements before the regex match to
paths that contain an element with a file that ends in .R. If a path
does not contain that element match NA is returned.