Skip to content

Commit

Permalink
add option for c_spec in read_any
Browse files Browse the repository at this point in the history
  • Loading branch information
wincowgerDEV committed Dec 7, 2024
1 parent f84cda5 commit c872396
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
15 changes: 12 additions & 3 deletions R/read_multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
#' in a character vector and will return a list.
#'
#' @param file file to be read from or written to.
#' @param c_spec logical, if multiple spectra should be concatenated or not.
#' Multiple spectra will return a list if this is false.
#' @param c_spec_args list of arguments passed to \code{c_spec()}
#' @param \ldots further arguments passed to the submethods.
#'
#' @return
#' All \code{read_*()} functions return \code{OpenSpecy} objects if a single
#' spectrum or map is provided, otherwise the provide a list of \code{OpenSpecy} objects.
#' spectrum or map is provided, otherwise they provide a list of \code{OpenSpecy} objects.
#'
#' @examples
#' \dontshow{data.table::setDTthreads(2)}
Expand All @@ -39,18 +42,25 @@
#' @importFrom data.table transpose
#'
#' @export
read_any <- function(file, ...) {
read_any <- function(file, c_spec = T,
c_spec_args = list(range = NULL, res = NULL), ...) {
if(length(file) == 2 & any(grepl("(\\.dat$)|(\\.img$)", ignore.case = T, file)) & any(grepl("(\\.hdr$)", ignore.case = T, file))){
os <- read_envi(file = file[grepl("(\\.dat$)|(\\.img$)", ignore.case = T, file)], header = file[grepl("(\\.hdr$)", ignore.case = T, file)], ...)
}
else if(length(file) > 1){
os <- read_many(file = file, ...)
if(c_spec & !is_OpenSpecy(os) & is.list(os)){
os <- do.call("c_spec", c(list(os), c_spec_args))
}
}
else if(any(grepl("(\\.dat$)|(\\.img$)", ignore.case = T, file))){
os <- read_envi(file = file[grepl("(\\.dat$)|(\\.img$)", ignore.case = T, file)], ...)
}
else if (grepl("(\\.zip$)", ignore.case = T, file)) {
os <- read_zip(file = file, ...)
if(c_spec & !is_OpenSpecy(os) & is.list(os)){
os <- do.call("c_spec", c(list(os), c_spec_args))
}
}
else if (grepl("(\\.xyz$)|(\\.csv$)|(\\.tsv$)|(\\.txt$)", ignore.case = T, file)) {
os <- read_text(file = file, ...)
Expand All @@ -67,7 +77,6 @@ read_any <- function(file, ...) {
} else {
os <- read_spec(file = file, ...)
}

return(os)
}

Expand Down
9 changes: 7 additions & 2 deletions man/read_multi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion tests/testthat/test-read_multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ dir.create(tmp, showWarnings = F)
data("raman_hdpe")

test_that("reading in multi files doesn't throw error", {
expect_silent(multi <- read_extdata("testdata_zipped.zip") |> read_any() |> c_spec())

#Check new configuration
expect_silent(multi <- read_extdata("testdata_zipped.zip") |> read_any(c_spec = F))
expect_type(multi, "list")
expect_false(is_OpenSpecy(multi))
expect_true(all(vapply(multi, is_OpenSpecy, FUN.VALUE = logical(1))))

expect_message(multi <- read_extdata("testdata_zipped.zip") |> read_any() |> c_spec())
expect_silent(multi <- read_extdata("testdata_zipped.zip") |> read_any())
expect_s3_class(multi, "OpenSpecy")

expect_equal(multi$wavenumber, raman_hdpe$wavenumber)
Expand Down

0 comments on commit c872396

Please sign in to comment.