Commit 165f14c7 authored by houyun's avatar houyun
Browse files

improve as.igraph

parent f8108571
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -191,7 +191,6 @@ importFrom(gtable,gtable_row)
importFrom(gtable,gtable_width)
importFrom(igraph,V)
importFrom(igraph,as.igraph)
importFrom(igraph,graph_from_data_frame)
importFrom(igraph,vertex_attr)
importFrom(magrittr,"%>%")
importFrom(purrr,map_dbl)
+42 −10
Original line number Diff line number Diff line
#' @title Coerce to a Graph
#' @description Functions to coerce a object to graph if possible.
#' @param x any \code{R} object.
#' @param use_adjacency logical, if TRUE will build graph from adjacency matrix.
#' @param diag logical, if TRUE will keep the diagonal of adjacency matrix data.
#' @param simplify if TRUE, Simple graphs are graphs which do not contain loop
#' and multiple edges.
#' @param directed whether or not to create a directed graph.
#' @param ... other parameters.
#' @return  a graph object.
#' @importFrom igraph graph_from_data_frame
#' @importFrom igraph as.igraph
#' @importFrom tidygraph as_tbl_graph
#' @author Hou Yun
#' @rdname network
as.igraph.cor_md_tbl <- function(x,
                                 ...,
                                 use_adjacency = FALSE,
                                 diag = FALSE,
                                 simplify = TRUE,
                                 directed = FALSE) {
  rnm <- row_names(x)
  cnm <- col_names(x)
  x <- dplyr::filter(x, ...)
  params <- list(...)
  if (is.null(params)) {
    gparams <- list()
  } else {
    gparams <- params[names(params) != ""]
    params <- unname(params[names(params) == ""])
  }
  x <- do.call(dplyr::filter, c(list(.data = x), params))

  if (isFALSE(use_adjacency)) {
    if(isTRUE(simplify)) {
      if(identical(rnm, cnm) && isFALSE(directed)) {
      x <- extract_upper(x, FALSE)
        x <- extract_upper(x, diag)
      }
      nodes <- unique(c(x$.rownames, x$.colnames))
    } else {
      nodes <- unique(c(rnm, cnm))
    }
  graph_from_data_frame(x, directed = directed, vertices = nodes)
    igraph::graph_from_data_frame(x, directed = directed, vertices = nodes)
  } else {
    if(isTRUE(simplify)) {
      if(identical(rnm, cnm) && isFALSE(directed)) {
        x <- extract_upper(x, diag)
      }
    }

    if (isTRUE(directed)) {
      mode <- "directed"
    } else {
      if (isTRUE(simplify)) {
        mode <- "max"
      } else {
        mode <- "undirected"
      }
    }

    do.call(igraph::graph_from_adjacency_matrix,
            c(list(adjmatrix = adjacency_matrix(x),
                   mode = mode, diag = diag, weighted = TRUE), gparams))
  }
}

#' @rdname network
@@ -104,7 +136,7 @@ adjacency_matrix.cor_md_tbl <- function(x, ...) {

#' @method adjacency_matrix default
adjacency_matrix.default <- function(x, ...) {
  adjacency_matrix(as_md_tbl(x), ...)
  adjacency_matrix(as_md_tbl(x, ...))
}

#' @noRd
+12 −1
Original line number Diff line number Diff line
@@ -15,7 +15,14 @@
\alias{as_tbl_graph.corr.test}
\title{Coerce to a Graph}
\usage{
\method{as.igraph}{cor_md_tbl}(x, ..., simplify = TRUE, directed = FALSE)
\method{as.igraph}{cor_md_tbl}(
  x,
  ...,
  use_adjacency = FALSE,
  diag = FALSE,
  simplify = TRUE,
  directed = FALSE
)

\method{as.igraph}{correlate}(x, ...)

@@ -44,6 +51,10 @@

\item{...}{other parameters.}

\item{use_adjacency}{logical, if TRUE will build graph from adjacency matrix.}

\item{diag}{logical, if TRUE will keep the diagonal of adjacency matrix data.}

\item{simplify}{if TRUE, Simple graphs are graphs which do not contain loop
and multiple edges.}