[R-gregmisc-commits] r2174 - in pkg/gtools: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 19 18:43:56 CEST 2018


Author: warnes
Date: 2018-06-19 18:43:55 +0200 (Tue, 19 Jun 2018)
New Revision: 2174

Added:
   pkg/gtools/R/split_path.R
   pkg/gtools/man/split_path.Rd
Log:
Add spit_path() function.

Added: pkg/gtools/R/split_path.R
===================================================================
--- pkg/gtools/R/split_path.R	                        (rev 0)
+++ pkg/gtools/R/split_path.R	2018-06-19 16:43:55 UTC (rev 2174)
@@ -0,0 +1,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 character scalar.  Path to be processed.
+#' @param depth_firs  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))))
+}

Added: pkg/gtools/man/split_path.Rd
===================================================================
--- pkg/gtools/man/split_path.Rd	                        (rev 0)
+++ pkg/gtools/man/split_path.Rd	2018-06-19 16:43:55 UTC (rev 2174)
@@ -0,0 +1,22 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/split_path.R
+\name{split_path}
+\alias{split_path}
+\title{Split a File Path into Components}
+\usage{
+split_path(x, depth_first = TRUE)
+}
+\arguments{
+\item{character}{scalar.  Path to be processed.}
+
+\item{depth_firs}{logical.  Should path be returned depth first?  Defaults
+to \code{TRUE}.}
+}
+\value{
+Character vector of path components, depth first.
+}
+\description{
+This function converts a character scalar containing a
+ \emph{valid} file path into a character vector of path components
+ (e.g. directories).
+}



More information about the R-gregmisc-commits mailing list