| smap2 | R Documentation |
These functions apply a function to each unique structure combination in two glycan structure vectors, taking advantage of hash-based deduplication to avoid redundant computation. Similar to purrr map2 functions, but optimized for glycan structure vectors.
smap2(.x, .y, .f, ...)
smap2_vec(.x, .y, .f, ..., .ptype = NULL)
smap2_lgl(.x, .y, .f, ...)
smap2_int(.x, .y, .f, ...)
smap2_dbl(.x, .y, .f, ...)
smap2_chr(.x, .y, .f, ...)
smap2_structure(.x, .y, .f, ...)
.x |
A glycan structure vector (glyrepr_structure). |
.y |
A vector of the same length as |
.f |
A function that takes an igraph object (from |
... |
Additional arguments passed to |
.ptype |
A prototype for the return type (for |
These functions only compute .f once for each unique combination of structure and corresponding
.y value, then map the results back to the original vector positions. This is much more efficient
than applying .f to each element pair individually when there are duplicate structure-value combinations.
smap2_structure() reuses unchanged graphs and validates and canonicalizes
changed graphs returned by .f. A callback that changes vertex identities
or components of a floating structure must also update its floating_parts
and floating_substituents metadata.
NA Handling:
NA elements in .x are preserved in the output - the function is not applied to NA positions,
and the corresponding results are set to NA.
Return Types:
smap2(): Returns a list with the same length as .x
smap2_vec(): Returns an atomic vector with the same length as .x
smap2_lgl(): Returns a logical vector
smap2_int(): Returns an integer vector
smap2_dbl(): Returns a double vector
smap2_chr(): Returns a character vector
smap2_structure(): Returns a new glycan structure vector (.f must return igraph objects)
smap2(): A list
smap2_vec(): An atomic vector of type specified by .ptype
smap2_lgl/int/dbl/chr(): Atomic vectors of the corresponding type
smap2_structure(): A new glyrepr_structure object
# Create structure vectors with duplicates
core1 <- o_glycan_core_1()
core2 <- n_glycan_core()
structures <- c(core1, core2, core1) # core1 appears twice
weights <- c(1.0, 2.0, 1.0) # corresponding weights
# Map a function that uses both structure and weight
smap2_dbl(structures, weights, function(g, w) igraph::vcount(g) * w)
# Use purrr-style lambda functions
smap2_dbl(structures, weights, ~ igraph::vcount(.x) * .y)
# Test with recycling (single weight for all structures)
smap2_dbl(structures, 2.5, ~ igraph::vcount(.x) * .y)
# Map a function that modifies structure based on second argument
# This example adds a graph attribute instead of modifying topology
add_weight_attr <- function(g, weight) {
igraph::set_graph_attr(g, "weight", weight)
}
weights_to_add <- c(1.5, 2.5, 1.5)
smap2_structure(structures, weights_to_add, add_weight_attr)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.