diff --git a/DESCRIPTION b/DESCRIPTION index 14e0027..0361812 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: MsCoreUtils Title: Core Utils for Mass Spectrometry Data -Version: 1.17.0 +Version: 1.17.1 Description: MsCoreUtils defines low-level functions for mass spectrometry data and is independent of any high-level data structures. These functions include mass spectra processing @@ -80,4 +80,4 @@ BugReports: https://github.com/RforMassSpectrometry/MsCoreUtils/issues URL: https://github.com/RforMassSpectrometry/MsCoreUtils biocViews: Infrastructure, Proteomics, MassSpectrometry, Metabolomics Roxygen: list(markdown=TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/NEWS.md b/NEWS.md index 857719e..644396e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# MsCoreUtils 1.17 + +## MsCoreUtils 1.17.1 + +- Fix `common_path()` to not return also the file name if the input parameter + contains only identical character strings. + # MsCoreUtils 1.15 ## MsCoreUtils 1.15.7 diff --git a/R/file_utils.R b/R/file_utils.R index 7dc27b0..bde0cc8 100644 --- a/R/file_utils.R +++ b/R/file_utils.R @@ -2,9 +2,12 @@ #' #' @description #' -#' Find the common part of the path for a provided set of files. +#' Find the common part of the path up to a provided set of files. Be aware that +#' the last element (after the last file separator) is treated as a *file*. +#' Thus, if only directories, without files are submitted, the common path +#' containing these directories is returned. #' -#' @param x `character` with the file names (including paths). +#' @param x `character` with the **file names** (including paths). #' #' @param fsep `character(1)` defining the file separator to be used in #' the returned common path. Defaults to the system platform's file @@ -42,16 +45,21 @@ #' #' ## No path #' common_path(c("a.txt", "b.txt")) +#' +#' ## Same path for all +#' common_path(c("a/a.txt", "a/a.txt")) common_path <- function(x, fsep = .Platform$file.sep) { if (!length(x)) return(character()) sx <- strsplit(x, split = "(\\\\)|/") - minl <- min(lengths(sx)) - cpath <- character() - for (i in seq_len(minl)) { - uvals <- unique(vapply(sx, `[`, character(1), i = i)) - if (length(uvals) == 1L) - cpath <- c(cpath, uvals) - } - paste0(cpath, collapse = fsep) + minl <- min(lengths(sx)) - 1L + if (minl > 0) { + cpath <- character() + for (i in seq_len(minl)) { + uvals <- unique(vapply(sx, `[`, character(1), i = i)) + if (length(uvals) == 1L) + cpath <- c(cpath, uvals) + } + paste0(cpath, collapse = fsep) + } else "" } diff --git a/man/common_path.Rd b/man/common_path.Rd index fc4899f..0ce942e 100644 --- a/man/common_path.Rd +++ b/man/common_path.Rd @@ -7,7 +7,7 @@ common_path(x, fsep = .Platform$file.sep) } \arguments{ -\item{x}{\code{character} with the file names (including paths).} +\item{x}{\code{character} with the \strong{file names} (including paths).} \item{fsep}{\code{character(1)} defining the file separator to be used in the returned common path. Defaults to the system platform's file @@ -17,7 +17,10 @@ separator.} \code{character(1)} representing the path common to all files in \code{x}. } \description{ -Find the common part of the path for a provided set of files. +Find the common part of the path up to a provided set of files. Be aware that +the last element (after the last file separator) is treated as a \emph{file}. +Thus, if only directories, without files are submitted, the common path +containing these directories is returned. } \note{ This function uses \code{"(\\\\\\\\)|/"} to split the provided paths into the @@ -44,6 +47,9 @@ common_path(character()) ## No path common_path(c("a.txt", "b.txt")) + +## Same path for all +common_path(c("a/a.txt", "a/a.txt")) } \author{ Johannes Rainer diff --git a/tests/testthat/test_file_utils.R b/tests/testthat/test_file_utils.R index 609ad2a..5e43a55 100644 --- a/tests/testthat/test_file_utils.R +++ b/tests/testthat/test_file_utils.R @@ -11,4 +11,8 @@ test_that("common_path works", { expect_equal(common_path(x), "C:/first/second") x <- c("C:\\first\\second\\a.txt", "C:\\first\\second\\third\\b.txt") expect_equal(common_path(x), "C:/first/second") + + ## Check if we have a single input. + expect_equal(common_path("a/b.txt"), "a") + expect_equal(common_path("a/b.txt", "a/b.txt"), "a") })