|
3 | 3 | #' |
4 | 4 | #' Utilities for checking/handling arguments |
5 | 5 | #' |
6 | | -#' $Revision: 1.18 $ $Date: 2025/06/28 02:29:54 $ |
| 6 | +#' $Revision: 1.19 $ $Date: 2026/07/15 03:11:51 $ |
7 | 7 | #' |
8 | 8 |
|
9 | 9 | "%orifnull%" <- function(a, b) { |
@@ -281,6 +281,18 @@ check.1.string <- function(x, context="", fatal=TRUE, warn=TRUE) { |
281 | 281 | return(FALSE) |
282 | 282 | } |
283 | 283 |
|
| 284 | +check.1.logical <- function(x, context="", fatal=TRUE, warn=TRUE) { |
| 285 | + if(is.logical(x) && length(x) == 1) |
| 286 | + return(TRUE) |
| 287 | + if(fatal || warn) { |
| 288 | + xname <- short.deparse(substitute(x)) |
| 289 | + whinge <- paste(sQuote(xname), "should be a single logical value") |
| 290 | + if(nzchar(context)) whinge <- paste(context, whinge) |
| 291 | + if(fatal) stop(whinge, call.=FALSE) else warning(whinge, call.=FALSE) |
| 292 | + } |
| 293 | + return(FALSE) |
| 294 | +} |
| 295 | + |
284 | 296 | complaining <- function(whinge, fatal=FALSE, value=NULL) { |
285 | 297 | if(fatal) stop(whinge, call.=FALSE) |
286 | 298 | warning(whinge, call.=FALSE) |
@@ -364,6 +376,46 @@ there.can.be.only.one <- function(..., .NeedOne=TRUE, .Fatal=TRUE) { |
364 | 376 | return(TRUE) |
365 | 377 | } |
366 | 378 |
|
| 379 | +## Ensure that x is a list of length n, |
| 380 | +## which can be passed to 'mapply' |
| 381 | +## Input 'x' can be a single object of a recognised class, |
| 382 | +## which will be replicated to the required length. |
| 383 | + |
| 384 | +ensure.nlist <- function(x, n, singletypes=character(0), |
| 385 | + xtitle=NULL, things="point patterns") { |
| 386 | + if(length(singletypes) && inherits(x, singletypes)) { |
| 387 | + ## single object of recognised class: replicate it |
| 388 | + x <- rep(list(x), n) |
| 389 | + return(x) |
| 390 | + } |
| 391 | + if(!is.list(x)) { |
| 392 | + ## error |
| 393 | + if(is.null(xtitle)) xtitle <- short.deparse(substitute(x)) |
| 394 | + whinge <- paste(xtitle, "should be a list") |
| 395 | + if(length(singletypes)) { |
| 396 | + otypes <- setdiff(singletypes, "NULL") |
| 397 | + if(length(otypes)) |
| 398 | + whinge <- paste(whinge, |
| 399 | + "or an object of class", |
| 400 | + commasep(dQuote(otypes), "or")) |
| 401 | + if("NULL" %in% singletypes) |
| 402 | + whinge <- paste(whinge, "or NULL") |
| 403 | + } |
| 404 | + stop(whinge, call.=FALSE) |
| 405 | + } |
| 406 | + nx <- length(x) |
| 407 | + if(nx != n) { |
| 408 | + if(is.null(xtitle)) xtitle <- short.deparse(substitute(x)) |
| 409 | + whinge <- paste("The length of", |
| 410 | + sQuote(xtitle), |
| 411 | + "should equal the number of", |
| 412 | + things, |
| 413 | + paren(paste(nx, "!=", n))) |
| 414 | + stop(whinge, call.=FALSE) |
| 415 | + } |
| 416 | + return(x) |
| 417 | +} |
| 418 | + |
367 | 419 | ## replace recognised keywords by other keywords |
368 | 420 | mapstrings <- function(x, map=NULL) { |
369 | 421 | if(is.null(map)) return(x) |
|
0 commit comments