Skip to content

Commit b69dc38

Browse files
authored
Merge pull request #86 from snlab-ch/develop
Added as_network and preparation for CRAN submission
2 parents ee8dbc6 + d16b313 commit b69dc38

11 files changed

+132
-17
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
^data-raw$
88
^docs$
99
^pkgdown$
10+
^cran-comments\.md$

DESCRIPTION

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
Package: migraph
2-
Title: Multimodal and multilevel network analysis
3-
Version: 0.6.0
4-
Date: 2021-03-03
5-
Description: This package assembles a number of utility functions for
6-
loading, visualising, and analysing multimode, multiplex, and multilevel networks.
7-
URL: https://github.com/jhollway/migraph
8-
BugReports: https://github.com/jhollway/migraph/issues
2+
Title: Multimodal and Multilevel Network Analysis
3+
Version: 0.6.1
4+
Date: 2021-04-11
5+
Description: A set of tools that extend common social network analysis packages
6+
for analysing multimodal and multilevel networks.
7+
It includes functions for one- and two-mode (and sometimes three-mode)
8+
centrality, centralization, clustering, and constraint,
9+
as well as for one- and two-mode network regression and block-modelling.
10+
All functions operate with matrices, edge lists,
11+
and 'igraph', 'network'/'sna', and 'tidygraph' objects.
12+
The package is released as a complement to
13+
'Multimodal Political Networks' (2021, ISBN:9781108985000),
14+
and includes various datasets used in the book.
15+
URL: https://github.com/snlab-ch/migraph
16+
BugReports: https://github.com/snlab-ch/migraph/issues
917
Depends: R (>= 4.0.0)
1018
License: MIT + file LICENSE
19+
Language: en-GB
1120
Encoding: UTF-8
1221
LazyData: true
1322
RoxygenNote: 7.1.1
@@ -22,7 +31,8 @@ Imports:
2231
dplyr,
2332
purrr,
2433
tibble,
25-
tidyr
34+
tidyr,
35+
network
2636
Suggests:
2737
testthat,
2838
roxygen2,
@@ -32,8 +42,7 @@ Suggests:
3242
Authors@R:
3343
c(person(given = "James",
3444
family = "Hollway",
35-
role = c("cre", "aut", "ctb"),
45+
role = c("cph", "cre", "aut", "ctb"),
3646
email = "james.hollway@graduateinstitute.ch",
3747
comment = c("IHEID", ORCID = "0000-0002-8361-9647")))
38-
VignetteBuilder: knitr
3948
Roxygen: list(markdown = TRUE)

NAMESPACE

+7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export("%>%")
99
export(.E)
1010
export(.G)
1111
export(.N)
12+
export(as.network)
1213
export(as_igraph)
1314
export(as_matrix)
15+
export(as_network)
1416
export(as_tidygraph)
1517
export(blockmodel_concor)
1618
export(create_complete)
@@ -22,6 +24,7 @@ export(graph_closeness)
2224
export(graph_clustering)
2325
export(graph_degree)
2426
export(is.igraph)
27+
export(is.network)
2528
export(is.tbl_graph)
2629
export(is_bipartite)
2730
export(is_twomode)
@@ -62,6 +65,10 @@ importFrom(igraph,is.igraph)
6265
importFrom(igraph,is_bipartite)
6366
importFrom(igraph,mean_distance)
6467
importFrom(magrittr,"%>%")
68+
importFrom(network,as.matrix.network.adjacency)
69+
importFrom(network,as.matrix.network.incidence)
70+
importFrom(network,as.network)
71+
importFrom(network,is.network)
6572
importFrom(purrr,map)
6673
importFrom(rlang,.data)
6774
importFrom(rlang,enquo)

NEWS.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# migraph 0.6.1
2+
3+
2021-04-11
4+
5+
## Package
6+
7+
* Closed #21 by elaborating DESCRIPTION file in preparation for CRAN submission
8+
* Updated several old URLs in documentation
9+
10+
## Classes
11+
12+
* Closed #85 by adding `as_network()` to coerce objects into network class
13+
* Modified other coercion functions to also work with network class objects
14+
115
# migraph 0.6.0
216

317
2021-03-03

R/convert.R

+50
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ as_matrix <- function(object){
4949
} else {
5050
mat <- igraph::as_adjacency_matrix(object, sparse = FALSE)
5151
}
52+
} else if (is.network(object)) {
53+
if(network::is.bipartite(object)){
54+
mat <- network::as.matrix.network.incidence(object)
55+
} else {
56+
mat <- network::as.matrix.network.adjacency(object)
57+
}
5258
} else if (is.matrix(object)) {
5359
mat <- object
5460
} else if (is.data.frame(object)){
@@ -75,6 +81,7 @@ as_matrix <- function(object){
7581
}
7682

7783
#' @rdname convert
84+
#' @importFrom network as.matrix.network.incidence as.matrix.network.adjacency
7885
#' @return An igraph graph object.
7986
#' @examples
8087
#' test <- data.frame(id1 = c("A","B","B","C","C"),
@@ -90,6 +97,14 @@ as_igraph <- function(object, twomode = FALSE){
9097
weights <- rlang::eval_tidy(weights, .E())
9198
} else if (is.igraph(object)) {
9299
graph <- object
100+
} else if (is.network(object)) {
101+
if(network::is.bipartite(object)){
102+
graph <- network::as.matrix.network.incidence(object)
103+
graph <- igraph::graph_from_incidence_matrix(graph)
104+
} else {
105+
graph <- network::as.matrix.network.adjacency(object)
106+
graph <- igraph::graph_from_adjacency_matrix(graph)
107+
}
93108
} else if (is.data.frame(object) | is.matrix(object)) {
94109
if (is.data.frame(object)) object <- as_matrix(object)
95110
if(nrow(object)!=ncol(object) | twomode){
@@ -115,6 +130,16 @@ as_tidygraph <- function(object, twomode = FALSE){
115130
tidy <- object
116131
} else if (is.igraph(object)) {
117132
tidy <- tidygraph::as_tbl_graph(object)
133+
} else if (is.network(object)) {
134+
if(network::is.bipartite(object)){
135+
tidy <- network::as.matrix.network.incidence(object)
136+
tidy <- igraph::graph_from_incidence_matrix(tidy)
137+
tidy <- tidygraph::as_tbl_graph(tidy)
138+
} else {
139+
tidy <- network::as.matrix.network.adjacency(object)
140+
tidy <- igraph::graph_from_adjacency_matrix(tidy)
141+
tidy <- tidygraph::as_tbl_graph(tidy)
142+
}
118143
} else if (is.data.frame(object) | is.matrix(object)) {
119144
if (is.data.frame(object)) object <- as_matrix(object)
120145
if(nrow(object)!=ncol(object) | twomode){
@@ -126,6 +151,31 @@ as_tidygraph <- function(object, twomode = FALSE){
126151
tidy
127152
}
128153

154+
#' @rdname convert
155+
#' @importFrom network is.network as.network
156+
#' @return A sna/network network class object
157+
#' @examples
158+
#' test <- data.frame(id1 = c("A","B","B","C","C"),
159+
#' id2 = c("I","G","I","G","H"))
160+
#' as_network(test)
161+
#' @export
162+
as_network <- function(object){
163+
164+
if(!network::is.network(object)){
165+
if(is_twomode(object)){
166+
net <- as_matrix(object)
167+
net <- network::as.network(net, bipartite = TRUE)
168+
} else {
169+
net <- as_matrix(object)
170+
net <- network::as.network(net, bipartite = FALSE)
171+
}
172+
} else {
173+
net <- object
174+
}
175+
net
176+
177+
}
178+
129179
#' @rdname convert
130180
#' @importFrom igraph is.bipartite
131181
#' @return A logical vector whether object is two-mode (TRUE) or not (FALSE)

R/data_elite_usa_advice.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @usage data(mpn_elite_usa_advice)
1414
#' @format Matrix with 14 rows and 20 columns
1515
#' @references
16-
#' Domhoff, G William. 2016. \href{http://www2.ucsc.edu/whorulesamerica/power_elite/}{“Who Rules America? Power Elite Database.”}
16+
#' Domhoff, G William. 2016. \href{https://whorulesamerica.ucsc.edu/power_elite/}{“Who Rules America? Power Elite Database.”}
1717
#'
1818
#' Knoke, David, Mario Diani, James Hollway, and Dimitris C Christopoulos. 2021.
1919
#' \href{https://www.cambridge.org/core/books/multimodal-political-networks/43EE8C192A1B0DCD65B4D9B9A7842128}{\emph{Multimodal Political Networks}}.

R/data_elite_usa_money.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @usage data(mpn_elite_usa_money)
1414
#' @format Matrix with 26 rows and 6+6 columns
1515
#' @references
16-
#' Domhoff, G William. 2016. \href{http://www2.ucsc.edu/whorulesamerica/power_elite/}{“Who Rules America? Power Elite Database.”}
16+
#' Domhoff, G William. 2016. \href{https://whorulesamerica.ucsc.edu/power_elite/}{“Who Rules America? Power Elite Database.”}
1717
#'
1818
#' The Center for Responsive Politics. 2019. \href{http://www.opensecrets.org}{“OpenSecrets.”}
1919
#'

R/reexports_network.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#' @importFrom network is.network
2+
#' @export
3+
network::is.network
4+
5+
#' @importFrom network as.network
6+
#' @export
7+
network::as.network

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ Cambridge University Press: Cambridge.
2323

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

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

3131
## Installation
3232

3333
### From binary
3434

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

@@ -55,7 +55,7 @@ please install the `{remotes}` package from CRAN and then enter into the console
5555
It draws together, updates, and builds upon many functions currently available in
5656
other excellent R packages such as
5757
[`{bipartite}`](https://github.com/biometry/bipartite),
58-
[`{multinet}`](https://cran.r-project.org/web/packages/multinet/multinet.pdf),
58+
[`{multinet}`](https://CRAN.R-project.org/package=multinet),
5959
[`{netmem}`](https://github.com/anespinosa/netmem),
6060
and [`{tnet}`](https://toreopsahl.com/tnet/),
6161
and implements many additional features currently only available outside the R ecosystem

cran-comments.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Test environments
2+
3+
* local R installation, R 4.0.3
4+
* Ubuntu Linux 20.04.1 LTS, R-release, GCC (on rhub)
5+
* Fedora Linux, R-devel, clang, gfortran (on rhub)
6+
* Mac OS X 10.15.7 (on Github), R 4.0.4
7+
* Microsoft Windows Server 2019 10.0.17763 (on Github), R 4.0.4
8+
* Ubuntu 20.04.2 (on Github), R 4.0.4
9+
10+
## R CMD check results
11+
12+
0 errors | 0 warnings | 0 notes
13+
14+
* This is a new release.

tests/testthat/test-convert.R

+13
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@ test_that("as_matrix converts correctly",{
2727
expect_vector(mpn_elite_mex %>% as_matrix())
2828
expect_vector(mpn_elite_usa_advice %>% as_matrix())
2929
})
30+
31+
test_that("as_tidygraph converts correctly",{
32+
expect_s3_class(as_tidygraph(mat1), "tbl_graph")
33+
expect_s3_class(as_tidygraph(southern_women), "tbl_graph")
34+
expect_s3_class(as_tidygraph(mpn_elite_usa_money), "tbl_graph")
35+
})
36+
37+
test_that("as_network converts correctly",{
38+
expect_s3_class(as_network(mat1), "network")
39+
expect_s3_class(as_network(southern_women), "network")
40+
expect_s3_class(as_network(mpn_elite_usa_money), "network")
41+
})
42+

0 commit comments

Comments
 (0)