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
31 changes: 21 additions & 10 deletions R/interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ neighbors <- function(graph, v, mode = c("out", "in", "all", "total")) {
#' @param mode Whether to query outgoing (\sQuote{out}), incoming
#' (\sQuote{in}) edges, or both types (\sQuote{all}). This is
#' ignored for undirected graphs.
#' @param loops Specifies how to treat loop edges.
#' `"none"` removes loop edges from the result.
#' `"once"` makes each loop edge appear only once in the result.
#' `"twice"` makes loop edges appear twice in the result
#' if the graph is undirected or `mode` is set to `"all"`
#' (and once otherwise as returning them twice
#' does not make sense for directed graphs).
#' @return An edge sequence containing the incident edges of
#' the input vertex.
#'
Expand All @@ -384,26 +391,30 @@ neighbors <- function(graph, v, mode = c("out", "in", "all", "total")) {
#' g <- make_graph("Zachary")
#' incident(g, 1)
#' incident(g, 34)
incident <- function(graph, v, mode = c("all", "out", "in", "total")) {
incident <- function(
graph,
v,
mode = c("all", "out", "in", "total"),
loops = c("twice", "none", "once")
) {
ensure_igraph(graph)
if (is_directed(graph)) {
mode <- igraph.match.arg(mode)
mode <- switch(mode, "out" = 1, "in" = 2, "all" = 3, "total" = 3)
} else {
mode <- 1
mode <- "out"
}
loops <- igraph.match.arg(loops)
v <- as_igraph_vs(graph, v)
if (length(v) == 0) {
stop("No vertex was specified")
}
on.exit(.Call(R_igraph_finalizer))
res <- .Call(R_igraph_incident, graph, v - 1, as.numeric(mode)) + 1L

if (igraph_opt("return.vs.es")) {
res <- create_es(graph, res)
}

res
incident_impl(
graph,
vid = v,
mode = mode,
loops = loops
)
}

#' Check whether a graph is directed
Expand Down
15 changes: 14 additions & 1 deletion man/incident.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@
Error in `el_to_vec()`:
! The columns of the data.frame are of different type (character and double)

# incident() works

Code
incident(g, 1)
Output
+ 16/78 edges:
[1] 1-- 2 1-- 3 1-- 4 1-- 5 1-- 6 1-- 7 1-- 8 1-- 9 1--11 1--12 1--13 1--14
[13] 1--18 1--20 1--22 1--32

---

Code
incident(g, 34)
Output
+ 17/78 edges:
[1] 9--34 10--34 14--34 15--34 16--34 19--34 20--34 21--34 23--34 24--34
[11] 27--34 28--34 29--34 30--34 31--34 32--34 33--34

7 changes: 7 additions & 0 deletions tests/testthat/test-interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,10 @@ test_that("get_edge_id() errors correctly for wrong matrices", {
mat <- matrix(c(1, 2, 1, 3, 1, 4), nrow = 2, ncol = 3)
lifecycle::expect_deprecated(get_edge_ids(g, mat))
})

test_that("incident() works", {
local_igraph_options(print.id = FALSE)
g <- make_graph("Zachary")
expect_snapshot(incident(g, 1))
expect_snapshot(incident(g, 34))
})