Skip to content

Commit 7e6f575

Browse files
authored
fix: keep vertex attribute type for disjoint_union() (#1909)
1 parent 3008dfc commit 7e6f575

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

R/operators.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,19 @@ disjoint_union <- function(...) {
244244
noattr <- setdiff(names(attr), names(va)) # existint and missing
245245
newattr <- setdiff(names(va), names(attr)) # new
246246
for (a in seq_along(exattr)) {
247-
attr[[exattr[a]]] <- c(attr[[exattr[a]]], va[[exattr[a]]])
247+
attr[[exattr[a]]] <- vctrs::vec_c(attr[[exattr[a]]], va[[exattr[a]]])
248248
}
249249
for (a in seq_along(noattr)) {
250-
attr[[noattr[a]]] <- c(attr[[noattr[a]]], rep(NA, vc[i]))
250+
attr[[noattr[a]]] <- vctrs::vec_c(
251+
attr[[noattr[a]]],
252+
vctrs::unspecified(vc[[i]])
253+
)
251254
}
252255
for (a in seq_along(newattr)) {
253-
attr[[newattr[a]]] <- c(rep(NA, cumvc[i]), va[[newattr[a]]])
256+
attr[[newattr[a]]] <- vctrs::vec_c(
257+
vctrs::unspecified(cumvc[[i]]),
258+
va[[newattr[a]]]
259+
)
254260
}
255261
}
256262
vertex.attributes(res) <- attr

tests/testthat/test-operators.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test_that("disjoint_union() works", {
3030
)
3131
})
3232

33-
test_that("disjoint_union() does not convert types", {
33+
test_that("disjoint_union() does not convert edge types", {
3434
# https://github.com/igraph/rigraph/issues/761
3535

3636
g1 <- make_graph(~ A - -B)
@@ -44,6 +44,28 @@ test_that("disjoint_union() does not convert types", {
4444
expect_s3_class(E(u)$date, c("POSIXct", "POSIXt"))
4545
})
4646

47+
test_that("disjoint_union() does not convert vertex types", {
48+
# https://github.com/igraph/rigraph/issues/1640
49+
50+
g1 <- make_graph(~ B --C, C --D)
51+
g2 <- make_graph(~ A --G, E --F)
52+
53+
g1 <- set_vertex_attr(
54+
g1,
55+
"date",
56+
value = as.POSIXct(c("2021-01-01 01:01:01", "2022-02-02 02:02:02", "2023-03-03 03:03:03"))
57+
)
58+
g2 <- set_vertex_attr(
59+
g2,
60+
"date",
61+
value = as.POSIXct(c("2021-03-03 03:03:03", "2022-04-04 04:04:04", "2023-05-05 05:05:05", "2024-06-06 06:06:06"))
62+
)
63+
64+
u <- disjoint_union(g1, g2)
65+
66+
expect_s3_class(vertex_attr(u, "date"), c("POSIXct", "POSIXt"))
67+
})
68+
4769
test_that("intersection() works", {
4870
g1 <- make_ring(10)
4971
g2 <- make_star(11, center = 11, mode = "undirected")

0 commit comments

Comments
 (0)