Skip to content

Commit

Permalink
Use arg. file
Browse files Browse the repository at this point in the history
  • Loading branch information
GegznaV committed Aug 10, 2021
1 parent bdcfa96 commit e5241ff
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions vignettes/read_txt_PerkinElmer.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#' Read PerkinElmer file (ASCII/txt)
#'
#' @param files Path to file.
#' @param file Path to file or several files
#' @param ... Passed to [base::scan()].
#' @param label Labels.
#'
Expand All @@ -13,7 +13,7 @@
#'
#' @export
#'
read_txt_PerkinElmer <- function(files = stop("filenames needed"),
read_txt_PerkinElmer <- function(file = stop("filenames needed"),
...,
label = list()) {

Expand All @@ -27,31 +27,31 @@ read_txt_PerkinElmer <- function(files = stop("filenames needed"),
label
)

if (length(files) == 0) {
if (length(file) == 0) {
warning("No files found.")
return(new("hyperSpec"))
}

## read the first file
buffer <- matrix(scan(files[1], ...), ncol = 2, byrow = TRUE)
buffer <- matrix(scan(file[1], ...), ncol = 2, byrow = TRUE)

## first column gives the wavelength vector
wavelength <- buffer[, 1]

## preallocate the spectra matrix:
## one row per file x as many columns as the first file has
spc <- matrix(ncol = nrow(buffer), nrow = length(files))
spc <- matrix(ncol = nrow(buffer), nrow = length(file))

## the first file's data goes into the first row
spc[1, ] <- buffer[, 2]

## now read the remaining files
for (f in seq(along = files)[-1]) {
buffer <- matrix(scan(files[f], ...), ncol = 2, byrow = TRUE)
for (f in seq(along = file)[-1]) {
buffer <- matrix(scan(file[f], ...), ncol = 2, byrow = TRUE)

## check whether they have the same wavelength axis
if (!all.equal(buffer[, 1], wavelength)) {
stop(paste(files[f], "has different wavelength axis."))
stop(paste(file[f], "has different wavelength axis."))
}

spc[f, ] <- buffer[, 2]
Expand All @@ -61,7 +61,7 @@ read_txt_PerkinElmer <- function(files = stop("filenames needed"),
spc <- new("hyperSpec", wavelength = wavelength, spc = spc, label = label)

## consistent file import behavior across import functions
.spc_io_postprocess_optional(spc, files)
.spc_io_postprocess_optional(spc, file)
}


Expand Down

0 comments on commit e5241ff

Please sign in to comment.