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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export(as_adjacency_matrix)
export(as_biadjacency_matrix)
export(as_bipartite)
export(as_data_frame)
export(as_directed)
export(as_edgelist)
export(as_graphnel)
export(as_ids)
Expand All @@ -156,6 +157,7 @@ export(as_membership)
export(as_phylo)
export(as_star)
export(as_tree)
export(as_undirected)
export(assortativity)
export(assortativity.degree)
export(assortativity.nominal)
Expand Down
58 changes: 44 additions & 14 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ as_edgelist <- function(graph, names = TRUE) {

#' Convert between directed and undirected graphs
#'
#' `as.directed()` converts an undirected graph to directed,
#' `as.undirected()` does the opposite, it converts a directed graph to
#' `as_directed()` converts an undirected graph to directed,
#' `as_undirected()` does the opposite, it converts a directed graph to
#' undirected.
#'
#' Conversion algorithms for `as.directed()`: \describe{
#' Conversion algorithms for `as_directed()`: \describe{
#' \item{"arbitrary"}{The number of edges in the graph stays the same, an
#' arbitrarily directed edge is created for each undirected edge, but the
#' direction of the edge is deterministic (i.e. it always points the same
Expand All @@ -435,7 +435,7 @@ as_edgelist <- function(graph, names = TRUE) {
#' graph contained loop edges.}
#' }
#'
#' Conversion algorithms for `as.undirected()`: \describe{
#' Conversion algorithms for `as_undirected()`: \describe{
#' \item{"each"}{The number of edges remains constant, an undirected edge
#' is created for each directed one, this version might create graphs with
#' multiple edges.} \item{"collapse"}{One undirected edge will be created
Expand All @@ -445,11 +445,11 @@ as_edgelist <- function(graph, names = TRUE) {
#' edges are ignored. This mode might create multiple edges if there are more
#' than one mutual edge pairs between the same pair of vertices. } }
#'
#' @aliases as.directed as.undirected
#' @aliases as_directed as_undirected
#' @param graph The graph to convert.
#' @param mode Character constant, defines the conversion algorithm. For
#' `as.directed()` it can be `mutual` or `arbitrary`. For
#' `as.undirected()` it can be `each`, `collapse` or
#' `as_directed()` it can be `mutual` or `arbitrary`. For
#' `as_undirected()` it can be `each`, `collapse` or
#' `mutual`. See details below.
#' @return A new graph object.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
Expand All @@ -461,14 +461,14 @@ as_edgelist <- function(graph, names = TRUE) {
#' @examples
#'
#' g <- make_ring(10)
#' as.directed(g, "mutual")
#' as_directed(g, "mutual")
#' g2 <- make_star(10)
#' as.undirected(g)
#' as_undirected(g)
#'
#' # Combining edge attributes
#' g3 <- make_ring(10, directed = TRUE, mutual = TRUE)
#' E(g3)$weight <- seq_len(ecount(g3))
#' ug3 <- as.undirected(g3)
#' ug3 <- as_undirected(g3)
#' print(ug3, e = TRUE)

#' @examplesIf rlang::is_interactive()
Expand All @@ -485,23 +485,23 @@ as_edgelist <- function(graph, names = TRUE) {
#' 9, 8, 9, 8, 9, 9, 10, 10, 10, 10
#' ))
#' E(g4)$weight <- seq_len(ecount(g4))
#' ug4 <- as.undirected(g4,
#' ug4 <- as_undirected(g4,
#' mode = "mutual",
#' edge.attr.comb = list(weight = length)
#' )
#' print(ug4, e = TRUE)
#'
#' @cdocs igraph_to_directed
as.directed <- to_directed_impl
as_directed <- to_directed_impl

#' @rdname as.directed
#' @rdname as_directed
#' @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
#' this.
#' @export
as.undirected <- function(graph, mode = c("collapse", "each", "mutual"), edge.attr.comb = igraph_opt("edge.attr.comb")) {
as_undirected <- function(graph, mode = c("collapse", "each", "mutual"), edge.attr.comb = igraph_opt("edge.attr.comb")) {
# Argument checks
ensure_igraph(graph)
mode <- switch(igraph.match.arg(mode),
Expand Down Expand Up @@ -1207,3 +1207,33 @@ as.matrix.igraph <- function(x, matrix.type = c("adjacency", "edgelist"), ...) {
edgelist = as_edgelist(graph = x, ...)
)
}

#' Convert between directed and undirected graphs
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `as.directed()` was renamed to `as_directed()` to create a more
#' consistent API.
#' @inheritParams as_directed
#' @keywords internal
#' @export
as.directed <- function(graph, mode = c("mutual", "arbitrary", "random", "acyclic")) {
lifecycle::deprecate_soft("2.0.4", "as.directed()", "as_directed()")
as_directed(graph, mode = mode)
}

#' Convert between undirected and unundirected graphs
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `as.undirected()` was renamed to `as_undirected()` to create a more
#' consistent API.
#' @inheritParams as_undirected
#' @keywords internal
#' @export
as.undirected <- function(graph, mode = c("collapse", "each", "mutual")) {
lifecycle::deprecate_soft("2.0.4", "as.undirected()", "as_undirected()")
as_undirected(graph = graph, mode = mode)
}
4 changes: 2 additions & 2 deletions R/flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value.
#' vertex_disjoint_paths(g2, 100, 1)
#'
#' g <- sample_gnp(50, 5 / 50)
#' g <- as.directed(g)
#' g <- as_directed(g)
#' g <- induced_subgraph(g, subcomponent(g, 1))
#' cohesion(g)
#'
Expand Down Expand Up @@ -527,7 +527,7 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR
#' edge_disjoint_paths(g2, 100, 1)
#'
#' g <- sample_gnp(50, 5 / 50)
#' g <- as.directed(g)
#' g <- as_directed(g)
#' g <- induced_subgraph(g, subcomponent(g, 1))
#' adhesion(g)
#'
Expand Down
2 changes: 1 addition & 1 deletion R/layout_drl.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ layout.drl <- function(graph, use.seed = FALSE, seed = matrix(runif(vcount(graph
#' @keywords graphs
#' @examples
#'
#' g <- as.undirected(sample_pa(100, m = 1))
#' g <- as_undirected(sample_pa(100, m = 1))
#' l <- layout_with_drl(g, options = list(simmer.attraction = 0))
#' plot(g, layout = l, vertex.size = 3, vertex.label = NA)
#'
Expand Down
2 changes: 1 addition & 1 deletion R/paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ is_dag <- is_dag_impl
#'
#' g <- make_graph(c(1,2, 1,3, 2,4, 3,4), directed = TRUE)
#' is_acyclic(g)
#' is_acyclic(as.undirected(g))
#' is_acyclic(as_undirected(g))
#' @seealso [is_forest()] and [is_dag()] for functions specific to undirected
#' and directed graphs.
#' @family cycles
Expand Down
2 changes: 1 addition & 1 deletion R/structural.properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ subgraph.edges <- function(graph, eids, delete.vertices = TRUE) { # nocov start
#'
#' The `barrat` type of transitivity does not work for graphs with
#' multiple and/or loop edges. If you want to calculate it for a directed
#' graph, call [as.undirected()] with the `collapse` mode first.
#' graph, call [as_undirected()] with the `collapse` mode first.
#'
#' @param graph The graph to analyze.
#' @param type The type of the transitivity to calculate. Possible values:
Expand Down
111 changes: 6 additions & 105 deletions man/as.directed.Rd

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

2 changes: 1 addition & 1 deletion man/as.matrix.igraph.Rd

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

23 changes: 23 additions & 0 deletions man/as.undirected.Rd

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

2 changes: 1 addition & 1 deletion man/as_adj_list.Rd

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

2 changes: 1 addition & 1 deletion man/as_adjacency_matrix.Rd

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

2 changes: 1 addition & 1 deletion man/as_biadjacency_matrix.Rd

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

Loading