Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c75e174
Add attribute combination support to graph operators (union, intersec…
schochastics May 28, 2026
41c4b0c
Merge branch 'main' into feat-attrib_comb
krlmlr May 28, 2026
5f7521a
make document work locally
schochastics May 28, 2026
cb49b91
Merge branch 'main' into feat-attrib_comb
schochastics Jun 6, 2026
d7e8bdc
chore: Auto-update from GitHub Actions
schochastics Jun 6, 2026
c26ab02
Merge branch 'main' into feat-attrib_comb
schochastics Jun 10, 2026
f9ad5b3
refactor: snake_case graph-operator attr-comb args, extract rename he…
schochastics Jun 10, 2026
b58da44
feat: snake_case *.attr.comb arguments and options, soft-deprecate do…
schochastics Jun 10, 2026
04ab715
chore: Auto-update from GitHub Actions
schochastics Jun 10, 2026
6d94668
Add `graph_attr_comb` igraph option to control default graph attribut…
schochastics Jun 16, 2026
5bebc2e
Merge remote-tracking branch 'origin/main' into feat-attrib_comb
schochastics Jun 16, 2026
05336ba
chore: Auto-update from GitHub Actions
schochastics Jun 16, 2026
a85a78c
rename *_comb to *_combine
schochastics Jun 16, 2026
d1ac089
chore: Auto-update from GitHub Actions
schochastics Jun 16, 2026
96e21e2
Sort known_names alphabetically and reorder known_codes accordingly i…
schochastics Jun 25, 2026
874f8eb
Add documentation for attribute combination codes and use integer lit…
schochastics Jun 25, 2026
eb89372
Reorder condition check in attribute combination to test for "rename"…
schochastics Jun 25, 2026
fde3bed
add example to rename
schochastics Jun 25, 2026
fbf2bcc
Update testthat minimum version to >= 3.3.0 and use expect_all_true
schochastics Jun 25, 2026
9a635d7
Merge remote-tracking branch 'origin/main' into feat-attrib_comb
schochastics Jun 25, 2026
409b310
chore: Auto-update from GitHub Actions
schochastics Jun 25, 2026
a8987c4
Merge branch 'main' into feat-attrib_comb
schochastics Jun 25, 2026
a190d59
revert: keep attr-comb option/arg names dotted; defer par rename to s…
schochastics Jul 8, 2026
757f82d
Merge remote-tracking branch 'origin/main' into feat-attrib_comb
schochastics Jul 8, 2026
69bd17e
chore: Auto-update from GitHub Actions
schochastics Jul 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/aaa-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ connect_neighborhood_impl <- function(
contract_vertices_impl <- function(
graph,
mapping,
vertex_attr_comb = igraph_opt("vertex.attr.comb")
vertex_attr_comb = igraph_opt("vertex_attr_comb")
) {
# Argument checks
ensure_igraph(graph)
Expand Down Expand Up @@ -245,7 +245,7 @@ simplify_impl <- function(
graph,
remove_multiple = TRUE,
remove_loops = TRUE,
edge_attr_comb = igraph_opt("edge.attr.comb")
edge_attr_comb = igraph_opt("edge_attr_comb")
) {
# Argument checks
ensure_igraph(graph)
Expand Down
2 changes: 1 addition & 1 deletion R/aaa-structural.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ to_directed_impl <- function(
to_undirected_impl <- function(
graph,
mode = c("collapse", "each", "mutual"),
edge_attr_comb = igraph_opt("edge.attr.comb")
edge_attr_comb = igraph_opt("edge_attr_comb")
) {
# Argument checks
ensure_igraph(graph)
Expand Down
57 changes: 36 additions & 21 deletions R/attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ is_bipartite <- function(graph) {

#############

igraph.i.attribute.combination <- function(comb) {
igraph.i.attribute.combination <- function(comb, allow_rename = FALSE) {
Comment thread
schochastics marked this conversation as resolved.
if (is.function(comb)) {
comb <- list(comb)
}
Expand All @@ -1310,34 +1310,40 @@ igraph.i.attribute.combination <- function(comb) {
if (anyDuplicated(names(comb)) > 0) {
cli::cli_warn("Some attributes are duplicated")
}
known_names <- c(
"ignore",
Comment thread
schochastics marked this conversation as resolved.
Outdated
"sum",
"prod",
"min",
"max",
"random",
"first",
"last",
"mean",
"median",
"concat"
)
known_codes <- c(0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
Comment thread
schochastics marked this conversation as resolved.
Outdated
if (allow_rename) {
known_names <- c(known_names, "rename")
known_codes <- c(known_codes, NA_integer_)
}
comb <- lapply(comb, function(x) {
if (!is.character(x)) {
x
} else {
known <- data.frame(
n = c(
"ignore",
"sum",
"prod",
"min",
"max",
"random",
"first",
"last",
"mean",
"median",
"concat"
),
i = c(0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
stringsAsFactors = FALSE
)
x <- pmatch(tolower(x), known[, 1])
if (is.na(x)) {
idx <- pmatch(tolower(x), known_names)
if (is.na(idx)) {
if (!allow_rename && identical(tolower(x), "rename")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be more logical to me to invert the two

if (identical(tolower(x), "rename") && !allow_rename) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why identical btw?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you seem to hold a grudge against identical() 😛
Just very defensive type-strict scalar comparison. Probably == would work here but its igraph, so better be save 😉

cli::cli_abort(
"{.val rename} is only supported by graph operators ({.fn union}, {.fn intersection}, {.fn compose}, {.fn disjoint_union}), not by this function."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrase to

  • Can't use rename with function X (possible to add its name)
  • Hint: rename is only supported by blablabla

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets surprisingly complicated so I would opt out of that and keep it as is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

Can't use rename with this function (not naming it)
Hint: rename is only supported by blablabla

)
}
cli::cli_abort(
"Unknown/unambigous attribute combination specification."
)
}
known[, 2][x]
if (is.na(known_codes[idx])) "rename" else known_codes[idx]
}
})

Expand Down Expand Up @@ -1435,6 +1441,15 @@ igraph.i.attribute.combination <- function(comb) {
#' Concatenate the attributes, using the [c()] function.
#' This results almost always a complex attribute.
#' }
#' \item{"rename"}{
#' Keep clashing attributes side-by-side under disambiguated names by
#' appending `_1`, `_2`, ... suffixes. This is the default for the
Comment thread
schochastics marked this conversation as resolved.
Outdated
#' graph operators [union()], [intersection()], [compose()] and
#' [disjoint_union()] and preserves their historical behaviour.
#' Only those operators accept `"rename"`; [simplify()] and
#' [contract()] will reject it because the rename strategy has no
Comment thread
schochastics marked this conversation as resolved.
#' per-element interpretation when many input values collapse into one.
#' }
#' }
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @seealso [graph_attr()], [vertex_attr()],
Expand Down
34 changes: 29 additions & 5 deletions R/community.R
Original file line number Diff line number Diff line change
Expand Up @@ -3201,13 +3201,14 @@ communities <- groups.communities
#'
#' The attributes of the graph are kept. Graph and edge attributes are
#' unchanged, vertex attributes are combined, according to the
#' `vertex.attr.comb` parameter.
#' `vertex_attr_comb` parameter.
#'
#' @param graph The input graph, it can be directed or undirected.
#' @param mapping A numeric vector that specifies the mapping. Its elements
#' correspond to the vertices, and for each element the ID in the new graph is
#' given.
#' @param vertex.attr.comb Specifies how to combine the vertex attributes in
#' @inheritParams rlang::args_dots_empty
#' @param vertex_attr_comb Specifies how to combine the vertex attributes in
#' the new graph. Please see [attribute.combination()] for details.
#' @return A new graph object.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
Expand All @@ -3220,7 +3221,7 @@ communities <- groups.communities
#' E(g)$weight <- runif(ecount(g))
#'
#' g2 <- contract(g, rep(1:5, each = 2),
#' vertex.attr.comb = toString
#' vertex_attr_comb = toString
#' )
#'
#' ## graph and edge attributes are kept, vertex attributes are
Expand All @@ -3232,12 +3233,35 @@ communities <- groups.communities
contract <- function(
graph,
mapping,
vertex.attr.comb = igraph_opt("vertex.attr.comb")
...,
vertex_attr_comb = igraph_opt("vertex_attr_comb")
) {
# BEGIN GENERATED ARG_HANDLE: contract, do not edit, see tools/generate-migrations.R
if (...length() > 0L) {
.arg_handle <- migrate_recover_args(
list(...),
current = list(vertex_attr_comb = vertex_attr_comb),
recover_new = c("vertex_attr_comb"),
recover_old = c("vertex.attr.comb"),
match_names = c("vertex.attr.comb", "vertex_attr_comb"),
match_to = c("vertex_attr_comb", "vertex_attr_comb"),
defaults = list(vertex_attr_comb = igraph_opt("vertex_attr_comb")),
head_args = c("graph", "mapping"),
fn_name = "contract"
)
list2env(.arg_handle$values, environment())
lifecycle::deprecate_soft(
"3.0.0",
what = I(.arg_handle$what),
details = .arg_handle$details
)
}
# END GENERATED ARG_HANDLE

contract_vertices_impl(
graph = graph,
mapping = mapping,
vertex_attr_comb = vertex.attr.comb
vertex_attr_comb = vertex_attr_comb
)
}

Expand Down
32 changes: 28 additions & 4 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ as_edgelist <- function(graph, names = TRUE) {
#' E(g4)$weight <- seq_len(ecount(g4))
#' ug4 <- as_undirected(g4,
#' mode = "mutual",
#' edge.attr.comb = list(weight = length)
#' edge_attr_comb = list(weight = length)
#' )
#' print(ug4, e = TRUE)
#'
Expand All @@ -570,7 +570,8 @@ as_directed <- function(
}

#' @rdname as_directed
#' @param edge.attr.comb Specifies what to do with edge attributes, if
#' @inheritParams rlang::args_dots_empty
#' @param edge_attr_comb Specifies what to do with edge attributes, if
#' `mode="collapse"` or `mode="mutual"`. In these cases many edges
#' might be mapped to a single one in the new graph, and their attributes are
#' combined. Please see [attribute.combination()] for details on
Expand All @@ -579,8 +580,31 @@ as_directed <- function(
as_undirected <- function(
graph,
mode = c("collapse", "each", "mutual"),
edge.attr.comb = igraph_opt("edge.attr.comb")
...,
edge_attr_comb = igraph_opt("edge_attr_comb")
) {
# BEGIN GENERATED ARG_HANDLE: as_undirected, do not edit, see tools/generate-migrations.R
if (...length() > 0L) {
.arg_handle <- migrate_recover_args(
list(...),
current = list(edge_attr_comb = edge_attr_comb),
recover_new = c("edge_attr_comb"),
recover_old = c("edge.attr.comb"),
match_names = c("edge.attr.comb", "edge_attr_comb"),
match_to = c("edge_attr_comb", "edge_attr_comb"),
defaults = list(edge_attr_comb = igraph_opt("edge_attr_comb")),
head_args = c("graph", "mode"),
fn_name = "as_undirected"
)
list2env(.arg_handle$values, environment())
lifecycle::deprecate_soft(
"3.0.0",
what = I(.arg_handle$what),
details = .arg_handle$details
)
}
# END GENERATED ARG_HANDLE

# Argument checks
ensure_igraph(graph)
mode <- igraph_match_arg(mode)
Expand All @@ -589,7 +613,7 @@ as_undirected <- function(
res <- to_undirected_impl(
graph = graph,
mode = mode,
edge_attr_comb = edge.attr.comb
edge_attr_comb = edge_attr_comb
)

res
Expand Down
Loading