extract {crochet} | R Documentation |

## Create an Implementation of [ For Custom Matrix-Like Types

### Description

`extract`

is a function that converts different index types such as
negative integer vectors, character vectors, or logical vectors passed to
the `[`

function as `i`

(e.g. `X[i]`

) or `i`

and
`j`

(e.g. `X[i, j]`

) into positive integer vectors. The
converted indices are provided as the `i`

parameter of
`extract_vector`

or `i`

and `j`

parameters of
`extract_matrix`

to facilitate implementing the extraction mechanism
for custom matrix-like types.

### Usage

```
extract(extract_vector, extract_matrix, allowDoubles = FALSE)
```

### Arguments

`extract_vector` |
A function in the form of |

`extract_matrix` |
A function in the form of |

`allowDoubles` |
If set, indices of type double are not converted to integers if the
operation would overflow to support matrices with |

### Details

The custom type must implement methods for `length`

,
`dim`

and `dimnames`

for this function
to work. Implementing methods for `nrow`

,
`ncol`

, `rownames`

, and
`colnames`

is not necessary as the default method of
those generics calls `dim`

or
`dimnames`

internally.

Optional arguments are supported and will be passed to
`extract_vector`

and `extract_matrix`

as long as they are named.

### Value

A function in the form of `function(x, i, j, ..., drop = TRUE)`

that
is meant to be used as a method for `[`

for a custom
type.

### See Also

`vignette("StringMatrix", package = "crochet")`

for a vignette
containing a complete example on how to use `extract`

to implement
`[`

for a custom type.

### Examples

```
b <- matrix(data = rnorm(25), nrow = 5, ncol = 5)
dimnames(b) <- list(letters[1:5], letters[1:5])
a <- structure(list(), class = "TestMatrix")
dim.TestMatrix <- function(x) {
dim(b)
}
dimnames.TestMatrix <- function(x) {
dimnames(b)
}
extract_vector <- function(x, i) {
# Dispatch to b instead to x for this demo
b[i, drop = FALSE]
}
extract_matrix <- function(x, i, j) {
# Dispatch to b instead to x for this demo
b[i, j, drop = FALSE]
}
`[.TestMatrix` <- extract(extract_vector = extract_vector, extract_matrix = extract_matrix)
```

*crochet*version 2.3.0 Index]