From 59cda3f58c4433f81c43880e5546291e30245b99 Mon Sep 17 00:00:00 2001 From: Martin Wilson Date: Thu, 14 Sep 2023 17:10:12 +0100 Subject: [PATCH] added find_mrs_files function --- DESCRIPTION | 4 ++-- NEWS.md | 1 + R/utils.R | 27 +++++++++++++++++++++++++++ man/find_mrs_files.Rd | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 man/find_mrs_files.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 0bba1a6c..a919f752 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: spant Type: Package Title: MR Spectroscopy Analysis Tools -Version: 2.15.9000 -Date: 2023-08-03 +Version: 2.15.0 +Date: 2023-09-14 Authors@R: c( person("Martin", "Wilson", email = "martin@pipegrep.co.uk", role = c("cre", "aut"), comment = c(ORCID = "0000-0002-2089-3956")), diff --git a/NEWS.md b/NEWS.md index 51069f48..9722d9be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # spant 2.15.0 * An SNR estimate of zero in comb_coils is now a failure case. * Bug fix for GE pfile reader header revision 20. +* Added a function for finding valid MRS files : find_mrs_files. # spant 2.14.0 * Added vline argument to stackplot function. diff --git a/R/utils.R b/R/utils.R index cafdcd8b..ecef154a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,30 @@ +#' Find valid MRS data files recursively from a directory path. +#' @param dir a directory path. +#' @return a vector of valid MRS data files. +#' @examples +find_mrs_files <- function(dir) { + + # recursively find all files + all_files <- list.files(dir, recursive = TRUE, full.names = TRUE) + + # empty vector of found files + mrs_files <- c() + + # check each file, catching any errors + for (file in all_files) { + tryCatch( + { + dummy <- read_mrs(file) + mrs_files <- c(mrs_files, file) + }, + error = function(cond) { + } + ) + } + return(mrs_files) +} + + #' Apply a function over specified array axes. #' @param x an array. #' @param axes a vector of axes to apply fun over. diff --git a/man/find_mrs_files.Rd b/man/find_mrs_files.Rd new file mode 100644 index 00000000..c97ae72f --- /dev/null +++ b/man/find_mrs_files.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{find_mrs_files} +\alias{find_mrs_files} +\title{Find valid MRS data files recursively from a directory path.} +\usage{ +find_mrs_files(dir) +} +\arguments{ +\item{dir}{a directory path.} +} +\value{ +a vector of valid MRS data files. +} +\description{ +Find valid MRS data files recursively from a directory path. +}