SpatialExperiment-colData {SpatialExperiment} | R Documentation

## SpatialExperiment colData

### Description

The `SpatialExperiment` class provides a modified `colData` setter, which ensures that the `SpatialExperiment` object remains valid.

### Usage

    ## S4 replacement method for signature 'SpatialExperiment,DataFrame'
    colData(x) <- value

    ## S4 replacement method for signature 'SpatialExperiment,'NULL''
    colData(x) <- value

### Arguments

`x` | a `SpatialExperiment`
`value` | a `DataFrame`

### Details

The `colData` setter performs several checks to ensure validity. If the replacement `colData` does not contain a `sample_id` column, the existing `sample_id`s will be retained. If the replacement `colData` contains `sample_id`s, a check is performed to ensure the number of unique `sample_id`s is the same, i.e. a one-to-one mapping is possible. If the replacement is `NULL`, the `sample_id`s are retained. In addition, checks are performed against the `sample_id`s in `imgData`.

### Value

a `SpatialExperiment` object with updated `colData`

### Examples

    example(read10xVisium)

    # return additional columns for colData
    head(colData(spe, spatialData = TRUE, spatialCoords = TRUE))

    # empty replacement retains sample identifiers
    colData(spe) <- NULL
    names(colData(spe))

    # replacement of sample identifiers
    # requires one-to-one mapping

    ## invalid replacement

    tryCatch(
      spe$sample_id <- seq(ncol(spe)),
      error = function(e) message(e))

    ## valid replacement

    old <- c("section1", "section2")
    new <- c("sample_A", "sample_B")
    idx <- match(spe$sample_id, old)

    tmp <- spe
    tmp$sample_id <- new[idx]
    table(spe$sample_id, tmp$sample_id)

[Package SpatialExperiment version 1.4.0 Index]