generated from r-hyperspec/pkg-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from r-hyperspec/feature/move-remaining
Update count_lines.R, read_asc_Andor, & Perkin from `hyperSpec`
- Loading branch information
Showing
5 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Count Lines (of an ASCII File) | ||
#' | ||
#' @param file the file name or connection | ||
#' @param chunksize `file` is read in chunks of `chunksize` lines. | ||
#' @return number of lines in file | ||
#' @export | ||
#' | ||
#' @concept io | ||
#' | ||
#' @author C. Beleites | ||
count_lines <- function(file, chunksize = 1e4) { | ||
nlines <- 0 | ||
|
||
con <- file(file, open = "r") | ||
on.exit(close(con)) | ||
|
||
while ((n <- length(readLines(con, n = chunksize))) > 0L) { | ||
nlines <- nlines + n | ||
} | ||
|
||
nlines | ||
} | ||
|
||
hySpc.testthat::test(count_lines) <- function() { | ||
context("count_lines") | ||
|
||
tmpfile <- tempfile() | ||
on.exit(unlink(tmpfile)) | ||
|
||
writeLines("blabla\nblubb", con = tmpfile) | ||
|
||
test_that( | ||
"file read in one chunk", | ||
expect_equal(count_lines(tmpfile), 2) | ||
) | ||
|
||
test_that( | ||
"file read in more chunks", | ||
expect_equal(count_lines(tmpfile, chunksize = 1L), 2) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Import Raman Spectra/Maps from Andor Cameras/Solis ASCII files. | ||
#' | ||
#' `read_asc_Andor()` reads Andor Solis ASCII (`.asc`) files where the first column gives the wavelength | ||
#' axes and the other columns the spectra. | ||
#' | ||
#' @param file filename or connection to ASCII file | ||
#' @param ...,quiet,dec,sep handed to [base::scan()] | ||
#' @return a hyperSpec object | ||
#' @author Claudia Beleites | ||
#' @seealso `vignette ("fileio")` for more information on file import and | ||
#' | ||
#' @concept io | ||
#' | ||
#' @export | ||
read_asc_Andor <- function(file = stop("filename or connection needed"), | ||
..., quiet = TRUE, dec = ".", sep = ",") { | ||
|
||
## check for valid data connection | ||
check_con(file = file) | ||
|
||
## read spectra | ||
tmp <- readLines(file) | ||
nwl <- length(tmp) | ||
txt <- scan(text = tmp, dec = dec, sep = sep, quiet = quiet, ...) | ||
|
||
dim(txt) <- c(length(txt) / nwl, nwl) | ||
|
||
## fix: Andor Solis may have final comma without values | ||
if (all(is.na(txt [nrow(txt), ]))) { | ||
txt <- txt [-nrow(txt), ] | ||
} | ||
|
||
spc <- new("hyperSpec", wavelength = txt [1, ], spc = txt [-1, ]) | ||
|
||
## consistent file import behaviour across import functions | ||
.fileio.optional(spc, file) | ||
} | ||
|
||
hySpc.testthat::test(read_asc_Andor) <- function() { | ||
skip("Need to adapt to new package") | ||
context("read_asc_Andor") | ||
test_that("Andor Solis .asc text files", { | ||
skip_if_not_fileio_available() | ||
expect_known_hash(read_asc_Andor("fileio/asc.Andor/ASCII-Andor-Solis.asc"), "9ead937f51") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#' File import filter PerkinElmer ASCII spectra. | ||
#' | ||
#' Imports a single spectrum in PerkinElmer's ASCII format. This function is experimental. | ||
#' | ||
#' @param file filename (or connection) | ||
#' @param ... further parameters are handed to [hySpc.read.txt::read_txt_long()] | ||
#' | ||
#' @return hyperSpec object | ||
#' @importFrom utils packageDescription | ||
#' @export | ||
#' | ||
#' @concept io | ||
#' | ||
read_asc_PerkinElmer <- function(file = stop("filename or connection needed"), ...) { | ||
content <- readLines(con = file) | ||
|
||
message( | ||
"read_asc_PerkinElmer is experimental, hyperSpec so far has no test data for PE .asc files.", | ||
" Please consider submitting your spectrum in an enhancement request to ", packageDescription("hyperSpec")$BugReports, | ||
" in order to help the development of hyperSpec." | ||
) | ||
|
||
## find beginning of DATA section | ||
startDATA <- grep("DATA", content) | ||
|
||
if (length(startDATA) != 1L) { | ||
stop( | ||
"read_asc_PerkinElmer so far can deal with single spectra files only.", | ||
" Please file an enhancement request at", packageDescription("hyperSpec")$BugReports, | ||
" with your file as an example or contact the maintainer (", | ||
maintainer("hyperSpec"), ")." | ||
) | ||
} | ||
|
||
## Spectra values are stored | ||
content <- content[-seq_len(startDATA)] | ||
|
||
spc <- read_txt_long(textConnection(content), header = FALSE, sep = "\t", ...) | ||
spc$filename <- NULL # not meaningful due to textConnection use | ||
|
||
## consistent file import behaviour across import functions | ||
.fileio.optional(spc, file) | ||
} |