Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes in read_nodelist, read_edgelist and as_network methods #176

Merged
merged 8 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: migraph
Title: Multimodal and Multilevel Network Analysis
Version: 0.8.10
Date: 2021-12-08
Version: 0.8.11
Date: 2021-12-10
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)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# migraph 0.8.11

## Import and export
- Fixed #172 by removing redundant header argument in `read_nodelist()` and
`read_edgelist()`

## Package
- Fixed #173 by extending `as_network()` method to convert correctly form
an `{igraph}` to a `{network}` object.
- Removed `ggraphgrid()` documentation

# migraph 0.8.10

## Import and export
Expand Down
10 changes: 7 additions & 3 deletions R/as.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,16 @@ as_network.network <- function(object) {

#' @export
as_network.matrix <- function(object) {
# Convert to adjacency matrix
out <- to_multilevel(object)
# Convert to adjacency matrix if not square already
if (nrow(object) != ncol(object)) {
out <- to_multilevel(object)
} else out <- object
network::as.network(out,
directed = is_directed(object),
bipartite = ifelse(is_twomode(object),
nrow(object),
FALSE),
loops = ifelse(sum(diag(out)) > 0, TRUE, FALSE),
ignore.eval = ifelse(is_weighted(object),
FALSE, TRUE),
names.eval = ifelse(is_weighted(object),
Expand All @@ -365,7 +368,8 @@ as_network.matrix <- function(object) {
as_network.igraph <- function(object) {
name <- type <- NULL
attr <- as.data.frame(igraph::get.vertex.attribute(object))
attr <- subset(attr, select = c(-name, -type))
if ("name" %in% colnames(attr)) attr <- subset(attr, select = c(-name))
if ("type" %in% colnames(attr)) attr <- subset(attr, select = c(-type))
out <- as_network(as_matrix(object, weight = is_weighted(object)))
if (length(attr) > 0) {
out <- network::set.vertex.attribute(out, names(attr), attr)
Expand Down
1 change: 1 addition & 0 deletions R/ggraphgrid.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Plot graph to grid
#'
#' For quick and easy graphing of networks to a grid plot
#' @noRd
#' @details The function uses approximate pattern matching
#' to redistributes the coarse layouts on the square grid points, while
#' preserving the topological relationships among the nodes (see Inoue et al. 2012).
Expand Down
5 changes: 3 additions & 2 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ read_edgelist <- function(file = file.choose(),
out <- read.csv2(file, header = TRUE, ...) # For EU
}
} else if (grepl("xlsx$|xls$", file)) {
out <- readxl::read_excel(file, header = TRUE, ...)
out <- readxl::read_excel(file, ...)
}
out
}
Expand Down Expand Up @@ -103,6 +103,7 @@ write_edgelist <- function(object,
#' @export
read_nodelist <- function(file = file.choose(),
sv = c("comma", "semi-colon"),

...){
sv <- match.arg(sv)
if (grepl("csv$", file)) {
Expand All @@ -112,7 +113,7 @@ read_nodelist <- function(file = file.choose(),
out <- read.csv2(file, header = TRUE, ...) # For EU
}
} else if (grepl("xlsx$|xls$", file)) {
out <- readxl::read_excel(file, header = TRUE, ...)
out <- readxl::read_excel(file, ...)
}
out
}
Expand Down
29 changes: 0 additions & 29 deletions man/ggraphgrid.Rd

This file was deleted.

22 changes: 21 additions & 1 deletion tests/testthat/test-grab.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ net <- as_tidygraph(data.frame(from = c("A", "B", "C", "D","E"),

net2 <- as_tidygraph(data.frame(from = c("A", "B", "C", "D","E"),
to = c("B", "C", "D", "E", "A"))) %>%
dplyr::mutate(friends = c("yes", "yes", "no", "no", "yes"))
dplyr::mutate(friends = c("yes", "yes", "no", "no", "yes")) %>%
igraph::set_edge_attr("weight", value = 1:5)

net3 <- as_matrix(data.frame(from = c("A", "A", "B", "C", "D", "D", "E", "E"),
to = c("B", "G", "C", "D", "E", "G", "A", "H")))

name <- c("Lisa", "John", "Lily", "Ben", "Adam")

friends <- c("yes", "yes", "no", "no", "yes")

test_that("node_names works", {
expect_equal(node_names(net), name)
expect_length(node_names(net), 5)
})

test_that("node_attribute works", {
expect_equal(node_attribute(net2, "friends"), friends)
expect_length(node_attribute(net2, "friends"), igraph::vcount(net2))
})

test_that("edge_attribute works", {
expect_equal(edge_attribute(net2, "weight"), c(1, 2, 3, 4, 5))
})

test_that("edge_weights works", {
expect_equal(edge_weights(net2), edge_attribute(net2, "weight"))
})

test_that("graph_nodes works", {
Expand All @@ -25,3 +39,9 @@ test_that("graph_nodes works", {
test_that("graph_edges works", {
expect_false(graph_edges(net)==10)
})

test_that("graph_dims works", {
expect_equal(graph_dims(net3), c(5, 7))
expect_length(graph_dims(net3), 2)
expect_false(length(graph_dims(net3)) == 1)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-to.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ test_that("to_undirected works",{
((as_matrix(ison_coleman) + t(as_matrix(ison_coleman))) > 0) * 1)
expect_equal(to_undirected(as_matrix(southern_women)),
as_matrix(southern_women))
expect_equal(unname(as_matrix(to_undirected(as_network(ison_coleman)))[1, ]),
c(rep(0,9),1,rep(0,6)))
expect_equal(as_matrix(to_undirected(as_network(ison_coleman))),
as_matrix(to_undirected(ison_coleman)))
})

test_that("to_onemode works",{
Expand Down