13  anylist

Function spatstat.geom::anylist() creates a list of objects of any type, i.e., an R object of S3 class 'anylist', which inherits from the class 'listof' defined in package stats shipped with R version 4.5.2 (2025-10-31).

Listing 13.1, Listing 13.2 summarize the S3 methods for the class 'anylist' in the spatstat.* family of packages,

Listing 13.1: Existing S3 methods spatstat.geom::*.anylist
Code
suppressPackageStartupMessages(library(spatstat.geom))
.S3methods(class = 'anylist', all.names = TRUE) |> 
  attr(which = 'info', exact = TRUE) |>
  subset.data.frame(subset = from == 'spatstat.geom')
#                       visible          from       generic  isS4
# [.anylist                TRUE spatstat.geom             [ FALSE
# [<-.anylist              TRUE spatstat.geom           [<- FALSE
# as.hyperframe.anylist    TRUE spatstat.geom as.hyperframe FALSE
# plot.anylist             TRUE spatstat.geom          plot FALSE
# print.anylist            TRUE spatstat.geom         print FALSE
# summary.anylist          TRUE spatstat.geom       summary FALSE
Listing 13.2: Existing S3 methods spatstat.explore::*.anylist
Code
suppressPackageStartupMessages(library(spatstat.explore))
.S3methods(class = 'anylist', all.names = TRUE) |> 
  attr(which = 'info', exact = TRUE) |>
  subset.data.frame(subset = from == 'spatstat.explore')
#                  visible             from  generic  isS4
# collapse.anylist    TRUE spatstat.explore collapse FALSE
# pool.anylist        TRUE spatstat.explore     pool FALSE

The examples in Chapter 13 require that the search path contains the following namespaces,

library(groupedHyperframe)

Package groupedHyperframe (v0.3.2) implements more S3 methods to the class 'anylist' (Table 13.1),

Table 13.1: S3 methods groupedHyperframe::*.anylist (v0.3.2)
visible generic isS4
kerndens.anylist TRUE groupedHyperframe::kerndens FALSE
quantile.anylist TRUE stats::quantile FALSE

13.1 Kernel Density Estimates of numeric-vectors

The S3 method kerndens.anylist() (Listing 13.3) finds the kernel densitys of each numeric-vector member of an anylist.

Listing 13.3: Example: function kerndens.anylist()
Code
set.seed(11); spatstat.geom::anylist(
  rnorm(1e2L),
  rnorm(1e3L)
) |> 
  kerndens(n = 8L)
# [[1]]
# [1] 0.0001485149 0.0279242695 0.2154613160 0.3923521927 0.3012886913 0.1122736307 0.0254082139 0.0002006779
# 
# [[2]]
# [1] 2.058857e-05 7.825877e-03 1.269701e-01 3.611332e-01 2.957708e-01 5.525767e-02 4.282642e-03 2.041884e-05

Listing 13.4 showcases the exception handling of a non-numeric anylist.

Listing 13.4: Exception: function kerndens.anylist(), no numeric-vector member
Code
spatstat.data::Kovesi$values |> 
  kerndens() |>
  is.null()
# [1] TRUE

13.2 Quantile of numeric-vectors

The S3 method quantile.anylist() (Listing 13.5) finds the quantiles of each numeric-vector member of an anylist.

Listing 13.5: Example: function quantile.anylist()
Code
set.seed(11); spatstat.geom::anylist(
  rnorm(1e2L),
  rnorm(1e3L)
) |> 
  quantile()
# [[1]]
#         0%        25%        50%        75%       100% 
# -2.1867814 -0.8209852 -0.1868264  0.5014134  2.3396931 
# 
# [[2]]
#           0%          25%          50%          75%         100% 
# -3.276987817 -0.628120701  0.007994794  0.669427485  3.699685561

Listing 13.6 showcases the exception handling of a non-numeric anylist.

Listing 13.6: Exception: function quantile.anylist(), no numeric-vector member
Code
spatstat.data::Kovesi$values |> 
  quantile() |>
  is.null()
# [1] TRUE

13.3 Split

Section 13.3 is intended as an educational handbook for beginners to R version 4.5.2 (2025-10-31) and package spatstat.geom (v3.6.1). This section does not discuss the functionality of package groupedHyperframe (v0.3.2).

The default method of the S3 generic function base::split() splits an anylist into a list of anylist. This feature is made possible by the magic of the S3 method spatstat.geom::`[.anylist`.

Listing 13.7 splits a spatial-object list (solist, Chapter 30) vesicles.extra (Section 10.20) into a list of solist.

Listing 13.7: Review: function base::split.default() on solist
v1 = spatstat.data::vesicles.extra |>
  split.default(f = factor(c('a', 'a', 'b', 'b')))
v2 = spatstat.data::vesicles.extra |>
  split(f = factor(c('a', 'a', 'b', 'b')))
stopifnot(identical(v1, v2))

Listing 13.8 splits a pixel-image-object list (imlist, Chapter 23) gorillas.extra (Section 10.12) into a list of imlist.

Listing 13.8: Review: function base::split.default() on imlist
g1 = spatstat.data::gorillas.extra |>
  split.default(f = factor(c('a', 'a', 'b', 'b', 'c', 'c', 'c')))
g2 = spatstat.data::gorillas.extra |>
  split(f = factor(c('a', 'a', 'b', 'b', 'c', 'c', 'c')))
stopifnot(identical(g1, g2))

Listing 13.9 splits a point-pattern-object list (ppplist, Chapter 28) waterstriders (Section 10.21) into a list of ppplist.

Listing 13.9: Review: function base::split.default() on ppplist
w1 = spatstat.data::waterstriders |>
  split.default(f = factor(c('a', 'a', 'b')))
w2 = spatstat.data::waterstriders |>
  split(f = factor(c('a', 'a', 'b')))
stopifnot(identical(w1, w2))