Skip to contents

Returns the nth ancestor directory of a given path.

Usage

parent_dir(
  path = NULL,
  levels = 1L,
  check_dir = FALSE,
  extract_full = FALSE,
  ...
)

Arguments

path

Character. The file or directory path to start from. If check_dir = TRUE and path exists as a file or has a file extension, its parent directory is used as the starting point. If check_dir = FALSE, a file path is treated as a directory name.

levels

Integer. Number of parent levels to jump up. Must be a single, non-negative integer.

check_dir

Logical. If TRUE, checks path is a valid directory and uses its parent if path is a file. If FALSE, does not check existence and uses the input as-is.

extract_full

Logical. If TRUE and levels exceeds the number of path components minus one, returns the root of the path. If FALSE, an error is thrown when levels exceeds the available ancestors.

...

Additional arguments passed to stop_ctx for error reporting.

Value

Character. The resulting path after jumping. If extract_full = TRUE and levels exceeds the available ancestors, returns the root of the path; otherwise, returns the ancestor path or throws an error.

Author

Ahmed El-Gabbas

Examples

parent_dir("/home/user/projects/data/file.txt", levels = 2)
#> /home/user/projects
# this will give an error
try(parent_dir("/home/user", levels = 4, extract_full = TRUE))
#> Error in parent_dir("/home/user", levels = 4, extract_full = TRUE) : 
#>   `levels` > available ancestors (max = 2). - 19:40:36
#> 
#> ----- Metadata -----
#> 
#> levels [levels]: <integer>
#> 4
#> 
#> abs_depth [abs_depth]: <integer>
#> 3
#> 
#> extract_full [extract_full]: <logical>
#> TRUE