File: split_path.R

package info (click to toggle)
gtools 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 620 kB
  • sloc: ansic: 157; asm: 127; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 894 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#' Split a File Path into Components
#'
#' @description This function converts a character scalar containing a
#'  \emph{valid} file path into a character vector of path components
#'  (e.g. directories).
#'
#' @param x            character scalar.  Path to be processed.
#' @param depth_first  logical.  Should path be returned depth first?  Defaults
#'   to \code{TRUE}.
#'
#' @return Character vector of path components, depth first.
#'
#' @export
#'
split_path <- function(x, depth_first=TRUE)
{
  if(length(x)>1) warning("This function is not vectorized.",
                          "Only processing the first element of x.")
  retval <- split_path_inner(x)
  if(!depth_first)
    retval <- rev(retval)
  retval[retval>""]
}


split_path_inner <- function(path) {
  if (dirname(path) %in% c(".", path)) return(basename(path))
  return(c(basename(path), split_path_inner(dirname(path))))
}