Skip to content

Commit 03c2537

Browse files
committed
New function
1 parent b668dc2 commit 03c2537

8 files changed

Lines changed: 79 additions & 5 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatstat.utils
2-
Version: 3.2-3
3-
Date: 2026-05-10
2+
Version: 3.2-3.002
3+
Date: 2026-07-15
44
Title: Utility Functions for 'spatstat'
55
Authors@R: c(person("Adrian", "Baddeley",
66
role = c("aut", "cre"),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export("can.be.formula")
3333
export("capitalise")
3434
export("cat.factor")
3535
export("check.1.integer")
36+
export("check.1.logical")
3637
export("check.1.real")
3738
export("check.1.string")
3839
export("check.anyvector")
@@ -63,6 +64,7 @@ export("dropifsingle")
6364
export("dround")
6465
export("ensure2vector")
6566
export("ensure3Darray")
67+
export("ensure.nlist")
6668
export("equispaced")
6769
export("eratosthenes")
6870
export("evenly.spaced")

NEWS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11

2+
CHANGES IN spatstat.utils VERSION 3.2-3.002
3+
4+
OVERVIEW
5+
6+
o Check logical arguments.
7+
8+
o Internal improvements.
9+
10+
NEW FUNCTIONS
11+
12+
o check.1.logical
13+
Check whether an argument is a single logical value.
14+
215
CHANGES IN spatstat.utils VERSION 3.2-3
316

417
OVERVIEW

R/utilarg.R

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#'
44
#' Utilities for checking/handling arguments
55
#'
6-
#' $Revision: 1.18 $ $Date: 2025/06/28 02:29:54 $
6+
#' $Revision: 1.19 $ $Date: 2026/07/15 03:11:51 $
77
#'
88

99
"%orifnull%" <- function(a, b) {
@@ -281,6 +281,18 @@ check.1.string <- function(x, context="", fatal=TRUE, warn=TRUE) {
281281
return(FALSE)
282282
}
283283

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+
284296
complaining <- function(whinge, fatal=FALSE, value=NULL) {
285297
if(fatal) stop(whinge, call.=FALSE)
286298
warning(whinge, call.=FALSE)
@@ -364,6 +376,46 @@ there.can.be.only.one <- function(..., .NeedOne=TRUE, .Fatal=TRUE) {
364376
return(TRUE)
365377
}
366378

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+
367419
## replace recognised keywords by other keywords
368420
mapstrings <- function(x, map=NULL) {
369421
if(is.null(map)) return(x)

inst/doc/packagesizes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
3434
"2026-01-08" "3.2-1" 43 196 0 3754 2535
3535
"2026-03-10" "3.2-2" 44 197 0 3778 2535
3636
"2026-05-10" "3.2-3" 44 197 0 3817 2683
37+
"2026-07-15" "3.2-3.002" 44 199 0 3869 2683

inst/info/packagesizes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
3434
"2026-01-08" "3.2-1" 43 196 0 3754 2535
3535
"2026-03-10" "3.2-2" 44 197 0 3778 2535
3636
"2026-05-10" "3.2-3" 44 197 0 3817 2683
37+
"2026-07-15" "3.2-3.002" 44 199 0 3869 2683

man/check.1.integer.Rd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
\alias{check.1.integer}
33
\alias{check.1.real}
44
\alias{check.1.string}
5+
\alias{check.1.logical}
56
\title{
67
Check Argument Type and Length
78
}
@@ -13,6 +14,7 @@
1314
check.1.real(x, context = "", fatal = TRUE, warn=TRUE)
1415
check.1.integer(x, context = "", fatal = TRUE, warn=TRUE)
1516
check.1.string(x, context = "", fatal = TRUE, warn=TRUE)
17+
check.1.logical(x, context = "", fatal = TRUE, warn=TRUE)
1618
}
1719
\arguments{
1820
\item{x}{
@@ -33,8 +35,8 @@ check.1.string(x, context = "", fatal = TRUE, warn=TRUE)
3335
}
3436
\details{
3537
These functions check whether the argument \code{x} is a single
36-
atomic value of type \code{numeric}, \code{integer} or
37-
\code{character}.
38+
atomic value of type \code{numeric}, \code{integer},
39+
\code{character} or \code{logical}.
3840

3941
If \code{x} does have the required length and type, the result
4042
of the function is the logical value \code{TRUE}.
@@ -58,6 +60,7 @@ check.1.string(x, context = "", fatal = TRUE, warn=TRUE)
5860
check.1.integer(pi, fatal=FALSE, context="In your dreams,")
5961
check.1.string(x, fatal=FALSE)
6062
check.1.integer(x, fatal=FALSE, warn=FALSE)
63+
check.1.logical(pi, fatal=FALSE, warn=FALSE)
6164
}
6265
\keyword{classes}
6366
\keyword{error}

man/spatstat.utils-internal.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
\alias{dround}
2525
\alias{ensure2vector}
2626
\alias{ensure3Darray}
27+
\alias{ensure.nlist}
2728
\alias{eratosthenes}
2829
\alias{exceedsMaxArraySize}
2930
\alias{exhibitStringList}
@@ -136,6 +137,7 @@ dropifsingle(x)
136137
dround(x)
137138
ensure2vector(x)
138139
ensure3Darray(x)
140+
ensure.nlist(x, n, singletypes, xtitle, things)
139141
eratosthenes(nmax, startset)
140142
exhibitStringList(prefix, strings)
141143
exceedsMaxArraySize(\dots)

0 commit comments

Comments
 (0)