Learn R Programming

crochet (version 2.3.0)

replace: Create an Implementation of [<- For Custom Matrix-Like Types

Description

replace 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 replace_vector or i and j parameters of replace_matrix to facilitate implementing the replacement mechanism for custom matrix-like types. Values are recycled to match the replacement length.

Usage

replace(replace_vector, replace_matrix, allowDoubles = FALSE)

Arguments

replace_vector

A function in the form of function(x, i, ..., value) that replaces a vector subset of x based on a single index i with the values in value and returns x.

replace_matrix

A function in the form of function(x, i, j, ..., value) that replaces a matrix subset of x based on two indices i and j with the values in value and returns x.

allowDoubles

If set, indices of type double are not converted to integers if the operation would overflow to support matrices with nrow(), ncol(), or length() greater than the largest integer that can be represented (.Machine$integer.max).

Value

A function in the form of function(x, i, j, ..., value) that is meant to be used as a method for [<- for a custom type.

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.

See Also

vignette("StringMatrix", package = "crochet") for a vignette containing a complete example on how to use replace to implement [<- for a custom type.

Examples

Run this code
# NOT RUN {
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)

replace_vector <- function(x, i, value) {
    .GlobalEnv$i <- i
    .GlobalEnv$value <- value
    # Dispatch to b instead to x for this demo
    with(.GlobalEnv, b[i] <- value)
    # Don't forget to return x
    return(x)
}

replace_matrix <- function(x, i, j, value) {
    .GlobalEnv$i <- i
    .GlobalEnv$j <- j
    .GlobalEnv$value <- value
    # Dispatch to b instead to x for this demo
    with(.GlobalEnv, b[i, j] <- value)
    # Don't forget to return x
    return(x)
}

`[<-.TestMatrix` <- replace(replace_vector = replace_vector, replace_matrix = replace_matrix)
# }

Run the code above in your browser using DataLab