Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions R/adjacency.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ graph_from_adjacency_matrix <- function(adjmatrix,
),
weighted = NULL, diag = TRUE,
add.colnames = NULL, add.rownames = NA) {
ensure_no_na(adjmatrix, "adjacency matrix")
mode <- igraph.match.arg(mode)

if (!is.matrix(adjmatrix) && !inherits(adjmatrix, "Matrix")) {
Expand Down
9 changes: 4 additions & 5 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -1397,10 +1397,8 @@ graph_from_data_frame <- function(d, directed = TRUE, vertices = NULL) {
}

## Handle if some elements are 'NA'
if (any(is.na(d[, 1:2]))) {
cli::cli_warn("In {.code d}, {.code NA} elements were replaced with string {.str NA}.")
d[, 1:2][is.na(d[, 1:2])] <- "NA"
}
ensure_no_na(d, "edge data frame")

if (!is.null(vertices) && any(is.na(vertices[, 1]))) {
cli::cli_warn("In {.code vertices[,1]}, {.code NA} elements were replaced with string {.str NA}.")
vertices[, 1][is.na(vertices[, 1])] <- "NA"
Expand Down Expand Up @@ -1489,8 +1487,9 @@ from_data_frame <- function(...) constructor_spec(graph_from_data_frame, ...)
#' graph_from_edgelist(cbind(1:10, c(2:10, 1)))
graph_from_edgelist <- function(el, directed = TRUE) {
if (!is.matrix(el) || ncol(el) != 2) {
cli::cli_abort("graph_from_edgelist expects a matrix with two columns")
cli::cli_abort("graph_from_edgelist expects a matrix with two columns.")
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.

Should be markup, but we'll address this later with assistance.

}
ensure_no_na(el, "edgelist")

if (nrow(el) == 0) {
res <- make_empty_graph(directed = directed)
Expand Down
1 change: 1 addition & 0 deletions R/incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ graph_from_biadjacency_matrix <- function(incidence, directed = FALSE,
multiple = FALSE, weighted = NULL,
add.names = NULL) {
# Argument checks
ensure_no_na(incidence, "biadjacency matrix")
directed <- as.logical(directed)
mode <- igraph.match.arg(mode)

Expand Down
7 changes: 7 additions & 0 deletions R/utils-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ igraph.match.arg <- function(arg, values, error_call = rlang::caller_env()) {
error_call = error_call
)
}

#' @importFrom rlang caller_env
ensure_no_na <- function(x, what, call = caller_env()) {
if (anyNA(x)) {
cli::cli_abort("Cannot create a graph object because the {what} contains NAs.", call = call)
}
}
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/adjacency.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@
IGRAPH U--- 2 0 --
+ edges:

# graph_from_adjacency_matrix errors for NAs

Code
graph_from_adjacency_matrix(A)
Condition
Error in `graph_from_adjacency_matrix()`:
! Cannot create a graph object because the adjacency matrix contains NAs.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@
2 2 3 2 b B c C
3 1 3 1 a A c C

# graph_from_edgelist errors for NAs

Code
graph_from_edgelist(A)
Condition
Error in `graph_from_edgelist()`:
! Cannot create a graph object because the edgelist contains NAs.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/incidence.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@
! `multiple` and `weighted` cannot be both `TRUE`.
igraph either interprets numbers larger than 1 as weights or as multiplicities, but it cannot be both.

# graph_from_biadjacency_matrix errors for NAs

Code
graph_from_biadjacency_matrix(A)
Condition
Error in `graph_from_biadjacency_matrix()`:
! Cannot create a graph object because the biadjacency matrix contains NAs.

9 changes: 6 additions & 3 deletions tests/testthat/test-adjacency.R
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@ test_that("sparse/dense matrices no loops works", {
g <- graph_from_adjacency_matrix(A, diag = FALSE)
expect_ecount(g, 1)
expect_equal(get_edge_ids(g, c(1, 2)), 1)

})

test_that("sparse/dense matrices multiple works", {
Expand All @@ -681,11 +680,10 @@ test_that("sparse/dense matrices multiple works", {
expect_ecount(g, 3)
expect_equal(as_edgelist(g), matrix(c(1, 2), 3, 2, byrow = TRUE))

A <- as(A,"dgCMatrix")
A <- as(A, "dgCMatrix")
g <- graph_from_adjacency_matrix(A, diag = FALSE)
expect_ecount(g, 3)
expect_equal(as_edgelist(g), matrix(c(1, 2), 3, 2, byrow = TRUE))

})

test_that("sparse/dense matrices min/max/plus", {
Expand All @@ -712,3 +710,8 @@ test_that("sparse/dense matrices min/max/plus", {
g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "plus", weighted = TRUE)
expect_equal(E(g)$weight[1], 5)
})

test_that("graph_from_adjacency_matrix errors for NAs", {
A <- matrix(c(1, 1, NA, 1), 2, 2)
expect_snapshot(graph_from_adjacency_matrix(A), error = TRUE)
})
5 changes: 5 additions & 0 deletions tests/testthat/test-conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ test_that("edge names work", {
)
})

test_that("graph_from_edgelist errors for NAs", {
A <- matrix(c(1, 2, NA, 1), 2, 2)
expect_snapshot(graph_from_edgelist(A), error = TRUE)
})

test_that("graph_from_data_frame works with factors", {
actors <- data.frame(
name = c("Alice", "Bob", "Cecil", "David", "Esmeralda"),
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,8 @@ test_that("graph_from_biadjacency_matrix() errors well", {
(g <- graph_from_biadjacency_matrix(inc, multiple = TRUE, weighted = TRUE))
})
})

test_that("graph_from_biadjacency_matrix errors for NAs", {
A <- matrix(c(1, 1, NA, 1), 2, 2)
expect_snapshot(graph_from_biadjacency_matrix(A), error = TRUE)
})
Loading