Skip to content

Commit 2da55fa

Browse files
committed
Accept T/F for .progress argument of nlapply
* and complain otherwise
1 parent e953e68 commit 2da55fa

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

R/neuronlist.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
384384
#' Progress bar section for details) The default value of \code{"auto"} shows
385385
#' a progress bar in interactive use after 2s. The default value can be
386386
#' overridden for the current session by setting the value of
387-
#' \code{options(nat.progressbar)} (see examples).
388-
#'
387+
#' \code{options(nat.progressbar)} (see examples). Values of \code{T} and
388+
#' \code{F} are aliases for 'text' and 'none', respectively.
389+
#'
389390
#' @section Progress bar: There are currently two supported approaches to
390391
#' defining progress bars for \code{nlapply}. The default (when
391392
#' \code{progress="auto"}) now uses a progress bar built using
@@ -472,11 +473,16 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
472473
#' }
473474
nlapply<-function (X, FUN, ..., subset=NULL, OmitFailures=NA,
474475
.progress=getOption('nat.progress', default='auto')){
475-
if(.progress=='auto') {
476+
if(isTRUE(.progress=='auto')) {
476477
.progress = ifelse(interactive(), "natprogress", "none")
477-
} else if(.progress=='traditional') {
478+
} else if(isTRUE(.progress=='traditional')) {
478479
.progress = ifelse(length(X)>=10 && interactive(), "text", "none")
479-
}
480+
} else if(isTRUE(.progress)) {
481+
.progress <- 'text'
482+
} else if(isFALSE(.progress)) {
483+
.progress <- 'none'
484+
}
485+
checkmate::assert_character(.progress, any.missing = F, len = 1L)
480486
cl=if(is.neuronlist(X) && !inherits(X, c('neuronlistfh','neuronlistz')))
481487
class(X)
482488
else c("neuronlist", 'list')
@@ -547,7 +553,14 @@ nmapply<-function(FUN, X, ..., MoreArgs = NULL, SIMPLIFY = FALSE,
547553
}
548554
FUN2=FUN
549555

550-
if(.progress=='auto') .progress=ifelse(interactive(), 'text', 'none')
556+
if(isTRUE(.progress=='auto')) {
557+
.progress = ifelse(interactive(), "text", "none")
558+
} else if(isTRUE(.progress)) {
559+
.progress <- 'text'
560+
} else if(isFALSE(.progress)) {
561+
.progress <- 'none'
562+
}
563+
checkmate::assert_character(.progress, any.missing = F, len = 1L)
551564

552565
if(.progress!='none'){
553566
p <- progress_natprogress()

tests/testthat/test-neuronlist.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,16 @@ test_that("nlapply can omit failures",{
108108
# this time with subset and omit failures
109109
expect_equal(length(dotprops(kcs3, k=5, subset=1:2, OmitFailures=TRUE)), 3)
110110
expect_equal(length(dotprops(kcs3, k=5, subset=c(1,3), OmitFailures=TRUE)), 2)
111+
112+
expect_output(dotprops(kcs3, k=5, OmitFailures=TRUE, .progress=T))
113+
expect_silent(dotprops(kcs3, k=5, OmitFailures=TRUE, .progress=F))
114+
expect_error(dotprops(kcs3, k=5, OmitFailures=TRUE, .progress=NA))
111115
})
112116

113117
test_that("nmapply with identity function returns its arguments",{
114118
kcs3=kcs20[1:3]
115119
expect_equal(nmapply(function(x) x, kcs3), kcs3)
120+
expect_silent(nmapply(function(x) x, kcs3, .progress=F))
116121
})
117122

118123
test_that("nmapply can vectorise more than one argument",{

0 commit comments

Comments
 (0)