Skip to contents

Wrapper for iteration over the Regions in a SpatialMap object

Usage

.smapply(
  object,
  ...,
  all = F,
  analyze = NULL,
  cores = NULL,
  parallel = NULL,
  future.init = NULL,
  cat.output = NULL,
  python = FALSE,
  progress = NULL
)

Arguments

object

The SpatialMap object

...

Parameters passed to lapply, namely to FUN

all

Whether to iterate over all Regions in the SpatialMap. Deprecated - now this will be handled by the analyze parameter.

analyze

What to analyze (and how). The options are "regions", NULL (activeAnalysis), the name of a currently existing formal analysis, or the name of a column in object's projectMetadata (an "informal" analysis).

cores

Parallelization options

parallel

Parallelization options

future.init

Whether future framework has been initialized already. Typically you don't need to interact with this mostly internal parameter

cat.output

Controls return behavior.

Defaults to NULL, in which case .smapply will try to return a valid SpatialMap object. FUN must return a valid Region object in this case.

Accepts a quoted function name (e.g. "rbind") which will be passed to Reduce(f = cat.output) in order to concatenate the output of FUN.

Alternatively, pass "none" to return the output as a list (e.g. a list of plots).

python

Whether or not a Python environment needs to be initialized to run the specified FUN (...).

progress

Whether or not to display a progress bar when iterating

Value

  • If cat.output is NULL, returns a SpatialMap object.

  • If cat.output is "none", returns a list object, named by the Regions or by the Analysis.

  • If cat.output is a quoted function (e.g. "rbind"), returns that list after concatenation via Reduce(f = cat.output)

Examples


sm_tonsil <- load_sm_data("tonsil")

# Returning a modified SpatialMap object

small_tonsil <- .smapply(object = sm_tonsil, function(r) {

  ## Retrieve spatial embeddings from Region 'r'
  emb <- embeddings(r, "spatial")

  ## Take the desired subset from the top corner
  my.subset <- emb[, "x"] < 1000 & facil::inv(emb[, "y"]) < 1000

  ## Check that some cells remain (otherwise things could break)
  if (sum(my.subset) < 2) {
    stop("Subset is empty")
  }

  ## Subset Region r and return
  r[my.subset]
})

# Returning a list

thousandth_cells_biomarkers <- .smapply(object = sm_tonsil, function(r) {
  dat <- Data(r, "Data")
  dat[, 1000]
},
cat.output = "none")

# Returning a dataframe

thousandth_cells_biomarkers_df <- .smapply(object = sm_tonsil, function(r) {
  dat <- Data(r, "Data")
  thousandth <- t(dat[, 1000, drop = FALSE]) %>%
    dplyr::as_tibble()
  res <- dplyr::bind_cols(Region = getID(r),
                          thousandth)
  return(res)
},
cat.output = "rbind")

Scroll to top