Skip to content

Commit

Permalink
mg_to_igraph is now igraph() 🎨
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Sep 1, 2019
1 parent 46e8a71 commit 1d47483
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 77 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ URL: https://mangal.io
BugReports: https://github.com/mangal-wg/rmangal/issues
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports:
httr (>= 1.3.1),
igraph,
jsonlite (>= 1.5),
memoise,
purrr,
sf
RoxygenNote: 6.1.1
Suggests:
ggraph,
knitr,
magrittr,
mapview,
rmarkdown,
taxize,
tidygraph,
testthat,
tibble,
USAboundaries
RoxygenNote: 6.1.1
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(as.igraph,mgNetwork)
S3method(as.igraph,mgNetworksCollection)
S3method(get_citation,mgNetwork)
S3method(get_citation,mgNetworksCollection)
S3method(get_collection,default)
Expand All @@ -9,21 +11,20 @@ S3method(get_collection,mgSearchNetworks)
S3method(get_collection,mgSearchNodes)
S3method(get_collection,mgSearchReference)
S3method(get_collection,mgSearchTaxonomy)
S3method(mg_to_igraph,mgNetwork)
S3method(mg_to_igraph,mgNetworksCollection)
S3method(print,mgNetwork)
S3method(print,mgNetworksCollection)
export(as.igraph)
export(avail_type)
export(clear_cache_rmangal)
export(combine_mgNetworks)
export(get_citation)
export(get_collection)
export(get_network_by_id)
export(get_network_by_id_indiv)
export(mg_to_igraph)
export(search_datasets)
export(search_interactions)
export(search_networks)
export(search_nodes)
export(search_reference)
export(search_taxonomy)
importFrom(igraph,as.igraph)
33 changes: 10 additions & 23 deletions R/mg_to_igraph.R → R/as.igraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,10 @@
#' An object of class `igraph` for a `mgNetwork` object and a list of
#' `igraph` objects for `mgNetworksCollection`.
#'
#'
#' @examples
#' insects_networks <- get_collection(search_networks(query='insect%'))
#' # Apply mg_to_igraph on one specific network
#' insects_network <- insects_networks[[1]]
#' ig_network <- mg_to_igraph(insects_network)
#' # Apply mg_to_igraph on networks collection
#' ig_coll_networks <- mg_to_igraph(insects_networks)
#' # Plot igraph object with vertex label
#' plot(ig_network, vertex.label = insects_network$nodes$taxonomy.name)
#' @export

mg_to_igraph <- function(x, ...) {
UseMethod("mg_to_igraph", x)
}


#' @describeIn mg_to_igraph Convert`mgNetwork` objects to `igraph` objects.
#' @export
mg_to_igraph.mgNetwork <- function(x, ...) {
#' @importFrom igraph as.igraph
#' @describeIn as.igraph Convert `mgNetwork` objects to `igraph` objects.
#' @export
as.igraph.mgNetwork <- function(x, ...) {
# Simple test to know if the graph is directed or undirected
directed <- ifelse(all(x$edges$direction == "directed"), TRUE, FALSE)
# Move id edge to the last column
Expand All @@ -40,8 +24,11 @@ mg_to_igraph.mgNetwork <- function(x, ...) {
}


#' @describeIn mg_to_igraph Convert `mgNetworksCollection` objects to `igraph` objects.
#' @describeIn as.igraph Convert `mgNetworksCollection` objects to a list of `igraph` objects.
#' @export
mg_to_igraph.mgNetworksCollection <- function(x, ...) {
lapply(x, mg_to_igraph.mgNetwork)
as.igraph.mgNetworksCollection <- function(x, ...) {
lapply(x, as.igraph.mgNetwork)
}

#' @export
igraph::as.igraph
2 changes: 1 addition & 1 deletion R/combine_mgNetworks.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Combine Mangal networks.
#' Combine Mangal networks
#'
#' Combine `mgNetworksCollection` and `mgNetwork` objects into a
#' `mgNetworksCollection` object.
Expand Down
30 changes: 30 additions & 0 deletions man/as.igraph.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/combine_mgNetworks.Rd

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

43 changes: 0 additions & 43 deletions man/mg_to_igraph.Rd

This file was deleted.

16 changes: 16 additions & 0 deletions man/reexports.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/helper-rmangal.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cl_df <- c("tbl_df", "tbl", "data.frame")
cl_df <- c("tbl_df", "tbl", "data.frame")
nm_co <- c("network", "nodes", "edges", "dataset", "reference")
4 changes: 2 additions & 2 deletions tests/testthat/test-as.igraph.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
context("mg_to_igraph")

# Prep test on mgNetwork
net_100 <- mg_to_igraph(get_network_by_id(100))
net_collection <- mg_to_igraph(get_collection(search_datasets("closs")))
net_100 <- as.igraph(get_network_by_id(100))
net_collection <- as.igraph(get_collection(search_datasets("closs")))

test_that("expected behavior", {
expect_equal(length(net_collection), 12)
Expand Down
16 changes: 16 additions & 0 deletions vignettes/rmangal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## ----setup, include = FALSE----------------------------------------------
knitr::opts_chunk$set(
message = FALSE,
collapse = TRUE,
comment = "#>"
)
# library(sf)

## ----echo = FALSE--------------------------------------------------------
knitr::include_graphics("figs/data_structure.svg")

## ----load_packages-------------------------------------------------------
library(rmangal)
library(magrittr) # for the pip %>%
library(tibble) # to use tibbles, enhanced data frames

2 changes: 1 addition & 1 deletion vignettes/rmangal.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ website](https://igraph.org/r/)) object and then to carry out network analysis:

```{R}
library(igraph)
ig_lagoons <- search_datasets(query = 'lagoon') %>% get_collection %>% mg_to_igraph
ig_lagoons <- search_datasets(query = 'lagoon') %>% get_collection %>% as.igraph
## Modularity analysis for the first network
modularity(ig_lagoons[[1]], membership(cluster_walktrap(ig_lagoons[[1]])))
## Degree values for all networks
Expand Down

0 comments on commit 1d47483

Please sign in to comment.