Skip to content

Commit 8ff7f35

Browse files
committed
feat: add parameter drop to chromatograms()
1 parent 28e40f9 commit 8ff7f35

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

R/methods-mzRpwiz.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,25 @@ setMethod("chromatograms", "mzRpwiz",
158158

159159

160160
setMethod("chromatogram", "mzRpwiz",
161-
function(object, chrom) {
161+
function(object, chrom, drop = TRUE) {
162162
## To avoid confusion, the first chromatogram (at index
163163
## 0) is indexed at position 1 in R and the last one (at
164164
## index nChrom(object) - 1) is indexed at position
165165
## nChrom(object).
166166
n <- nChrom(object)
167167
all <- FALSE
168-
if (missing(chrom)) {
168+
if (missing(chrom))
169169
chrom <- 1:n
170-
all <- TRUE
171-
}
172170
stopifnot(is.numeric(chrom))
173171
chrom <- as.integer(chrom)
174172
if (min(chrom) < 1 | max(chrom) > n)
175173
stop("Index out of bound [", 1, ":", n, "].")
176174
## Update index to match original indices at the C-level
177175
chrom <- chrom - 1L
178-
if (length(chrom) == 1 & !all) {
179-
ans <- object@backend$getChromatogramsInfo(chrom)
180-
} else {
181-
ans <- lapply(chrom,
182-
function(x)
183-
object@backend$getChromatogramsInfo(x))
184-
}
185-
return(ans)
176+
if (length(chrom) == 1 && drop)
177+
object@backend$getChromatogramsInfo(chrom)
178+
else
179+
lapply(chrom, object@backend$getChromatogramsInfo)
186180
})
187181

188182
setMethod("chromatogramHeader", "mzRpwiz",

inst/unitTests/test_chromatograms.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ test_chromatograms1 <- function() {
1212
checkIdentical(nrow(chromatogram(x, 137L)), 567L)
1313
checkIdentical(nrow(chromatogram(x, 138L)), 567L)
1414
chr <- chromatogram(x, 138L)
15+
checkTrue(is.data.frame(chr))
1516
checkIdentical(colnames(chr), c("rtime", "intensity"))
17+
chrl <- chromatogram(x, 138L, drop = FALSE)
18+
checkTrue(is.list(chrl))
19+
checkEquals(chr, chrl[[1L]])
1620
}
1721

1822
test_chromatograms2 <- function() {

man/peaks.Rd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@
8484
for all chromatograms is returned.
8585
}
8686

87+
\item{drop}{
88+
For \code{chromatogram}, \code{chromatograms}: \code{logical(1)}
89+
whether the result should always be a \code{list} of
90+
\code{data.frame} (\code{drop = FALSE}), even if data from a single
91+
chromatogram is requested, or if, in such cases, a \code{data.frame}
92+
should be returned (\code{drop = TRUE}, the default).
93+
}
94+
8795
\item{...}{Other arguments. A \code{scan} parameter can be passed to
8896
\code{peaks}.} }
8997

0 commit comments

Comments
 (0)