get_path_segment {mintyr}R Documentation

Extract Specific Segments from File Paths

Description

The get_path_segment function extracts specific segments from file paths provided as character strings. Segments can be extracted from either the beginning or the end of the path, depending on the value of n.

Usage

get_path_segment(paths, n = 1)

Arguments

paths

A 'character vector' containing file system paths

  • Must be non-empty

  • Path segments separated by forward slash '/'

  • Supports absolute and relative paths

  • Handles cross-platform path representations

  • Supports paths with mixed separators ('\\' and '/')

n

Numeric index for segment selection

  • Positive values: Select from path start

  • Negative values: Select from path end

  • Supports single index or range extraction

  • Cannot be 0

  • Default is 1 (first segment)

Details

Sophisticated Path Segment Extraction Mechanism:

  1. Comprehensive input validation

  2. Path normalization and preprocessing

  3. Robust cross-platform path segmentation

  4. Flexible indexing with forward and backward navigation

  5. Intelligent segment retrieval

  6. Graceful handling of edge cases

Indexing Behavior:

Path Parsing Characteristics:

Value

'character vector' with extracted path segments

Note

Critical Operational Constraints:

See Also

Examples

# Example: Path segment extraction demonstrations

# Setup test paths
paths <- c(
  "C:/home/user/documents",   # Windows style path
  "/var/log/system",          # Unix system path
  "/usr/local/bin"            # Unix binary path
)

# Example 1: Extract first segment
get_path_segment(
  paths,                      # Input paths
  1                           # Get first segment
)
# Returns: c("home", "var", "usr")

# Example 2: Extract second-to-last segment
get_path_segment(
  paths,                      # Input paths
  -2                          # Get second-to-last segment
)
# Returns: c("user", "log", "local")

# Example 3: Extract from first to last segment
get_path_segment(
  paths,                      # Input paths
  c(1,-1)                     # Range from first to last
)
# Returns full paths without drive letters

# Example 4: Extract first three segments
get_path_segment(
  paths,                      # Input paths
  c(1,3)                      # Range from first to third
)
# Returns: c("home/user/documents", "var/log/system", "usr/local/bin")

# Example 5: Extract last two segments (reverse order)
get_path_segment(
  paths,                      # Input paths
  c(-1,-2)                    # Range from last to second-to-last
)
# Returns: c("documents/user", "system/log", "bin/local")

# Example 6: Extract first two segments
get_path_segment(
  paths,                      # Input paths
  c(1,2)                      # Range from first to second
)
# Returns: c("home/user", "var/log", "usr/local")

[Package mintyr version 0.1.0 Index]