swap: Replace Elements of a Path

Description Usage Arguments Value See Also Examples

Description

Replace the elements of a path. Note either a index (use inds) or regex (use pattern) based approach is used to identify the indices to replace. The more specific swap_index or swap_regex can be used to be more targetted, transparent, pipeable, and efficient.

Usage

1
2
3
4
5
swap(x, inds, replacement, pattern = NULL, collapse = TRUE, ...)

swap_index(x, inds, replacement, collapse = TRUE, ...)

swap_regex(x, pattern, replacement, collapse = TRUE, ...)

Arguments

x

A parse_path object.

inds

Indices to replace.

replacement

A value to replace the indentified indices with.

pattern

A character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. Note that under the hood grep is used to locate the indices of path elements that match the regex condition. Only the first match will be used. The argument perl = TRUE is hard coded.

collapse

logical. If TRUE the path elements will be collapsed with the "/" separator.

...

ignored.

Value

Returns a vector (either atomic or list of character path elements) of swapped paths.

See Also

Other sub-path manipulation functions: before, front, index

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
swap(list(1:8), inds = c(2, 4), "REPLACED VALUE")
swap(list(1:8), pattern = "[238]", replacement = "REPLACED VALUE")

library(magrittr)
files %>%
    parse_path() %>%
    swap(1, "MyFile")

files %>%
    parse_path() %>%
    swap_index(3, "textMining")

files %>%
    parse_path() %>%
    swap_regex("^qdap$", "textMining")

files %>%
    parse_path() %>%
    swap_regex("\\.R$", "function.R")

files %>%
    parse_path() %>%
    swap_regex("^(P|R)", "oops")

trinker/pathr documentation built on May 31, 2019, 9:41 p.m.