Skip to content

Commit

Permalink
Merge pull request #86 from snlab-ch/develop
Browse files Browse the repository at this point in the history
Added as_network and preparation for CRAN submission
  • Loading branch information
jhollway committed Apr 11, 2021
2 parents ee8dbc6 + d16b313 commit b69dc38
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 17 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^data-raw$
^docs$
^pkgdown$
^cran-comments\.md$
29 changes: 19 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
Package: migraph
Title: Multimodal and multilevel network analysis
Version: 0.6.0
Date: 2021-03-03
Description: This package assembles a number of utility functions for
loading, visualising, and analysing multimode, multiplex, and multilevel networks.
URL: https://github.com/jhollway/migraph
BugReports: https://github.com/jhollway/migraph/issues
Title: Multimodal and Multilevel Network Analysis
Version: 0.6.1
Date: 2021-04-11
Description: A set of tools that extend common social network analysis packages
for analysing multimodal and multilevel networks.
It includes functions for one- and two-mode (and sometimes three-mode)
centrality, centralization, clustering, and constraint,
as well as for one- and two-mode network regression and block-modelling.
All functions operate with matrices, edge lists,
and 'igraph', 'network'/'sna', and 'tidygraph' objects.
The package is released as a complement to
'Multimodal Political Networks' (2021, ISBN:9781108985000),
and includes various datasets used in the book.
URL: https://github.com/snlab-ch/migraph
BugReports: https://github.com/snlab-ch/migraph/issues
Depends: R (>= 4.0.0)
License: MIT + file LICENSE
Language: en-GB
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Expand All @@ -22,7 +31,8 @@ Imports:
dplyr,
purrr,
tibble,
tidyr
tidyr,
network
Suggests:
testthat,
roxygen2,
Expand All @@ -32,8 +42,7 @@ Suggests:
Authors@R:
c(person(given = "James",
family = "Hollway",
role = c("cre", "aut", "ctb"),
role = c("cph", "cre", "aut", "ctb"),
email = "james.hollway@graduateinstitute.ch",
comment = c("IHEID", ORCID = "0000-0002-8361-9647")))
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export("%>%")
export(.E)
export(.G)
export(.N)
export(as.network)
export(as_igraph)
export(as_matrix)
export(as_network)
export(as_tidygraph)
export(blockmodel_concor)
export(create_complete)
Expand All @@ -22,6 +24,7 @@ export(graph_closeness)
export(graph_clustering)
export(graph_degree)
export(is.igraph)
export(is.network)
export(is.tbl_graph)
export(is_bipartite)
export(is_twomode)
Expand Down Expand Up @@ -62,6 +65,10 @@ importFrom(igraph,is.igraph)
importFrom(igraph,is_bipartite)
importFrom(igraph,mean_distance)
importFrom(magrittr,"%>%")
importFrom(network,as.matrix.network.adjacency)
importFrom(network,as.matrix.network.incidence)
importFrom(network,as.network)
importFrom(network,is.network)
importFrom(purrr,map)
importFrom(rlang,.data)
importFrom(rlang,enquo)
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# migraph 0.6.1

2021-04-11

## Package

* Closed #21 by elaborating DESCRIPTION file in preparation for CRAN submission
* Updated several old URLs in documentation

## Classes

* Closed #85 by adding `as_network()` to coerce objects into network class
* Modified other coercion functions to also work with network class objects

# migraph 0.6.0

2021-03-03
Expand Down
50 changes: 50 additions & 0 deletions R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ as_matrix <- function(object){
} else {
mat <- igraph::as_adjacency_matrix(object, sparse = FALSE)
}
} else if (is.network(object)) {
if(network::is.bipartite(object)){
mat <- network::as.matrix.network.incidence(object)
} else {
mat <- network::as.matrix.network.adjacency(object)
}
} else if (is.matrix(object)) {
mat <- object
} else if (is.data.frame(object)){
Expand All @@ -75,6 +81,7 @@ as_matrix <- function(object){
}

#' @rdname convert
#' @importFrom network as.matrix.network.incidence as.matrix.network.adjacency
#' @return An igraph graph object.
#' @examples
#' test <- data.frame(id1 = c("A","B","B","C","C"),
Expand All @@ -90,6 +97,14 @@ as_igraph <- function(object, twomode = FALSE){
weights <- rlang::eval_tidy(weights, .E())
} else if (is.igraph(object)) {
graph <- object
} else if (is.network(object)) {
if(network::is.bipartite(object)){
graph <- network::as.matrix.network.incidence(object)
graph <- igraph::graph_from_incidence_matrix(graph)
} else {
graph <- network::as.matrix.network.adjacency(object)
graph <- igraph::graph_from_adjacency_matrix(graph)
}
} else if (is.data.frame(object) | is.matrix(object)) {
if (is.data.frame(object)) object <- as_matrix(object)
if(nrow(object)!=ncol(object) | twomode){
Expand All @@ -115,6 +130,16 @@ as_tidygraph <- function(object, twomode = FALSE){
tidy <- object
} else if (is.igraph(object)) {
tidy <- tidygraph::as_tbl_graph(object)
} else if (is.network(object)) {
if(network::is.bipartite(object)){
tidy <- network::as.matrix.network.incidence(object)
tidy <- igraph::graph_from_incidence_matrix(tidy)
tidy <- tidygraph::as_tbl_graph(tidy)
} else {
tidy <- network::as.matrix.network.adjacency(object)
tidy <- igraph::graph_from_adjacency_matrix(tidy)
tidy <- tidygraph::as_tbl_graph(tidy)
}
} else if (is.data.frame(object) | is.matrix(object)) {
if (is.data.frame(object)) object <- as_matrix(object)
if(nrow(object)!=ncol(object) | twomode){
Expand All @@ -126,6 +151,31 @@ as_tidygraph <- function(object, twomode = FALSE){
tidy
}

#' @rdname convert
#' @importFrom network is.network as.network
#' @return A sna/network network class object
#' @examples
#' test <- data.frame(id1 = c("A","B","B","C","C"),
#' id2 = c("I","G","I","G","H"))
#' as_network(test)
#' @export
as_network <- function(object){

if(!network::is.network(object)){
if(is_twomode(object)){
net <- as_matrix(object)
net <- network::as.network(net, bipartite = TRUE)
} else {
net <- as_matrix(object)
net <- network::as.network(net, bipartite = FALSE)
}
} else {
net <- object
}
net

}

#' @rdname convert
#' @importFrom igraph is.bipartite
#' @return A logical vector whether object is two-mode (TRUE) or not (FALSE)
Expand Down
2 changes: 1 addition & 1 deletion R/data_elite_usa_advice.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @usage data(mpn_elite_usa_advice)
#' @format Matrix with 14 rows and 20 columns
#' @references
#' Domhoff, G William. 2016. \href{http://www2.ucsc.edu/whorulesamerica/power_elite/}{“Who Rules America? Power Elite Database.”}
#' Domhoff, G William. 2016. \href{https://whorulesamerica.ucsc.edu/power_elite/}{“Who Rules America? Power Elite Database.”}
#'
#' Knoke, David, Mario Diani, James Hollway, and Dimitris C Christopoulos. 2021.
#' \href{https://www.cambridge.org/core/books/multimodal-political-networks/43EE8C192A1B0DCD65B4D9B9A7842128}{\emph{Multimodal Political Networks}}.
Expand Down
2 changes: 1 addition & 1 deletion R/data_elite_usa_money.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @usage data(mpn_elite_usa_money)
#' @format Matrix with 26 rows and 6+6 columns
#' @references
#' Domhoff, G William. 2016. \href{http://www2.ucsc.edu/whorulesamerica/power_elite/}{“Who Rules America? Power Elite Database.”}
#' Domhoff, G William. 2016. \href{https://whorulesamerica.ucsc.edu/power_elite/}{“Who Rules America? Power Elite Database.”}
#'
#' The Center for Responsive Politics. 2019. \href{http://www.opensecrets.org}{“OpenSecrets.”}
#'
Expand Down
7 changes: 7 additions & 0 deletions R/reexports_network.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @importFrom network is.network
#' @export
network::is.network

#' @importFrom network as.network
#' @export
network::as.network
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ Cambridge University Press: Cambridge.

The package is offered as a complement to existing R packages for network analysis.
It can analyse data in base formats such as matrices and (data frame) edgelists,
but also leverages [`{igraph}`](https://igraph.org/r/) and is consistent with a [`{tidygraph}`](https://tidygraph.data-imaginist.com/index.html) workflow.
but can also work with [`{igraph}`](https://igraph.org/r/) and [`{network}`](http://statnet.org) objects,
and is consistent with a [`{tidygraph}`](https://tidygraph.data-imaginist.com/index.html) workflow.

Please explore [the website](https://jhollway.github.io/migraph/) to find out more.
Some of these functions are also relied on for the [Topological Typology](https://jhollway.shinyapps.io/TopoTypo/) app.
Please explore [the website](https://snlab-ch.github.io/migraph/) to find out more.

## Installation

### From binary

Perhaps the easiest way to install `{migraph}` is by installing a compiled binary.
Binaries for all major OSes -- Windows, Mac, and Linux --
can be found by clicking on the latest release [here](https://github.com/snlab-nl/rsiena/releases/latest).
can be found by clicking on the latest release [here](https://github.com/snlab-ch/migraph/releases/latest).
Download the appropriate binary for your operating system,
and install using an adapted version of the following commands:

Expand All @@ -55,7 +55,7 @@ please install the `{remotes}` package from CRAN and then enter into the console
It draws together, updates, and builds upon many functions currently available in
other excellent R packages such as
[`{bipartite}`](https://github.com/biometry/bipartite),
[`{multinet}`](https://cran.r-project.org/web/packages/multinet/multinet.pdf),
[`{multinet}`](https://CRAN.R-project.org/package=multinet),
[`{netmem}`](https://github.com/anespinosa/netmem),
and [`{tnet}`](https://toreopsahl.com/tnet/),
and implements many additional features currently only available outside the R ecosystem
Expand Down
14 changes: 14 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Test environments

* local R installation, R 4.0.3
* Ubuntu Linux 20.04.1 LTS, R-release, GCC (on rhub)
* Fedora Linux, R-devel, clang, gfortran (on rhub)
* Mac OS X 10.15.7 (on Github), R 4.0.4
* Microsoft Windows Server 2019 10.0.17763 (on Github), R 4.0.4
* Ubuntu 20.04.2 (on Github), R 4.0.4

## R CMD check results

0 errors | 0 warnings | 0 notes

* This is a new release.
13 changes: 13 additions & 0 deletions tests/testthat/test-convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,16 @@ test_that("as_matrix converts correctly",{
expect_vector(mpn_elite_mex %>% as_matrix())
expect_vector(mpn_elite_usa_advice %>% as_matrix())
})

test_that("as_tidygraph converts correctly",{
expect_s3_class(as_tidygraph(mat1), "tbl_graph")
expect_s3_class(as_tidygraph(southern_women), "tbl_graph")
expect_s3_class(as_tidygraph(mpn_elite_usa_money), "tbl_graph")
})

test_that("as_network converts correctly",{
expect_s3_class(as_network(mat1), "network")
expect_s3_class(as_network(southern_women), "network")
expect_s3_class(as_network(mpn_elite_usa_money), "network")
})

0 comments on commit b69dc38

Please sign in to comment.