diff --git a/NAMESPACE b/NAMESPACE index cd0c1795e5a..976df8fc2a4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/conversion.R b/R/conversion.R index 6d690240665..03c02dad121 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -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 @@ -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 @@ -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} @@ -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() @@ -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), @@ -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) +} diff --git a/R/flow.R b/R/flow.R index 536b8a21056..607e70dc1ff 100644 --- a/R/flow.R +++ b/R/flow.R @@ -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) #' @@ -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) #' diff --git a/R/layout_drl.R b/R/layout_drl.R index 2b66df1f20b..e9a600b02dc 100644 --- a/R/layout_drl.R +++ b/R/layout_drl.R @@ -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) #' diff --git a/R/paths.R b/R/paths.R index 50cdf2b88c9..f7c5e780d93 100644 --- a/R/paths.R +++ b/R/paths.R @@ -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 diff --git a/R/structural.properties.R b/R/structural.properties.R index 4ded0c44b26..d765c2fc22f 100644 --- a/R/structural.properties.R +++ b/R/structural.properties.R @@ -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: diff --git a/man/as.directed.Rd b/man/as.directed.Rd index 4a36a346c02..d1711005cc3 100644 --- a/man/as.directed.Rd +++ b/man/as.directed.Rd @@ -2,121 +2,22 @@ % Please edit documentation in R/conversion.R \name{as.directed} \alias{as.directed} -\alias{as.undirected} \title{Convert between directed and undirected graphs} \usage{ as.directed(graph, mode = c("mutual", "arbitrary", "random", "acyclic")) - -as.undirected( - graph, - mode = c("collapse", "each", "mutual"), - edge.attr.comb = igraph_opt("edge.attr.comb") -) } \arguments{ \item{graph}{The graph to convert.} \item{mode}{Character constant, defines the conversion algorithm. For -\code{as.directed()} it can be \code{mutual} or \code{arbitrary}. For -\code{as.undirected()} it can be \code{each}, \code{collapse} or +\code{as_directed()} it can be \code{mutual} or \code{arbitrary}. For +\code{as_undirected()} it can be \code{each}, \code{collapse} or \code{mutual}. See details below.} - -\item{edge.attr.comb}{Specifies what to do with edge attributes, if -\code{mode="collapse"} or \code{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 \code{\link[=attribute.combination]{attribute.combination()}} for details on -this.} -} -\value{ -A new graph object. } \description{ -\code{as.directed()} converts an undirected graph to directed, -\code{as.undirected()} does the opposite, it converts a directed graph to -undirected. -} -\details{ -Conversion algorithms for \code{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 -way if you call the function multiple times).} -\item{"mutual"}{Two directed edges are created for each undirected -edge, one in each direction.} -\item{"random"}{The number of edges in the graph stays the same, and -a randomly directed edge is created for each undirected edge. You -will get different results if you call the function multiple times -with the same graph.} -\item{"acyclic"}{The number of edges in the graph stays the same, and -a directed edge is created for each undirected edge such that the -resulting graph is guaranteed to be acyclic. This is achieved by ensuring -that edges always point from a lower index vertex to a higher index. -Note that the graph may include cycles of length 1 if the original -graph contained loop edges.} -} - -Conversion algorithms for \code{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 -for each pair of vertices which are connected with at least one directed -edge, no multiple edges will be created.} \item{"mutual"}{One -undirected edge will be created for each pair of mutual edges. Non-mutual -edges are ignored. This mode might create multiple edges if there are more -than one mutual edge pairs between the same pair of vertices. } } -} -\examples{ - -g <- make_ring(10) -as.directed(g, "mutual") -g2 <- make_star(10) -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) -print(ug3, e = TRUE) -\dontshow{if (rlang::is_interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -x11(width = 10, height = 5) -layout(rbind(1:2)) -plot(g3, layout = layout_in_circle, edge.label = E(g3)$weight) -plot(ug3, layout = layout_in_circle, edge.label = E(ug3)$weight) -\dontshow{\}) # examplesIf} - -g4 <- make_graph(c( - 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 4, - 6, 7, 7, 6, 7, 8, 7, 8, 8, 7, 8, 9, 8, 9, - 9, 8, 9, 8, 9, 9, 10, 10, 10, 10 -)) -E(g4)$weight <- seq_len(ecount(g4)) -ug4 <- as.undirected(g4, - mode = "mutual", - edge.attr.comb = list(weight = length) -) -print(ug4, e = TRUE) +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} +\code{as.directed()} was renamed to \code{as_directed()} to create a more +consistent API. } -\seealso{ -\code{\link[=simplify]{simplify()}} for removing multiple and/or loop edges from -a graph. - -Other conversion: -\code{\link{as.matrix.igraph}()}, -\code{\link{as_adj_list}()}, -\code{\link{as_adjacency_matrix}()}, -\code{\link{as_biadjacency_matrix}()}, -\code{\link{as_data_frame}()}, -\code{\link{as_edgelist}()}, -\code{\link{as_graphnel}()}, -\code{\link{as_long_data_frame}()}, -\code{\link{graph_from_adj_list}()}, -\code{\link{graph_from_graphnel}()} -} -\author{ -Gabor Csardi \email{csardi.gabor@gmail.com} -} -\concept{conversion} -\keyword{graphs} -\section{Related documentation in the C library}{\href{https://igraph.org/c/html/latest/igraph-Structural.html#igraph_to_directed}{\code{igraph_to_directed()}}.} - +\keyword{internal} diff --git a/man/as.matrix.igraph.Rd b/man/as.matrix.igraph.Rd index 7c55a76cc05..b74331c37e4 100644 --- a/man/as.matrix.igraph.Rd +++ b/man/as.matrix.igraph.Rd @@ -47,11 +47,11 @@ as.matrix(g, "adjacency", sparse = FALSE, attr = "weight") } \seealso{ Other conversion: -\code{\link{as.directed}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/as.undirected.Rd b/man/as.undirected.Rd new file mode 100644 index 00000000000..e97443382a7 --- /dev/null +++ b/man/as.undirected.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/conversion.R +\name{as.undirected} +\alias{as.undirected} +\title{Convert between undirected and unundirected graphs} +\usage{ +as.undirected(graph, mode = c("collapse", "each", "mutual")) +} +\arguments{ +\item{graph}{The graph to convert.} + +\item{mode}{Character constant, defines the conversion algorithm. For +\code{as_directed()} it can be \code{mutual} or \code{arbitrary}. For +\code{as_undirected()} it can be \code{each}, \code{collapse} or +\code{mutual}. See details below.} +} +\description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} + +\code{as.undirected()} was renamed to \code{as_undirected()} to create a more +consistent API. +} +\keyword{internal} diff --git a/man/as_adj_list.Rd b/man/as_adj_list.Rd index d181b997f2e..b240ce1799f 100644 --- a/man/as_adj_list.Rd +++ b/man/as_adj_list.Rd @@ -66,11 +66,11 @@ as_adj_edge_list(g) \code{\link[=as_edgelist]{as_edgelist()}}, \code{\link[=as_adj]{as_adj()}} Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/as_adjacency_matrix.Rd b/man/as_adjacency_matrix.Rd index 9ae327accef..db5a0c2343d 100644 --- a/man/as_adjacency_matrix.Rd +++ b/man/as_adjacency_matrix.Rd @@ -83,11 +83,11 @@ as_adjacency_matrix(g, attr = "weight") \code{\link[=graph_from_adjacency_matrix]{graph_from_adjacency_matrix()}}, \code{\link[=read_graph]{read_graph()}} Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/as_biadjacency_matrix.Rd b/man/as_biadjacency_matrix.Rd index bc40afa0f65..c4cd1cd8ca3 100644 --- a/man/as_biadjacency_matrix.Rd +++ b/man/as_biadjacency_matrix.Rd @@ -62,11 +62,11 @@ as_biadjacency_matrix(g) \code{\link[=graph_from_biadjacency_matrix]{graph_from_biadjacency_matrix()}} for the opposite operation. Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/as_directed.Rd b/man/as_directed.Rd new file mode 100644 index 00000000000..73c9e213a56 --- /dev/null +++ b/man/as_directed.Rd @@ -0,0 +1,122 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/conversion.R +\name{as_directed} +\alias{as_directed} +\alias{as_undirected} +\title{Convert between directed and undirected graphs} +\usage{ +as_directed(graph, mode = c("mutual", "arbitrary", "random", "acyclic")) + +as_undirected( + graph, + mode = c("collapse", "each", "mutual"), + edge.attr.comb = igraph_opt("edge.attr.comb") +) +} +\arguments{ +\item{graph}{The graph to convert.} + +\item{mode}{Character constant, defines the conversion algorithm. For +\code{as_directed()} it can be \code{mutual} or \code{arbitrary}. For +\code{as_undirected()} it can be \code{each}, \code{collapse} or +\code{mutual}. See details below.} + +\item{edge.attr.comb}{Specifies what to do with edge attributes, if +\code{mode="collapse"} or \code{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 \code{\link[=attribute.combination]{attribute.combination()}} for details on +this.} +} +\value{ +A new graph object. +} +\description{ +\code{as_directed()} converts an undirected graph to directed, +\code{as_undirected()} does the opposite, it converts a directed graph to +undirected. +} +\details{ +Conversion algorithms for \code{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 +way if you call the function multiple times).} +\item{"mutual"}{Two directed edges are created for each undirected +edge, one in each direction.} +\item{"random"}{The number of edges in the graph stays the same, and +a randomly directed edge is created for each undirected edge. You +will get different results if you call the function multiple times +with the same graph.} +\item{"acyclic"}{The number of edges in the graph stays the same, and +a directed edge is created for each undirected edge such that the +resulting graph is guaranteed to be acyclic. This is achieved by ensuring +that edges always point from a lower index vertex to a higher index. +Note that the graph may include cycles of length 1 if the original +graph contained loop edges.} +} + +Conversion algorithms for \code{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 +for each pair of vertices which are connected with at least one directed +edge, no multiple edges will be created.} \item{"mutual"}{One +undirected edge will be created for each pair of mutual edges. Non-mutual +edges are ignored. This mode might create multiple edges if there are more +than one mutual edge pairs between the same pair of vertices. } } +} +\examples{ + +g <- make_ring(10) +as_directed(g, "mutual") +g2 <- make_star(10) +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) +print(ug3, e = TRUE) +\dontshow{if (rlang::is_interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +x11(width = 10, height = 5) +layout(rbind(1:2)) +plot(g3, layout = layout_in_circle, edge.label = E(g3)$weight) +plot(ug3, layout = layout_in_circle, edge.label = E(ug3)$weight) +\dontshow{\}) # examplesIf} + +g4 <- make_graph(c( + 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 4, + 6, 7, 7, 6, 7, 8, 7, 8, 8, 7, 8, 9, 8, 9, + 9, 8, 9, 8, 9, 9, 10, 10, 10, 10 +)) +E(g4)$weight <- seq_len(ecount(g4)) +ug4 <- as_undirected(g4, + mode = "mutual", + edge.attr.comb = list(weight = length) +) +print(ug4, e = TRUE) + +} +\seealso{ +\code{\link[=simplify]{simplify()}} for removing multiple and/or loop edges from +a graph. + +Other conversion: +\code{\link{as.matrix.igraph}()}, +\code{\link{as_adj_list}()}, +\code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, +\code{\link{as_edgelist}()}, +\code{\link{as_graphnel}()}, +\code{\link{as_long_data_frame}()}, +\code{\link{graph_from_adj_list}()}, +\code{\link{graph_from_graphnel}()} +} +\author{ +Gabor Csardi \email{csardi.gabor@gmail.com} +} +\concept{conversion} +\keyword{graphs} +\section{Related documentation in the C library}{\href{https://igraph.org/c/html/latest/igraph-Structural.html#igraph_to_directed}{\code{igraph_to_directed()}}.} + diff --git a/man/as_edgelist.Rd b/man/as_edgelist.Rd index cb3db5ee491..9708cb79784 100644 --- a/man/as_edgelist.Rd +++ b/man/as_edgelist.Rd @@ -36,12 +36,12 @@ as_edgelist(g) \code{\link[=graph_from_adjacency_matrix]{graph_from_adjacency_matrix()}}, \code{\link[=read_graph]{read_graph()}} Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, diff --git a/man/as_graphnel.Rd b/man/as_graphnel.Rd index 7f0b3f4377c..b4c33bac985 100644 --- a/man/as_graphnel.Rd +++ b/man/as_graphnel.Rd @@ -48,12 +48,12 @@ g4 other graph representations. Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, diff --git a/man/as_long_data_frame.Rd b/man/as_long_data_frame.Rd index 053e8077b5c..4e5e865dee9 100644 --- a/man/as_long_data_frame.Rd +++ b/man/as_long_data_frame.Rd @@ -32,12 +32,12 @@ as_long_data_frame(g) } \seealso{ Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{graph_from_adj_list}()}, diff --git a/man/edge_connectivity.Rd b/man/edge_connectivity.Rd index 0530953375c..e2b96878aab 100644 --- a/man/edge_connectivity.Rd +++ b/man/edge_connectivity.Rd @@ -90,7 +90,7 @@ edge_connectivity(g2, 100, 1) 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) diff --git a/man/graph_from_adj_list.Rd b/man/graph_from_adj_list.Rd index ec4022cf3c2..7a85ac68801 100644 --- a/man/graph_from_adj_list.Rd +++ b/man/graph_from_adj_list.Rd @@ -66,12 +66,12 @@ which_multiple(g3) \code{\link[=as_edgelist]{as_edgelist()}} Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/graph_from_data_frame.Rd b/man/graph_from_data_frame.Rd index afcbdd549a5..81465551cc5 100644 --- a/man/graph_from_data_frame.Rd +++ b/man/graph_from_data_frame.Rd @@ -129,11 +129,11 @@ for another way to create graphs, \code{\link[=read.table]{read.table()}} to rea from files. Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/graph_from_graphnel.Rd b/man/graph_from_graphnel.Rd index c2b08071cd8..29469e22c36 100644 --- a/man/graph_from_graphnel.Rd +++ b/man/graph_from_graphnel.Rd @@ -62,12 +62,12 @@ g4 graph representations. Other conversion: -\code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, \code{\link{as_biadjacency_matrix}()}, \code{\link{as_data_frame}()}, +\code{\link{as_directed}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, \code{\link{as_long_data_frame}()}, diff --git a/man/is_acyclic.Rd b/man/is_acyclic.Rd index 13836664a8b..c00977d8c7e 100644 --- a/man/is_acyclic.Rd +++ b/man/is_acyclic.Rd @@ -23,7 +23,7 @@ cycles in undirected graphs. 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{ \code{\link[=is_forest]{is_forest()}} and \code{\link[=is_dag]{is_dag()}} for functions specific to undirected diff --git a/man/layout_with_drl.Rd b/man/layout_with_drl.Rd index 993d7066c2b..e77f6f62c95 100644 --- a/man/layout_with_drl.Rd +++ b/man/layout_with_drl.Rd @@ -97,7 +97,7 @@ There are five pre-defined parameter settings as well, these are called } \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) diff --git a/man/transitivity.Rd b/man/transitivity.Rd index f0eebaead27..48bb8347a65 100644 --- a/man/transitivity.Rd +++ b/man/transitivity.Rd @@ -85,7 +85,7 @@ the edge weights are the same. The \code{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 \code{\link[=as.undirected]{as.undirected()}} with the \code{collapse} mode first. +graph, call \code{\link[=as_undirected]{as_undirected()}} with the \code{collapse} mode first. } \examples{ diff --git a/man/vertex_connectivity.Rd b/man/vertex_connectivity.Rd index 6f421131305..1585b5a76f0 100644 --- a/man/vertex_connectivity.Rd +++ b/man/vertex_connectivity.Rd @@ -85,7 +85,7 @@ vertex_connectivity(g2, 100, 1) 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) diff --git a/tests/testthat/_snaps/as.directed.md b/tests/testthat/_snaps/as.directed.md new file mode 100644 index 00000000000..2e552572ea9 --- /dev/null +++ b/tests/testthat/_snaps/as.directed.md @@ -0,0 +1,22 @@ +# as.directed() deprecation + + Code + is_directed(as.directed(g, mode = "mutual")) + Condition + Warning: + `as.directed()` was deprecated in igraph 2.0.4. + i Please use `as_directed()` instead. + Output + [1] TRUE + +# as.undirected() deprecation + + Code + is_directed(as.undirected(g, mode = "collapse")) + Condition + Warning: + `as.undirected()` was deprecated in igraph 2.0.4. + i Please use `as_undirected()` instead. + Output + [1] FALSE + diff --git a/tests/testthat/test-alpha.centrality.R b/tests/testthat/test-alpha.centrality.R index 9d18594246d..41274c96e13 100644 --- a/tests/testthat/test-alpha.centrality.R +++ b/tests/testthat/test-alpha.centrality.R @@ -64,7 +64,7 @@ test_that("undirected, alpha centrality works, #653", { ac2 <- alpha_centrality(g, sparse = FALSE) expect_equal(ac1, ac2) - g2 <- as.directed(g, mode = "mutual") + g2 <- as_directed(g, mode = "mutual") ac3 <- alpha_centrality(g, sparse = FALSE) expect_equal(ac1, ac3) }) diff --git a/tests/testthat/test-as.directed.R b/tests/testthat/test-as.directed.R deleted file mode 100644 index 194e658d43c..00000000000 --- a/tests/testthat/test-as.directed.R +++ /dev/null @@ -1,36 +0,0 @@ -test_that("as.directed works", { - g <- sample_gnp(100, 2 / 100) - g2 <- as.directed(g, mode = "mutual") - g3 <- as.directed(g, mode = "arbitrary") - g4 <- as.directed(g, mode = "random") - g5 <- as.directed(g, mode = "acyclic") - - expect_equal(degree(g), degree(g2) / 2) - expect_equal(degree(g), degree(g3)) - expect_equal(degree(g), degree(g4)) - expect_equal(degree(g), degree(g5)) - - expect_isomorphic(g, as.undirected(g2)) - expect_isomorphic(g, as.undirected(g3)) - expect_isomorphic(g, as.undirected(g4)) - expect_isomorphic(g, as.undirected(g5)) -}) - -test_that("as.directed keeps attributes", { - g <- graph_from_literal(A - B - C, D - A, E) - g$name <- "Small graph" - g2 <- as.directed(g, mode = "mutual") - g3 <- as.directed(g, mode = "arbitrary") - expect_equal(g2$name, g$name) - expect_equal(V(g2)$name, V(g)$name) - expect_equal(g3$name, g$name) - expect_equal(V(g3)$name, V(g)$name) - - E(g)$weight <- seq_len(ecount(g)) - g4 <- as.directed(g, "mutual") - df4 <- as_data_frame(g4) - g5 <- as.directed(g, "arbitrary") - df5 <- as_data_frame(g5) - expect_equal(df4[order(df4[, 1], df4[, 2]), ]$weight, c(1, 2, 1, 3, 3, 2)) - expect_equal(df5[order(df5[, 1], df5[, 2]), ]$weight, 1:3) -}) diff --git a/tests/testthat/test-as.undirected.R b/tests/testthat/test-as.undirected.R deleted file mode 100644 index 9d4bf2fd5fd..00000000000 --- a/tests/testthat/test-as.undirected.R +++ /dev/null @@ -1,20 +0,0 @@ -test_that("as.undirected keeps attributes", { - g <- graph_from_literal(A + -+B, A - -+C, C + -+D) - g$name <- "Tiny graph" - E(g)$weight <- seq_len(ecount(g)) - - g2 <- as.undirected(g, mode = "collapse") - df2 <- as_data_frame(g2) - g3 <- as.undirected(g, mode = "each") - df3 <- as_data_frame(g3) - g4 <- as.undirected(g, mode = "mutual") - df4 <- as_data_frame(g4) - - expect_equal(g2$name, g$name) - expect_equal(g3$name, g$name) - expect_equal(g4$name, g$name) - - expect_equal(df2[order(df2[, 1], df2[, 2]), ]$weight, c(4, 2, 9)) - expect_equal(df3[order(df3[, 1], df3[, 2]), ]$weight, c(1, 3, 2, 4, 5)) - expect_equal(df4[order(df4[, 1], df4[, 2]), ]$weight, c(4, 9)) -}) diff --git a/tests/testthat/test-assortativity.R b/tests/testthat/test-assortativity.R index 85a720a01a9..0cc0a56dce1 100644 --- a/tests/testthat/test-assortativity.R +++ b/tests/testthat/test-assortativity.R @@ -22,12 +22,12 @@ test_that("assortativity works", { expect_equal(assortativity_degree(g), assortativity_igraph) expect_equal(assortativity_degree(g), reference_assortativity(g)) - asu <- assortativity_degree(simplify(as.undirected(g, mode = "collapse"))) + asu <- assortativity_degree(simplify(as_undirected(g, mode = "collapse"))) expect_equal(asu, -0.16319921031570466807) p <- read_graph(f <- gzfile("power.gml.gz"), format = "gml") expect_equal(assortativity_degree(p), assortativity(p, degree(p))) - expect_equal(assortativity_degree(p), reference_assortativity(as.directed(p, mode = "mutual"))) + expect_equal(assortativity_degree(p), reference_assortativity(as_directed(p, mode = "mutual"))) }) test_that("nominal assortativity works", { diff --git a/tests/testthat/test-attributes.R b/tests/testthat/test-attributes.R index 118103a9e74..050d3f05718 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -185,8 +185,8 @@ test_that("cannot use vs/es from another graph", { test_that("attribute combinations handle errors correctly", { g <- make_graph(c(1, 2, 2, 1)) E(g)$weight <- c("a", "b") - expect_error(as.undirected(g, edge.attr.comb = list(weight = "sum")), "invalid 'type'") - expect_error(as.undirected(g, edge.attr.comb = list(weight = sum)), "invalid 'type'") + expect_error(as_undirected(g, edge.attr.comb = list(weight = "sum")), "invalid 'type'") + expect_error(as_undirected(g, edge.attr.comb = list(weight = sum)), "invalid 'type'") }) test_that("can change type of attributes (#466)", { diff --git a/tests/testthat/test-centrality.R b/tests/testthat/test-centrality.R index 121696c1861..efc0b74622d 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -19,6 +19,6 @@ test_that("subgraph_centrality() ignored edge directions", { g <- sample_gnm(10, 20, directed = TRUE) expect_equal( subgraph_centrality((g)), - subgraph_centrality(as.undirected(g, mode = "each")) + subgraph_centrality(as_undirected(g, mode = "each")) ) }) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R new file mode 100644 index 00000000000..27ec7370e56 --- /dev/null +++ b/tests/testthat/test-conversion.R @@ -0,0 +1,71 @@ +test_that("as_directed works", { + g <- sample_gnp(100, 2 / 100) + g2 <- as_directed(g, mode = "mutual") + g3 <- as_directed(g, mode = "arbitrary") + g4 <- as_directed(g, mode = "random") + g5 <- as_directed(g, mode = "acyclic") + + expect_equal(degree(g), degree(g2) / 2) + expect_equal(degree(g), degree(g3)) + expect_equal(degree(g), degree(g4)) + expect_equal(degree(g), degree(g5)) + + expect_isomorphic(g, as_undirected(g2)) + expect_isomorphic(g, as_undirected(g3)) + expect_isomorphic(g, as_undirected(g4)) + expect_isomorphic(g, as_undirected(g5)) +}) + +test_that("as_directed keeps attributes", { + g <- graph_from_literal(A - B - C, D - A, E) + g$name <- "Small graph" + g2 <- as_directed(g, mode = "mutual") + g3 <- as_directed(g, mode = "arbitrary") + expect_equal(g2$name, g$name) + expect_equal(V(g2)$name, V(g)$name) + expect_equal(g3$name, g$name) + expect_equal(V(g3)$name, V(g)$name) + + E(g)$weight <- seq_len(ecount(g)) + g4 <- as_directed(g, "mutual") + df4 <- as_data_frame(g4) + g5 <- as_directed(g, "arbitrary") + df5 <- as_data_frame(g5) + expect_equal(df4[order(df4[, 1], df4[, 2]), ]$weight, c(1, 2, 1, 3, 3, 2)) + expect_equal(df5[order(df5[, 1], df5[, 2]), ]$weight, 1:3) +}) + +test_that("as.directed() deprecation", { + local_igraph_options(print.id = FALSE) + + g <- sample_gnp(100, 2 / 100) + expect_snapshot(is_directed(as.directed(g, mode = "mutual"))) +}) + +test_that("as.undirected() deprecation", { + local_igraph_options(print.id = FALSE) + + g <- sample_gnp(100, 2 / 100) + expect_snapshot(is_directed(as.undirected(g, mode = "collapse"))) +}) + +test_that("as_undirected() keeps attributes", { + g <- graph_from_literal(A + -+B, A - -+C, C + -+D) + g$name <- "Tiny graph" + E(g)$weight <- seq_len(ecount(g)) + + g2 <- as_undirected(g, mode = "collapse") + df2 <- as_data_frame(g2) + g3 <- as_undirected(g, mode = "each") + df3 <- as_data_frame(g3) + g4 <- as_undirected(g, mode = "mutual") + df4 <- as_data_frame(g4) + + expect_equal(g2$name, g$name) + expect_equal(g3$name, g$name) + expect_equal(g4$name, g$name) + + expect_equal(df2[order(df2[, 1], df2[, 2]), ]$weight, c(4, 2, 9)) + expect_equal(df3[order(df3[, 1], df3[, 2]), ]$weight, c(1, 3, 2, 4, 5)) + expect_equal(df4[order(df4[, 1], df4[, 2]), ]$weight, c(4, 9)) +}) diff --git a/tests/testthat/test-dyad.census.R b/tests/testthat/test-dyad.census.R index 7470f2206dc..828a50764f4 100644 --- a/tests/testthat/test-dyad.census.R +++ b/tests/testthat/test-dyad.census.R @@ -21,7 +21,7 @@ test_that("dyad_census works with celegansneural", { expect_equal(dc, list(mut = 197, asym = 1951, null = 41808)) expect_equal(sum(which_mutual(ce)), dc$mut * 2) expect_equal( - ecount(as.undirected(ce, mode = "collapse")) - dc$mut, + ecount(as_undirected(ce, mode = "collapse")) - dc$mut, dc$asym ) expect_equal(sum(unlist(dc)), vcount(ce) * (vcount(ce) - 1) / 2) diff --git a/tests/testthat/test-trees.R b/tests/testthat/test-trees.R index 9739b2f27cc..0d428c8707e 100644 --- a/tests/testthat/test-trees.R +++ b/tests/testthat/test-trees.R @@ -78,13 +78,13 @@ test_that("to_prufer and make_from_prufer works for trees", { g <- make_tree(13, 3, mode = "out") seq <- to_prufer(g) g2 <- make_from_prufer(seq) - g3 <- as.undirected(g) + g3 <- as_undirected(g) expect_true(isomorphic(g2, g3)) g <- make_tree(13, 3, mode = "in") seq <- to_prufer(g) g2 <- make_from_prufer(seq) - g3 <- as.undirected(g) + g3 <- as_undirected(g) expect_true(isomorphic(g2, g3)) })