intergraph/0000755000176200001440000000000014556752432012432 5ustar liggesusersintergraph/NAMESPACE0000644000176200001440000000121514545555372013652 0ustar liggesusers# Generated by roxygen2: do not edit by hand S3method(asDF,igraph) S3method(asDF,network) S3method(asIgraph,data.frame) S3method(asIgraph,network) S3method(asNetwork,data.frame) S3method(asNetwork,igraph) S3method(dumpAttr,igraph) S3method(dumpAttr,network) S3method(igDirected,igraph) S3method(igDirected,network) S3method(igEcount,igraph) S3method(igEcount,network) S3method(igVcount,igraph) S3method(igVcount,network) S3method(print,netcompare) S3method(print,netcomparea) export(asDF) export(asIgraph) export(asNetwork) export(attrmap) export(dumpAttr) export(netcompare) importFrom(network,add.edges.network) importFrom(network,as.matrix.network) intergraph/README.md0000644000176200001440000000237614470117115013705 0ustar liggesusers# `intergraph`: Coercion Routines for Network Data Objects [![R-CMD-check](https://github.com/mbojan/intergraph/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mbojan/intergraph/actions/workflows/R-CMD-check.yaml) [![](https://www.r-pkg.org/badges/version/intergraph)](https://www.r-pkg.org/pkg/intergraph) [![CRAN status](https://www.r-pkg.org/badges/version/intergraph)](https://CRAN.R-project.org/package=intergraph) [![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/intergraph)](https://www.r-pkg.org/pkg/intergraph) [![DOI](https://zenodo.org/badge/doi/10.5281/zenodo.19148.svg)](https://dx.doi.org/10.5281/zenodo.19148) [![Codecov test coverage](https://codecov.io/gh/mbojan/intergraph/branch/master/graph/badge.svg)](https://app.codecov.io/gh/mbojan/intergraph?branch=master) This is an R package implementing methods for converting network data objects between classes defined in other packages. Currently supported classes: network, igraph. See projects web page on [mbojan.github.io/intergraph](https://mbojan.github.io/intergraph/). # Install Install stable version from CRAN or development version with ```r remotes::install_github("mbojan/intergraph") ``` # License GPL-3. intergraph/data/0000755000176200001440000000000014451017164013331 5ustar liggesusersintergraph/data/exIgraph.rda0000644000176200001440000000065314451017753015600 0ustar liggesusersBZh91AY&SY){DUS?` @`@t`Ld04&C0L64h4ih d ˜F )LPmV6Aޭs&y+ )B@ 0 Q @, #*"%; UUUUUUUR !]xst b=HRN%w|ķs^yV@:W<2G(L1FJҤI눚]g^tro9WOL A\?.Rʔ͂lP.p!(\rintergraph/data/exIgraph2.rda0000644000176200001440000000066414451017753015664 0ustar liggesusersBZh91AY&SYg){DUS? @`@tjLiz&4z4&C0L(`&10&&$ɂHi= #i3YZw66\jLXk=%)ND[) ToekdJJZ,Hf^ܪwV5%HN P 0hBb0thB.ga)+FiskS34n{\#"˚o^#L#T@LDDr"B`$a!i u\j.QbaU*Q$L3f8'> AsGdR5P@@›zbSdt1t1mØdjw)7Ex_YnOs,x+b}-KFjn56KulwحuC3?%zkGZS,lkkE1ÅSM4)ZȄ„.ߒRY9읓;lUQlS:5bҮE?y*[U UEBP-P K*!!FQEHq$ UUUUUUUY_'IF{m~~J|d# iS)B># 'igraph' # example network as object of class "network" summary(exNetwork) # convert to class "igraph" g <- asIgraph(exNetwork) # check if 'exNetwork' and 'g' are the same # (dropping some aux attributes) all.equal( structure(as.matrix(exNetwork, "edgelist"), n=NULL, vnames=NULL), igraph::as_edgelist(g) ) # compare results using 'netcompare' netcompare(exNetwork, g) ### convert 'igraph' -> 'network' # example network as object of class "igraph" summary(exIgraph) # convert to class "network" gg <- asNetwork(exIgraph) # check if they are the same # (dropping some attributes) all.equal( get.edgelist(exIgraph), structure(as.matrix(gg, "edgelist"), n=NULL, vnames=NULL)) netcompare(exIgraph, gg) } } \seealso{ Useful links: \itemize{ \item \url{https://mbojan.github.io/intergraph/} \item Report bugs at \url{https://github.com/mbojan/intergraph/issues} } } \author{ Written and maintained by Michal Bojanowski \email{m.bojanowski@icm.edu.pl}. } \keyword{package} intergraph/man/netcompare.Rd0000644000176200001440000000370714451017164015626 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/netcompare.R \name{netcompare} \alias{netcompare} \title{Comparing and testing network objects} \usage{ netcompare(target, current, test = FALSE, ...) } \arguments{ \item{target, current}{network objects, currently \code{network} and \code{igraph} classes are supported} \item{test}{logical, whether to perform the test or return comparison data, see Details} \item{\dots}{other arguments, currently ignored} } \value{ Depending on the value of \code{test} either an object of class \code{netcompare} containing the results of all the tests (if \code{test=FALSE}) or (if \code{test=TRUE}) a logical whether or not the networks are (nearly) the same. } \description{ Compare or test network objects for (near) equality. } \details{ Arguments \code{target} and \code{current} can be network objects of one of the supported classes. They do not have to be of the same class though. The function does a series of comparisons between \code{target} and \code{current}: \enumerate{ \item The network structure comparison is made based on adjacency matrices (mind this when using for huge networks). \item Network/edge/vertex attributes are checked for presence in both objects. \item Common network/edge/vertex attribures are checked for equality. } All the results are collected in a list of class \code{netcompare} with an associated \code{print} method. If \code{test} is TRUE then instead of the detailed test results the function returns TRUE or FALSE depending on some of the checks resulted positively. Currently attribute checks are ignored, i.e. what is taken into account is: \enumerate{ \item Equivalence of adjacency matrices \item Directed / undirected character of the network \item Edge set size \item Vertex set size } } \examples{ netcompare( asIgraph(exNetwork), exNetwork) netcompare( asIgraph(exNetwork), exNetwork, test=TRUE) } \seealso{ \code{\link{all.equal}}, \code{\link{identical}} } intergraph/man/exNetwork.Rd0000644000176200001440000001010114545555275015457 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/exNetwork.R \docType{data} \name{exNetwork} \alias{exNetwork} \alias{exIgraph} \alias{exNetwork2} \alias{exIgraph2} \title{Sample network structure} \format{ \describe{ \item{exNetwork,exNetwork2}{is of class \code{network}} \item{exIgraph,exIgraph2}{is of class \code{igraph}} } Objects \code{exNetwork} and \code{exIgraph} store directed version of the network. Objects \code{exNetwork2} and \code{exIgraph2} store the undirected version: all direction information from the edges is removed. The network consists of 15 vertices and 11 edges. \itemize{ \item Vertex 1 is an isolate. \item Vertices 2-6 constitute a star with vertex 2 as a center. \item Vertices 7-8 and 9-10 make two dyads \item Vertcies 11, 12, 13,14 and 15 make a stem-and-leaf network. } } \description{ An examples of networks together with network, edge and vertex attributes used primarly for testing. The same networks are stored in objects of class \code{network} and \code{igraph}. } \details{ Vertices and edges has attribute \code{label}. For vertices these are simply letters from "a" to "o". For edges these are two-letter sequences corresponding to the neightboring vertices, i.e. the label for the edges linking nodes "b" and "c" will be "bc". The order is irrelevant. In the \code{exNetwork} object the \code{label} attribute is also copied to the \code{vertex.names} attribute to facilitate plotting. The \code{exIgraph} object has additional graph attribute \code{layout} so that by default Fruchterman-Reingold placement is used for plotting. } \examples{ if(require(network, quietly=TRUE) ) print(exNetwork) if( require(igraph, quietly=TRUE) ) print(exIgraph) # showing-off 'network' versions if(require(network, quietly=TRUE)) { op <- par(mar=c(1,1,1,1)) layout( matrix(1:2, 1, 2, byrow=TRUE) ) # need to change the family to device default because of faulty 'igraph' plot(exNetwork, main="Directed, class 'network'", displaylabels=TRUE) plot(exNetwork2, main="Undirected, class 'network'", displaylabels=TRUE) par(op) } # not running because of a bug in 'igraph': plot.igraph wants to set default # font for vertex labels to 'serif', which is not supported on all devices if(FALSE) { # showing-off 'igraph' versions if(require(igraph, quietly=TRUE)) { op <- par(mar=c(1,1,1,1)) layout( matrix(1:2, 1, 2, byrow=TRUE) ) plot(exIgraph, main="Directed, class 'igraph'") plot(exIgraph2, main="Undirected, class 'igraph'") par(op) } } # The data was generated with the following code if(FALSE) { # directed igraph g <- igraph::graph( c(2,1, 3,1, 4,1, 5,1, # star 6,7, 8,9, # two dyads 10,11, 11,12, 12,13, 13,14, 14,12), # stem-leaf n=14, directed=TRUE) # add some vertex attributes vl <- letters[seq(1, vcount(g))] g <- igraph::set_vertex_attr(g, "label", value=vl) # add some edge attributes m <- igraph::as_edgelist(g) l <- matrix(vl[m+1], ncol=2) el <- apply(l, 1, paste, collapse="") g <- igraph::set_edge_attr(g, "label", value=el) g <- igraph::set_graph_attr(g, "layout", igraph::layout_with_fr) rm(vl, l, m, el) exIgraph <- g # undirected igraph exIgraph2 <- igraph::as.undirected(exIgraph) exIgraph2 <- igraph::set_edge_attr(exIgraph2, "label", value=igraph::edge_attr(exIgraph, "label")) # copy as a 'network' object through adjacency matrix m <- igraph::as_adjacency_matrix(exIgraph) g <- network::network(m, vertex.attr=list(label=vattr(exIgraph, "label")), vertex.attrnames="label", directed=TRUE) network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph, "label")) network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph, "label")) exNetwork <- network::network.copy(g) # copy as a 'network' object through adjacency matrix m <- igraph::as_adjacency_matrix(exIgraph2) g <- network::network(m, vertex.attr=list(label=vattr(exIgraph2, "label")), vertex.attrnames="label", directed=FALSE) network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph2, "label")) network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph2, "label")) exNetwork2 <- network::network.copy(g) } } \keyword{datasets} intergraph/DESCRIPTION0000644000176200001440000000205614556752432014143 0ustar liggesusersPackage: intergraph Type: Package Title: Coercion Routines for Network Data Objects Version: 2.0-4 Authors@R: person("Michał", "Bojanowski", role=c("aut", "cre"), email="michal2992@gmail.com", comment=c(ORCID="0000-0001-7503-852X")) Description: Functions implemented in this package allow to coerce (i.e. convert) network data between classes provided by other R packages. Currently supported classes are those defined in packages: network and igraph. URL: https://mbojan.github.io/intergraph/ BugReports: https://github.com/mbojan/intergraph/issues Imports: network (>= 1.4-2), igraph (>= 0.6-0), utils Suggests: rmarkdown, knitr, roxygen2, testthat, tibble VignetteBuilder: knitr License: GPL-3 LazyLoad: yes LazyData: yes RoxygenNote: 7.3.1 Encoding: UTF-8 Depends: R (>= 2.10) Config/testthat/edition: 3 NeedsCompilation: no Packaged: 2024-01-30 16:05:37 UTC; mbojan Author: Michał Bojanowski [aut, cre] () Maintainer: Michał Bojanowski Repository: CRAN Date/Publication: 2024-02-01 17:30:02 UTC intergraph/build/0000755000176200001440000000000014556217121013520 5ustar liggesusersintergraph/build/vignette.rds0000644000176200001440000000032414556217121016056 0ustar liggesusersmP0 lp.2`do~XTםBKuK!!|F|8}[Qc{HUVᵌjx0AwlPC_EK`n"jQ}tډ{ęaRUݣ ':37? t`xshs~vɵintergraph/tests/0000755000176200001440000000000014451017164013562 5ustar liggesusersintergraph/tests/testthat/0000755000176200001440000000000014556752432015434 5ustar liggesusersintergraph/tests/testthat/test-asDF.R0000644000176200001440000000312014451125647017341 0ustar liggesusers# From 'network' objects -------------------------------------------------- test_that("asDF(exNetwork) returns data frames with proper no of rows", { expect_silent( l <- asDF(exNetwork) ) # Created edgelist has appropriate number of edges expect_equal(nrow(l$edges), network::network.edgecount(exNetwork)) # Created vertex data frame has correct number of vertices expect_equal(nrow(l$vertexes), network::network.size(exNetwork)) }) test_that("asDF(exNetwork2) returns data frames with proper no of rows", { expect_silent( l <- asDF(exNetwork2) ) # Created edgelist has appropriate number of edges expect_equal( nrow(l$edges), network::network.edgecount(exNetwork)) # Created vertex data frame has correct number of vertices expect_equal( nrow(l$vertexes), network::network.size(exNetwork)) }) # From 'igraph' objects --------------------------------------------------- test_that("asDF(exIgraph) returns data frames with proper no of rows", { expect_silent( l <- asDF(exIgraph) ) # Created edgelist has appropriate number of edges expect_equal(nrow(l$edges), igraph::ecount(exIgraph)) # Created vertex data frame has correct number of vertices expect_equal(nrow(l$vertexes), igraph::vcount(exIgraph)) }) test_that("asDF(exIgraph2) returns data frames with proper no of rows", { expect_silent( l <- asDF(exIgraph2) ) # Created edgelist has appropriate number of edges expect_equal( nrow(l$edges), igraph::ecount(exIgraph2)) # Created vertex data frame has correct number of vertices expect_equal( nrow(l$vertexes), igraph::vcount(exIgraph2)) }) intergraph/tests/testthat/_snaps/0000755000176200001440000000000014451125647016713 5ustar liggesusersintergraph/tests/testthat/_snaps/netcompare.md0000644000176200001440000000153714556216726021405 0ustar liggesusers# netcompare just works Code print(r) Output Identical adjacency matrices: TRUE Network-level features: target current vcount 15 15 ecount 11 11 directed TRUE TRUE Presence of network-level attributes target current n 0 0 Common attributes comparison (TRUE=identical) No common attributes Presence of vertex-level attributes target current n 1 1 label TRUE TRUE Common attributes comparison (TRUE=identical) label : TRUE Presence of edge-level attributes target current n 1 1 label TRUE TRUE Common attributes comparison (TRUE=identical) label : TRUE intergraph/tests/testthat/test-attrmap.R0000644000176200001440000000071414545555275020212 0ustar liggesusers# Custom attrmap specifications ------------------------------------------- test_that("'na' vertex attribute can be dropped when going network->igraph", { net <- asNetwork(exIgraph) a <- attrmap() r <- data.frame( type="vertex", fromcls="network", fromattr="na", tocls="igraph", toattr=NA, stringsAsFactors=FALSE ) aa <- rbind(a, r) g <- asIgraph(net, amap=aa) expect_false( "na" %in% igraph::vertex_attr_names(g)) } ) intergraph/tests/testthat/test-validnet.R0000644000176200001440000000334414451125647020342 0ustar liggesusers# Validating edge lists --------------------------------------------------- test_that("Valid edgelist passes validation", { df1 <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) r1 <- intergraph:::validateEL(df1) expect_equal(df1, r1) } ) test_that("Edgelist with single column throws error", { expect_error( intergraph:::validateEL( data.frame(ego=1:5))) } ) test_that("Edgelist with NA (invalid) gives warning", { df2 <- data.frame( ego = 1:5, alter = c(2,NA,2,5,4)) expect_warning( intergraph:::validateEL(df2) ) } ) # Validating vertex data frames ------------------------------------------- test_that("Valid vertex DB passes validation", { df1 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) expect_equal(df1, validateVDB(df1)) } ) test_that("Empty vertex DB throws error", { df2 <- data.frame(id=numeric(0), x=character(0)) expect_error( validateVDB(df2) ) } ) test_that("Vertex DB with duplicated ids throws error", { df3 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) df3$id[3] <- 1 expect_error( validateVDB(df3)) } ) test_that("NAs in vertex ids gives warning", { df4 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) df4$id[2] <- NA expect_warning( validateVDB(df4)) } ) # Validating vdb vs edb --------------------------------------------------- test_that("Valid edgelist and vertex DB pass validation", { edb <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) vdb <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) expect_true( validNetDB(edb, vdb)) } ) test_that("Edgelist with some ids not present in VDB throw error", { elist <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) vdb <- data.frame(id=1:4, x=c(1,2,3,4), y=c(3,2,1,2)) expect_error( validNetDB(elist, vdb)) } ) intergraph/tests/testthat/test-asNetwork.R0000644000176200001440000000533614451125647020514 0ustar liggesusers# From data.frames -------------------------------------------------------- test_that("Disassembling directed network to d.f. and assembling back to network works", { l <- asDF(exNetwork) g <- asNetwork( l$edges, vertices=l$vertexes) expect_identical(g, exNetwork) } ) test_that("Disassembling undirected network to d.f. and assembling back to network works", { l <- asDF(exNetwork2) g <- asNetwork( l$edges, vertices=l$vertexes, directed=FALSE) expect_identical(g, exNetwork2) } ) test_that("Disassembling directed igraph to d.f. and assembling back to network: edgecount", { l <- asDF(exIgraph) g <- asNetwork(l$edges, vertices=l$vertexes) expect_equal( network::network.edgecount(g), nrow(l$edges)) } ) test_that("Disassembling directed igraph to d.f. and assembling back to network: vcount", { l <- asDF(exIgraph) g <- asNetwork(l$edges, vertices=l$vertexes) expect_equal( network::network.size(g), nrow(l$vertexes)) } ) test_that("Disassembling undirected igraph to d.f. and assembling back to network: edgecount", { l <- asDF(exIgraph2) g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE) expect_equal( network::network.edgecount(g), nrow(l$edges)) } ) test_that("Disassembling undirected igraph to d.f. and assembling back to network: vcount", { l <- asDF(exIgraph2) g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE) expect_equal( network::network.size(g), nrow(l$vertexes)) } ) # From igraphs ------------------------------------------------------------ test_that("Directed igraphs via netcompare", { res <- netcompare( asNetwork(exIgraph), exIgraph, test=TRUE ) expect_true(res) } ) test_that("Undirected igraphs via netcompare", { res2 <- netcompare( asNetwork(exIgraph2), exIgraph2, test=TRUE ) expect_true(res2) } ) test_that("NAs in igraph vertex labels are copied (bug 1926)", { # network with NA on edge attribute g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) igraph::E(g)$label <- c(1,2,3,NA,4) # convert to 'network' net <- asNetwork(g) # warning that NA is converted to "NA"? expect_true( is.na( network::get.edge.attribute(net, "label")[4] ) ) } ) # From tibbles ------------------------------------------------------------ test_that("Network is created from edgelist as tibble", { edb <- tibble::tibble( from = 1:4, to = 2:5 ) expect_silent( net <- asNetwork(edb) ) expect_equal( network::network.size(net), 5) expect_equal( network::network.edgecount(net), 4) }) test_that("Network is created from tibbles", { edb <- tibble::tibble( from = 1:4, to = 2:5 ) vdb <- tibble::tibble( id = 1:5, ch = letters[1:5], stringsAsFactors = FALSE ) expect_silent( net <- asNetwork(edb, vertices=vdb)) }) intergraph/tests/testthat/test-netcompare.R0000644000176200001440000000064714451125647020674 0ustar liggesuserstest_that("netcompare just works",{ expect_no_condition( r <- netcompare(exIgraph, exIgraph) ) expect_snapshot(print(r)) }) # compareAlist ------------------------------------------------------------ test_that("compareAlist returns NA when intersection of names sets is empty", { l1 <- list(a=2, b=1:5, c=1:5, d=3) l2 <- list(B=1:5, C=5:1) expect_identical(compareAlist(l1, l2), as.character(NA)) } ) intergraph/tests/testthat/test-github_issues.R0000644000176200001440000000136014545555275021415 0ustar liggesusers# GitHub issue 1 ---------------------------------------------------------- test_that("NAs are preserved in edge attributes", { g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) igraph::E(g)$label <- c(1,2,3,NA,4) net <- asNetwork(g) expect_true(any( is.na(network::get.edge.attribute(net, "label")))) ig <- asIgraph(net) expect_true(any( is.na(igraph::edge_attr(ig, "label")))) } ) test_that("NAs are preserved in vertex attributes", { g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) igraph::V(g)$label <- c(1,2,3,NA,4) net <- asNetwork(g) expect_true(any( is.na(network::get.vertex.attribute(net, "label")))) ig <- asIgraph(net) expect_true(any( is.na(igraph::vertex_attr(ig, "label")))) } ) intergraph/tests/testthat/test-asIgraph.R0000644000176200001440000000500314545555275020274 0ustar liggesusers# From data.frames -------------------------------------------------------- test_that("Disassembling to d.f and assembling back to igraph gives the same result", { # convert to data frames l <- asDF(exIgraph) # assemble back g <- asIgraph( l$edges, vertices=l$vertexes) expect_true( igraph::identical_graphs(g, exIgraph) ) } ) test_that("Providing non-existing name to 'vnames' throws an error", { ## testing 'vnames' argument # non-existent column in 'vertices' expect_error( asIgraph(l$edges, vertices=l$vertexes, vnames="foo") ) } ) test_that("Vertex names are properly set via 'vnames' argument for directed network", { # existing column in 'vertices' l <- asDF(exIgraph) g <- asIgraph( l$edges, vertices=l$vertexes, vnames="label") expect_equal( l$vertexes$label, igraph::vertex_attr(g, "name")) } ) test_that("Vertex names are properly set via 'vnames' argument for undirected network", { ## above tests but for the undirected network ## convert to data frames and assemble back to igraph object l <- asDF(exIgraph2) g <- asIgraph( l$edges, vertices=l$vertexes, directed=FALSE) expect_true( igraph::identical_graphs(g, exIgraph2) ) } ) # From tibbles ------------------------------------------------------------ test_that("Igraph is created from edgelist as tibble", { edb <- tibble::tibble( from = 1:4, to = 2:5 ) expect_silent( net <- asIgraph(edb) ) expect_equal( igraph::vcount(net), 5) expect_equal( igraph::ecount(net), 4) }) test_that("Network is created from tibbles", { edb <- tibble::tibble( from = 1:4, to = 2:5 ) vdb <- tibble::tibble( id = 1:5, ch = letters[1:5] ) expect_silent( net <- asIgraph(edb, vertices=vdb)) expect_equal( igraph::vcount(net), 5) expect_equal( igraph::ecount(net), 4) }) # From networks ----------------------------------------------------------- test_that("Conversion for exNetwork is OK tested with netcompare", { # directed network res <- netcompare( asIgraph(exNetwork), exNetwork, test=TRUE ) expect_true(res) } ) test_that("Conversion for exNetwork2 is OK tested with netcompare", { # undirected network res2 <- netcompare( asIgraph(exNetwork2), exNetwork2, test=TRUE ) expect_true(res2) } ) test_that("Conversion of bipartite networks is not yet supported", { ### bipartite network (not yet supported) m <- matrix(0, ncol=2, nrow=3) m[1,1] <- m[2,1] <- m[1,2] <- m[3,2] <- 1 net <- network::network(t(m), bipartite=TRUE, directed=FALSE) expect_error(asIgraph(net)) } ) intergraph/tests/testthat.R0000644000176200001440000000010014451017164015534 0ustar liggesuserslibrary(testthat) library(intergraph) test_check("intergraph") intergraph/vignettes/0000755000176200001440000000000014556217121014431 5ustar liggesusersintergraph/vignettes/howto.Rmd0000644000176200001440000002020514545555275016251 0ustar liggesusers--- title: "Short `intergraph` tutorial" author: "Michał Bojanowski" output: rmarkdown::html_vignette: toc: true number_sections: true vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{Short intergraph tutorial} %\VignetteEncoding{UTF-8} --- ```{r, setup, include=FALSE} library(intergraph) library(knitr) set.seed(123) ``` - - - "Intergraph" is an R package with coercion routines for netowrk data objects. For more information, see * Homepage on [https://mbojan.github.io/intergraph/](https://mbojan.github.io/intergraph/). * Package development pages on [https://github.com/mbojan/intergraph](https://github.com/mbojan/intergraph). This is a short tutorial showing how to use functions in package "intergraph" using some example network data contained in the package. # Loading example data To show the data, first load the packages. ```{r,packages} library(intergraph) library(network) library(igraph) ``` Now, these are the summaries of the "igraph" objects: ```{r, summarize-igraph} summary(exIgraph) summary(exIgraph2) ``` These are the summaries of the "network" objects: ```{r,summarize-network} exNetwork exNetwork2 ``` More information is available in the Appendix. # Functions `asNetwork` and `asIgraph` Conversion of network objects between classes "network" and "igraph" can be performed using functions `asNetwork` and `asIgraph`. ## network => igraph Converting "network" objects to "igraph" is done by calling function `asIgraph` on a "network" object: ```{r,network2igraph} # check class of 'exNetwork' class(exNetwork) # convert to 'igraph' g <- asIgraph(exNetwork) # check class of the result class(g) ``` Check if edgelists of the objects are identical ```{r} el.g <- get.edgelist(g) el.n <- as.matrix(exNetwork, "edgelist") identical( as.numeric(el.g), as.numeric(el.n)) ``` ## igraph => network Converting "igraph" objects to "network" is done by calling function `asNetwork` on an "igraph" object: ```{r,igraph2network} net <- asNetwork(exIgraph) ``` Note the warning because of a "non-standard" network attribute `layout`, which is a function. Printing "network" objects does not handle non-standard attributes very well. However, all the data and attributes are copied correctly. Check if edgelists of the objects are identical ```{r} el.g2 <- get.edgelist(exIgraph) el.n2 <- as.matrix(net, "edgelist") identical( as.numeric(el.g2), as.numeric(el.n2)) ``` ## Handling attributes Objects of class "igraph" and "network", apart from storing actual network data (vertexes and edges), allow for adding attributes of vertexes, edges, and attributes of the network as a whole (called "network attributes" or "graph attributes" in the nomenclatures of packages "network" and "igraph" respectively). Vertex and edge attributes are used by "igraph" and "network" in a largely similar fashion. However, network-level attributes are used differently. Objects of class "network" use network-level attributes to store various metadata, e.g., network size, whether the network is directed, is bipartite, etc. In "igraph" this information is stored separately. The above difference affects the way the attributes are copied when we convert "network" and "igraph" objects into one another. Both functions `asNetwork` and `asIgraph` have an additional argument `attrmap` that is used to specify how vertex, edge, and network attributes are copied. The `attrmap` argument requires a data frame. Rows of that data frame specify rules of copying/renaming different attributes. The data frame should have the following columns (all of class "character"): * `type`: one of "network", "vertex" or "edge", whether the rule applies to network, vertex or edge attribute. * `fromslc`: name of the which we are *converting from* * `fromattr`: name of the attribute in the object we are converting from * `tocls`: name of the class of the object we are *converting to* * `toattr`: name of the attribute in the object we are converting to The default rules are returned by a function `attrmap()`, these are: ```{r attrmap-defaults} attrmap() ``` For example, the last row specifies a rule that when an object of class "igraph" is converted to class "network", then a vertex attribute `name` in the "igraph" object will be copied to a vertex attribute called `vertex.names` in the resulting object of class "network. If the column `toattr` contains an `NA`, that means that the corresponding attribute is not copied. For example, the first row specifies a rule that when an object of class "network" is converted to class "igraph", then a network attribute `directed` in the "network" object is *not* copied to the resulting object of class "igraph". Users can customize the rules, or add new ones, by constructing similar data frames and supplying them through argument `attrmap` to functions `asIgraph` and `asNetwork`. As an example let us set the option to always drop the `na` vertex attribute. First, we need to setup the rule by adding an extra row to the data frame returned by `attrmap`: ```{r attrmap-example-rules} new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na", tocls="igraph", toattr=NA, stringsAsFactors=FALSE) # combine with the default rules rules <- rbind( attrmap(), new_rule ) rules ``` Now we can use it with `asIgraph`: ```{r attrmap-example} (ig1 <- asIgraph(exNetwork)) (ig2 <- asIgraph(exNetwork, amap=rules)) # check if "na" was dropped "na" %in% igraph::vertex_attr_names(ig1) "na" %in% igraph::vertex_attr_names(ig2) ``` # Network objects to/from data frames Function `asDF` can be used to convert network object (of class "igraph" or "network") to a list of two data frames: ```{r asDF} l <- asDF(exIgraph) str(l) ``` The resulting list has two components `edges` and `vertexes`. The `edges` component is essentially an edge list containing ego and alter ids in the first two columns. The remaining columns store edge attributes (if any). For our example data it is ```{r show-edgedb} l$edges ``` The `vertexes` component contains data on vertexes with vertex id (the same that is used in the first two column of `edges`) is stored in the first two columns. The remaining columns store vertex attributes (if any). For our example data it is: ```{r show-vertexdb} l$vertexes ``` Functions `asNetwork` and `asIgraph` can also be used to create network objects from data frames such as those above. The first argument should be an edge list data frame. Optional argument `vertices` expectes data frames with vertex data (just like `l$vertexes`). Additionally we need to specify whether the edges should be interpreted as directed or not through the argument `directed`. For example, to create an object of class "network" from the dataframes created above from object `exIgraph` we can: ```{r fromdf} z <- asNetwork(l$edges, directed=TRUE, l$vertexes) z ``` This is actually what basically happens when we call `asNetwork(exIgraph)` - - - # Appendix ## Example networks Package intergraph contains four example networks: * Objects `exNetwork` and `exIgraph` contain the same *directed* network as objects of class "network" and "igraph" respectively. * Objects `exNetwork2` and `exIgraph2` contain the same *undirected* network as objects of class "network" and "igraph" respectively. All four datasets contain: * A vertex attribute `label` with vertex labels. These are letters from `a` to `o`. * An edge attribute `label` with edge labels. These are pasted letters of the adjecent nodes. We will use them in the examples below. Networks are shown below using the following code: ```{r showdata-code,eval=FALSE} layout(matrix(1:4, 2, 2, byrow=TRUE)) op <- par(mar=c(1,1,2,1)) # compute layout coords <- layout.fruchterman.reingold(exIgraph) plot(exIgraph, main="exIgraph", layout=coords) plot(exIgraph2, main="exIgraph2", layout=coords) plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords) plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords) par(op) ``` ```{r showdata-pic, ref.label="showdata-code",echo=FALSE,fig.height=10,fig.width=10} ``` ## Session information ```{r, session_info} sessionInfo() ``` intergraph/R/0000755000176200001440000000000014545556137012635 5ustar liggesusersintergraph/R/exNetwork.R0000644000176200001440000000305614451017164014736 0ustar liggesusers#'Sample network structure #' #'An examples of networks together with network, edge and vertex attributes #'used primarly for testing. The same networks are stored in objects of class #'\code{network} and \code{igraph}. #' #'Vertices and edges has attribute \code{label}. For vertices these are simply #'letters from "a" to "o". For edges these are two-letter sequences #'corresponding to the neightboring vertices, i.e. the label for the edges #'linking nodes "b" and "c" will be "bc". The order is irrelevant. #' #'In the \code{exNetwork} object the \code{label} attribute is also copied to #'the \code{vertex.names} attribute to facilitate plotting. #' #'The \code{exIgraph} object has additional graph attribute \code{layout} so #'that by default Fruchterman-Reingold placement is used for plotting. #' #'@name exNetwork #'@aliases exNetwork exIgraph exNetwork2 exIgraph2 #'@docType data #'@format \describe{ \item{exNetwork,exNetwork2}{is of class \code{network}} #'\item{exIgraph,exIgraph2}{is of class \code{igraph}} } Objects #'\code{exNetwork} and \code{exIgraph} store directed version of the network. #'Objects \code{exNetwork2} and \code{exIgraph2} store the undirected version: #'all direction information from the edges is removed. #' #'The network consists of 15 vertices and 11 edges. \itemize{ \item Vertex 1 is #'an isolate. \item Vertices 2-6 constitute a star with vertex 2 as a center. #'\item Vertices 7-8 and 9-10 make two dyads \item Vertcies 11, 12, 13,14 and #'15 make a stem-and-leaf network. } #'@keywords datasets #'@example examples/exNetwork.R NULL intergraph/R/intergraph-package.R0000644000176200001440000000233514545556137016517 0ustar liggesusers#' Coercion Routines for Network Data Objects #' #' This package contains methods for coercion between various classes used to #' represent network data in R. #' #' Functions implemented in this package allow to coerce (i.e. convert) network #' data between classes provided by other R packages. Currently supported #' classes are: "network" from package \pkg{network}, "igraph" from package #' \pkg{igraph}. #' #' The main functions are: #' \itemize{ #' \item \code{\link{asNetwork}} and its methods to create objects of class #' "network". #' #' \item \code{\link{asIgraph}} and its methods to create objects of class #' "igraph". #' } #' See their help pages for more information and examples. #' #' As all the supported packages are written using S3 methods, so are the #' methods in this package. #' #' If you find this package useful in your work please cite it. Type #' \code{citation(package="intergraph")} for the information how to do that. #' #' @importFrom network as.matrix.network #' @importFrom network add.edges.network #' #' @name intergraph-package #' #' @author Written and maintained by Michal Bojanowski \email{m.bojanowski@@icm.edu.pl}. #' #' @keywords package #' #' @example examples/package-intergraph.R "_PACKAGE" intergraph/R/asDF.R0000644000176200001440000000770114545555275013604 0ustar liggesusers#' Convert network to data frame(s) #' #' Convert a network data object to, possibly two, data frames: a data frame #' with an edge list with edge attributes (if any), and a data frame of vertexes #' with vertex attributes (if any). This is a generic function, see below for #' available methods. #' #' Currently there are methods for \code{object} being in one of the following #' classes: "network", "igraph". #' #' The function first gets the graph edge list using the appropriate function #' depending on the class of \code{object} (see below). Edge attributes, if #' any, are then extracted using \code{\link{dumpAttr}} and added to it. #' #' The vertex data frame is constructed with a vertex id as a sequence of #' integer numbers. Details are method-specific, see below. Vertex attributes #' are extracted with \code{\link{dumpAttr}} and added to this data frame. #' #' Method-specific notes: #' #' @param object R object representing a network, see below for available #' methods #' #' @param \dots other arguments passed to/from other methods #' #' @return List with two components: #' \describe{ #' \item{\code{edges}}{containing an edge list data frame at first two columns #' and edge attributes on further ones.} #' \item{\code{vertexes}}{with vertex id in the first column, named \code{id} #' and any vertex attributes in the other columns.} #' } #' #' @export #' #' @example examples/asDF.R #' asDF <- function(object, ...) UseMethod("asDF") #' @method asDF network #' @export #' @rdname asDF #' @details #' For objects of class "network". Objects of this class store the vertex ids #' as integer numbers. There is also an attribute "vertex.names" which is #' always created when using graph constructors provided in the package #' \pkg{network}. \code{asDF} adds "vertex.names" to the vertex data frame as #' a normal attribute and does not use it as a vertex id in the edge list. #' #' The edge list is created using \code{\link[network]{as.matrix.network}} #' function and contains integer vertex ids. asDF.network <- function(object, ...) { # get edge list and substitute vertex names dfedge <- as.data.frame(network::as.matrix.network(object, "edgelist"), stringsAsFactors=FALSE) # add edge attributes, if any eattr <- dumpAttr(object, "edge") if( length(eattr) > 0 ) dfedge <- cbind(dfedge, as.data.frame(eattr, stringsAsFactors=FALSE)) # make vertex data frame dfvertex <- data.frame(intergraph_id=seq(1, network::network.size(object))) # add vertex attributes if any vattr <- dumpAttr(object, "vertex") if( length(vattr) > 0 ) dfvertex <- cbind( dfvertex, as.data.frame(vattr, stringsAsFactors=FALSE)) list(edges=dfedge, vertexes=dfvertex) } #' @method asDF igraph #' @export #' @rdname asDF #' @details #' Objects of class "igraph", as provided by the \pkg{igraph} package. Vertex #' ids in these objects integers starting from 1 (in \pkg{igraph} version prior #' to 0.6-0 vertex ids started from 0). However, it is also possible to provide #' a vertex attribute "name". It is added to the vertex data frame as a normal #' vertex attribute and is not used on the edge list data frame. #' #' The edge list is created using \code{\link[igraph]{get.edgelist}} function #' with argument \code{names} set to \code{FALSE} so that integer vertex ids #' are used. asDF.igraph <- function(object, ...) { # get edgelist dfedge <- as.data.frame(igraph::as_edgelist(object, names=FALSE), stringsAsFactors=FALSE) # add edge attributes, if any eattr <- dumpAttr(object, "edge") if( length(eattr) > 0 ) dfedge <- cbind(dfedge, as.data.frame(eattr, stringsAsFactors=FALSE)) # make vertex data frame dfvertex <- data.frame(intergraph_id=seq(1, igraph::vcount(object) )) # add vertex attributes, if any vattr <- dumpAttr(object, "vertex") if( length(vattr) > 0 ) dfvertex <- cbind( dfvertex, as.data.frame(vattr, stringsAsFactors=FALSE)) list(edges=dfedge, vertexes=dfvertex) } intergraph/R/dumpAttr.R0000644000176200001440000000410514545555275014562 0ustar liggesusers#' Dump network attributes to a list #' #' Given a network return a list of all the attributes. #' #' #' @aliases dumpAttr dumpAttr.network dumpAttr.igraph #' @param x network object #' @param type character, type of attributes to dump #' @param \dots other arguments from/to other methods #' @return If \code{type} is one of "network", "vertex" or "edge" then a list of #' corresponding attributes. #' #' If \code{type} is "all" then lists of lists of attributes. #' #' @export #' @example examples/dumpAttr.R dumpAttr <- function(x, ...) UseMethod("dumpAttr") #' @method dumpAttr network #' @export #' @rdname dumpAttr dumpAttr.network <- function(x, type=c("all", "network", "vertex", "edge"), ...) { type <- match.arg(type) if(type == "all") { n <- c("network", "vertex", "edge") rval <- lapply( n, function(nam) dumpAttr(x, type=nam)) names(rval) <- n return(rval) } else { type <- match.arg(type) nam <- switch(type, network = network::list.network.attributes(x), edge = network::list.edge.attributes(x), vertex = network::list.vertex.attributes(x) ) rval <- switch( type, network = lapply( nam, function(a) network::get.network.attribute(x, a)), edge = lapply( nam, function(a) network::get.edge.attribute(x$mel, a)), vertex = lapply( nam, function(a) network::get.vertex.attribute(x, a)) ) names(rval) <- nam return(rval) } } #' @method dumpAttr igraph #' @export #' @rdname dumpAttr dumpAttr.igraph <- function(x, type=c("all", "network", "vertex", "edge"), ...) { type <- match.arg(type) if(type == "all") { n <- c("network", "vertex", "edge") rval <- lapply( n, function(nam) dumpAttr(x, type=nam)) names(rval) <- n return(rval) } else { nams <- switch( type, network = igraph::graph_attr_names(x), edge = igraph::edge_attr_names(x), vertex = igraph::vertex_attr_names(x) ) rval <- switch( type, network = lapply( nams, function(a) igraph::graph_attr(x, a) ), edge = lapply( nams, function(a) igraph::edge_attr(x, a) ), vertex = lapply( nams, function(a) igraph::vertex_attr(x, a) ) ) names(rval) <- nams return(rval) } } intergraph/R/netcompare.R0000644000176200001440000001445514451125647015120 0ustar liggesusers#' Comparing and testing network objects #' #' Compare or test network objects for (near) equality. #' #' Arguments \code{target} and \code{current} can be network objects of one of #' the supported classes. They do not have to be of the same class though. #' #' The function does a series of comparisons between \code{target} and #' \code{current}: #' #' \enumerate{ #' \item The network structure comparison is made based on adjacency matrices #' (mind this when using for huge networks). #' #' \item Network/edge/vertex attributes are checked for presence in both #' objects. #' #' \item Common network/edge/vertex attribures are checked for equality. #' } #' All the results are collected in a list of class \code{netcompare} with an #' associated \code{print} method. #' #' If \code{test} is TRUE then instead of the detailed test results the function #' returns TRUE or FALSE depending on some of the checks resulted positively. #' Currently attribute checks are ignored, i.e. what is taken into account is: #' #' \enumerate{ #' \item Equivalence of adjacency matrices #' \item Directed / undirected character of the network #' \item Edge set size #' \item Vertex set size #' } #' #' @param target,current network objects, currently \code{network} and #' \code{igraph} classes are supported #' #' @param test logical, whether to perform the test or return comparison data, #' see Details #' #' @param \dots other arguments, currently ignored #' #' @return Depending on the value of \code{test} either an object of class #' \code{netcompare} containing the results of all the tests (if #' \code{test=FALSE}) or (if \code{test=TRUE}) a logical whether or not the #' networks are (nearly) the same. #' #' @seealso \code{\link{all.equal}}, \code{\link{identical}} #' #' @export #' #' @examples #' #'netcompare( asIgraph(exNetwork), exNetwork) #'netcompare( asIgraph(exNetwork), exNetwork, test=TRUE) #' netcompare <- function(target, current, test=FALSE, ...) { # trivial checks rval <- list() # class rval$class <- c(target=class(target), current=class(current)) # number of vertices rval$vcount <- c(target=igVcount(target), current=igVcount(current)) # number of edges rval$ecount <- c( target=igEcount(target), current=igEcount(current)) # directedness rval$directed <- c( target=igDirected(target), current=igDirected(current)) # compare adjacency matrices rval$identical_am <- compareEdges(target, current) # compare attributes targeta <- dumpAttr(target) currenta <- dumpAttr(current) rval$network <- compareAttributes( targeta$network, currenta$network) rval$vertex <- compareAttributes( targeta$vertex, currenta$vertex) rval$edge <- compareAttributes( targeta$edge, currenta$edge) rval <- structure(rval, class="netcompare") if(test) compareTest(rval) else return(rval) } #' @export print.netcompare <- function(x, ...) { cat("\n") cat("Identical adjacency matrices:\n") cat( paste(x$identical_am, collapse=", "), "\n", fill=TRUE, labels=" ") cat("Network-level features:\n") m <- do.call("rbind", lapply(x[c("vcount", "ecount", "directed")], format)) print(m, quote=FALSE) cat("\n") cat("Presence of network-level attributes\n") print(x$network) cat("\n") cat("Presence of vertex-level attributes\n") print(x$vertex) cat("\n") cat("Presence of edge-level attributes\n") print(x$edge) } # comparing and testing for (near) equality of networks # # Result of comparison: # # 1. computed network-level comparisons # # 2. built-in network-level comparisons # # 3. attributes # # 3a. attribute presence # # 3b. identical common attributes # # NOTE: Makes use of non-exported generic functions compareTest <- function(object) { stopifnot(inherits(object, "netcompare")) rval <- logical() rval["adjacency"] <- object$identical_am rval["vcount"] <- object$vcount[1] == object$vcount[2] rval["ecount"] <- object$ecount[1] == object$ecount[2] rval["directed"] <- object$directed[1] == object$directed[2] all(rval) } compareEdges <- function(target, current, use.names=FALSE) { op <- igraph::igraph_options(sparsematrices = FALSE) on.exit(igraph::igraph_options(op)) tr <- try(utils::getS3method("as.matrix", class=class(target)), silent=TRUE) if(inherits(tr, "try-error")) stop("cannot find 'as.matrix' method for class ", dQuote(class(target))) tr <- try(utils::getS3method("as.matrix", class=class(current)), silent=TRUE) if(inherits(tr, "try-error")) stop("cannot find 'as.matrix' method for class ", dQuote(class(current))) mtar <- as.matrix(target, "adjacency") mcur <- as.matrix(current, "adjacency") # compare matrices (no dimnames) if(use.names) all.equal(mtar, mcur) else all.equal( structure(mtar, dimnames=NULL), structure(mcur, dimnames=NULL) ) } # compare common components of a list (by name) # return a list of all.equal results compareAlist <- function(target, current) { # common components nams <- intersect(names(target), names(current)) if( length(nams) == 0 ) return(as.character(NA)) rval <- lapply(nams, function(n) all.equal( target[[n]], current[[n]]) ) names(rval) <- nams rval } # Compare lists of attributes (as returned by 'dumpAttr') compareAttributes <- function(target, current) { rval <- list() # compare number of attributes rval$n <- c(target=length(target), current=length(current)) # Check for attributes by name pre <- list() u <- union(names(target), names(current)) if(length(u) == 0L) { pre <- matrix(NA, 0, 2, dimnames = list(NULL, c("target", "current"))) } else { r <- t(sapply(u, function(a) c( a %in% names(target), a %in% names(current) ) )) pre <- c(pre, list(r)) pre <- do.call("rbind", pre) dimnames(pre) <- list(rownames(pre), c("target", "current")) } rval$presence <- pre rval$bycomp <- compareAlist(target, current) structure(rval, class="netcomparea") } # Print method for the result of 'compareAttributes' #' @export print.netcomparea <- function(x, ...) { m <- do.call("rbind", lapply( x[c("n", "presence")], format)) print(m, quote=FALSE) cat("Common attributes comparison (TRUE=identical)\n") if( identical( x$bycomp, as.character(NA)) ) { cat(" No common attributes\n") } else { l <- sapply(x$bycomp, paste, collapse=", ") for(i in seq(along=l)) cat(names(l)[i], ":", l[i], fill=TRUE, labels=" ") } } intergraph/R/attrmap.R0000644000176200001440000000624514451017164014423 0ustar liggesusers#' Network attribute copying/renaming table #' #' Setting and retrieving rules through which network/edge/vertex attributes #' are copied or renamed when converting network objects. #' #' Different classes for network data use different attribute names to store #' the same information. Some of the classes define attributes not used by #' other classes. This function is used to retrieve or set the rules in which #' these attributes are copied, renamed or dropped when converting network #' objects. #' #' The rules are stored in a data frame with the following columns (all of mode #' character): #' \describe{ #' \item{type}{type, or level of the attribute, one of "vertex", "edge" or #' "network"} #' \item{fromcls}{name of the class which is being coerced} #' \item{fromattr}{name of the "source" attribute} #' \item{tocls}{name of the class to which coercion is being done} #' \item{toattr}{name of the attribute in the result of coercion, or NA} #' } #' When converting network \code{x}, of class \code{xclass} to network \code{y} #' of class \code{yclass} the coercion method looks up the rows in this table #' for which \code{fromcls=xclass} and \code{tocls=yclass}. If network \code{x} #' has any of the attributes listed in the \code{fromattr} in the resulting #' subset of the table then, it is renamed to the corresponding value of #' \code{toattr}. If \code{toattr} is \code{NA} the attribute is dropped from #' the result. #' #' @param newdf data.frame, new set of copy/rename rules, see Details #' #' @return If \code{newdf} is NULL, a data frame with the current copy/rename #' rules. Otherwise \code{newdf} are set as new rules and the old ones are #' returned invisibly, much like \code{\link{par}}. #' #' @seealso This is used by \code{\link{asIgraph}} and \code{\link{asNetwork}} #' #' @export #' #' @examples #' #'# Current values #'attrmap() #' attrmap <- function(newdf=NULL) { cur <- utils::getFromNamespace(".attrmap", "intergraph") if( is.null(newdf) ) { # return current return(cur) } else { # assign new utils::assignInNamespace(".attrmap", newdf, "intergraph") # return old invisibly invisible(cur) } } # subset of attrmap depending on type and from/to classes attrmapmat <- function(from, to, atype, db=attrmap()) { i <- with(db, type==atype & fromcls==from & tocls==to) as.matrix( db[i, c("fromattr", "toattr")] ) } # attribute renaming table .attrmap <- matrix(ncol=5, byrow=TRUE, c( "network" , "network" , "directed" , "igraph" , NA , "network" , "network" , "bipartite" , "igraph" , NA , "network" , "network" , "loops" , "igraph" , NA , "network" , "network" , "mnext" , "igraph" , NA , "network" , "network" , "multiple" , "igraph" , NA , "network" , "network" , "n" , "igraph" , NA , "network" , "network" , "hyper" , "igraph" , NA , "vertex" , "igraph" , "name" , "network" , "vertex.names" ) ) .attrmap <- as.data.frame(.attrmap, stringsAsFactors=FALSE) names(.attrmap) <- c("type", "fromcls", "fromattr", "tocls", "toattr") intergraph/R/asIgraph.R0000644000176200001440000000763214545555275014530 0ustar liggesusers#' Coerce an object to class "igraph" #' #' Coerce objects to class "igraph". #' #' \code{asIgraph} is a generic function with methods written for data frames #' and objects of class "network". #' #' If \code{x} is a data frame, the method used is a wrapper around #' \code{\link[igraph]{graph.data.frame}} in package \pkg{igraph}. The #' \code{vnames} argument was added so that the user can specify which vertex #' attribute from the data frame supplied through \code{vertices} argument is #' used for vertex names (the \code{name} attribute in \code{igraph} objects) in #' the returned result. By default the vertex names are not created. #' #' If \code{x} is of class "network" (package \pkg{network}) the function #' uses \code{\link{asDF}} to extract data on edges and vertex with their #' attributes (if present). Network attributes are extracted as well. Not all #' vertex/edge/network attributes are worth preserving though. Attributes are #' copied, dropped or renamed based on rules given in the \code{amap} #' argument, see \code{\link{attrmap}} for details. The function currently does #' not support objects that represent neither bipartite networks nor #' hypergraphs. #' #' @param x R object to be converted #' @param directed logical, whether the created network should be directed #' @param amap data.frame with attribute copy/rename rules, see #' \code{\link{attrmap}} #' @param vertices NULL or data frame, optional data frame containing vertex #' attributes #' @param vnames character, name of the column in \code{vertices} to be used as #' a \code{name} vertex attribute, if \code{NULL} no vertex names are created #' @param \dots other arguments from/to other methods #' #' @return Object of class "igraph". #' #' @seealso \code{\link[igraph]{graph.data.frame}} #' #' @export #' #' @example examples/asIgraph.R #' asIgraph <- function(x, ...) UseMethod("asIgraph") #' @method asIgraph network #' @export #' @rdname asIgraph asIgraph.network <- function(x, amap=attrmap(), ...) { object <- x # hypergraphs not supported if(network::is.hyper(object)) stop("hypergraphs are not supported") if(network::is.bipartite(object)) stop("bipartite networks are not supported") na <- dumpAttr(object, "network") l <- asDF(object) ### prepare edge attributes eats <- attrmapmat("network", "igraph", "edge", db=amap) # drop some todrop <- eats[ is.na(eats[,"toattr"]) , "fromattr" ] edges <- l$edges[ !( names(l$edges) %in% todrop ) ] # rename some names(edges) <- recode(names(edges), eats) ### prepare vertex attributes vats <- attrmapmat("network", "igraph", "vertex", db=amap) # drop some todrop <- vats[ is.na(vats[,"toattr"]) , "fromattr" ] vertexes <- l$vertexes[ !( names(l$vertexes) %in% todrop ) ] # rename some names(vertexes) <- recode(names(vertexes), vats) ### make 'igraph' object rval <- asIgraph( edges, directed=network::is.directed(object), vertices=vertexes, ...) ### apply/rename/drop network attributes nats <- attrmapmat("network", "igraph", "network", db=amap) todrop <- nats[ is.na(nats[,"toattr"]) , "fromattr" ] na <- na[ - which( names(na) %in% todrop ) ] names(na) <- recode(names(na), nats) if( length(na) > 0 ) { for( naname in names(na) ) rval <- igraph::set_graph_attr(rval, naname, na[[naname]]) } rval } #' @method asIgraph data.frame #' @export #' @rdname asIgraph asIgraph.data.frame <- function(x, directed=TRUE, vertices=NULL, vnames=NULL, ...) { object <- x rval <- igraph::graph_from_data_frame( object, directed=directed, vertices=vertices) if(is.null(vnames)) { rval <- igraph::delete_vertex_attr(rval, "name") } else { if( !(vnames %in% names(vertices)) ) stop("no column ", vnames, " in 'vertices'") rval <- igraph::set_vertex_attr(rval, "name", value=vertices[[vnames]]) } rval } intergraph/R/recode.R0000644000176200001440000000043714451017164014211 0ustar liggesusers# given a vector and a recode matrix replace the values in 'x' that match in # 'm[,1]' with the corresponding replacements from 'm[,2]' recode <- function(x, mat) { i <- match(x, mat[,1]) i2 <- which(!is.na(i)) i <- i[i2] rval <- x rval[i2] <- mat[,2][i] rval } intergraph/R/validnet.R0000644000176200001440000000345014451017164014554 0ustar liggesusers# Validating data frames containing edge database (edge list with edge # attributes) and vertex data bases (vertices with vertex attributes) # Validates edge list validateEL <- function(x) { # must be data.frame stopifnot(inherits(x, "data.frame")) # at least two columns if (ncol(x) < 2) { stop("the data frame should contain at least two columns") } # Handling NAs if (any(is.na(x[,1:2]))) { warning("In first two columns of `x' `NA' elements were replaced with string \"NA\"") x[,1:2][is.na(x[,1:2])] <- "NA" } x } # validate vertex database validateVDB <- function(x) { stopifnot(inherits(x, "data.frame")) # empty data frame if(nrow(x) == 0) stop("vertex data frame has no rows") # duplicated vertex ids dups <- duplicated(x[,1]) if( any(dups) ) stop(paste("duplicated ids in vertex db:", paste(x[dups,1], collapse=", "))) # Handling NAs isna <- is.na(x[,1]) if (any(isna)) { warning("in `vertices[,1]' `NA' elements were replaced with string \"NA\"") x[isna, 1] <- "NA" } x } # validate edge DB versus vertex DB # returns TRUE or vector of warnings validNetDB <- function(edb, vdb, test=FALSE) { edb <- validateEL(edb) vdb <- validateVDB(vdb) errors <- NULL # TODO ids in el missing in vdb uvids <- unique(c(edb[,1], edb[,2])) i <- uvids %in% vdb[,1] if(!all(i)) errors <- c(errors, paste("some vertex ids in edge db are not found in vertex db:", paste(uvids[!i], collapse=", "))) # return if(is.null(errors)) return(TRUE) if(test) return(errors) else { msg <- "vertex and edge data frames are incompatible:" if(length(errors) > 1L) stop(paste(msg, paste(paste(seq_along(errors), errors, sep=": ")), collapse="\n")) else stop(msg, " ", errors) } } intergraph/R/s3common.R0000644000176200001440000000127314545555372014521 0ustar liggesusers #============================================================================ # common functions igVcount <- function(x, ...) UseMethod("igVcount") #' @export igVcount.igraph <- function(x, ...) igraph::vcount(x) #' @export igVcount.network <- function(x, ...) network::network.size(x) igEcount <- function(x, ...) UseMethod("igEcount") #' @export igEcount.igraph <- function(x, ...) igraph::ecount(x) #' @export igEcount.network <- function(x, ...) network::network.edgecount(x) igDirected <- function(x) UseMethod("igDirected") #' @export igDirected.igraph <- function(x) { igraph::is_directed(x) } #' @export igDirected.network <- function(x) { network::is.directed(x) } intergraph/R/asNetwork.R0000644000176200001440000001114214545555275014736 0ustar liggesusers#' Convert objects to class "network" #' #' Convert objects to class "network" #' #' This is a generic function which dispatches on argument \code{x}. It creates #' objects of class "network" from other R objects. #' #' The method for data frames is inspired by the similar function in package #' \pkg{igraph}: \code{\link[igraph]{graph.data.frame}}. It assumes that first #' two columns of \code{x} constitute an edgelist. The remaining columns are #' interpreted as edge attributes. Optional argument \code{vertices} allows for #' including vertex attributes. The first column is assumed to vertex id, the #' same that is used in the edge list. The remaining colums are interpreted as #' vertex attributes. #' #' The method for objects of class "igraph" takes the network of that class and #' converts it to data frames using \code{\link{asDF}}. The network is recreated #' in class "network" using \code{asNetwork.data.frame}. The function currently #' does not support bipartite "igraph" networks. #' #' @param x an R object to be coerced, see Details for the description of #' available methods #' @param amap data.frame with attribute copy/rename rules, see #' \code{\link{attrmap}} #' @param directed logical, whether the created network should be directed #' @param vertices NULL or data frame, optional data frame containing vertex #' attributes #' @param \dots other arguments from/to other methods #' @return Object of class "network". #' @seealso \code{\link[igraph]{graph.data.frame}} #' #' \code{\link{asIgraph}} for conversion in the other direction. #' #' @export #' #' @example examples/asNetwork.R #' asNetwork <- function(x, ...) UseMethod("asNetwork") #' @method asNetwork data.frame #' @export #' @rdname asNetwork asNetwork.data.frame <- function(x, directed=TRUE, vertices=NULL, ...) { edb <- validateEL( as.data.frame(x) ) # got vertex DB? if(!is.null(vertices)) { vdb <- validateVDB( as.data.frame(vertices) ) stopifnot(validNetDB(edb, vdb)) } # number of vertices if(is.null(vertices)) nv <- length(unique(c(edb[,1], edb[,2]))) else nv <- nrow(vertices) # create an empty network object rval <- network::network.initialize(nv, directed=directed, hyper=FALSE, multiple=any(duplicated(edb[,1:2])), loops=any(edb[,1] == edb[,2])) # add edges rval <- network::add.edges(rval, as.list(edb[,1]), as.list(edb[,2])) # add edge attribbutes if( ncol(edb) > 2) for(i in seq(3, ncol(edb))) { rval <- network::set.edge.attribute(rval, attrname=names(edb)[i], value=edb[,i]) } # vertex attributes if( !is.null(vertices) && ncol(vertices) > 1 ) { for( i in seq(2, ncol(vdb)) ) { rval <- network::set.vertex.attribute(rval, attrname=names(vdb)[i], value=vdb[,i]) } } rval } #' @method asNetwork igraph #' @export #' @rdname asNetwork asNetwork.igraph <- function(x, amap=attrmap(), ...) { object <- x na <- dumpAttr(object, "network") l <- asDF(object) ### prepare edge attributes eats <- attrmapmat("igraph", "network", "edge", db=amap) if( nrow(eats) > 0 ) { # drop some todrop <- eats[ is.na(eats[,"toattr"]) , "fromattr" ] edges <- l$edges[ !( names(l$edges) %in% todrop ) ] # rename some names(edges) <- recode(names(edges), eats) } else { edges <-l$edges } ### prepare vertex attributes vats <- attrmapmat("igraph", "network", "vertex", db=amap) if( nrow(vats) > 0 ) { # drop some todrop <- vats[ is.na(vats[,"toattr"]) , "fromattr" ] vertexes <- l$vertexes[ !( names(l$vertexes) %in% todrop ) ] # rename some names(vertexes) <- recode(names(vertexes), vats) } else { vertexes <- l$vertexes } ### make 'igraph' object rval <- asNetwork( edges, directed = igraph::is_directed(object), multiple = igraph::any_multiple(object), loops = igraph::any_loop(object), vertices = vertexes, ...) ### apply/rename/drop network attributes nats <- attrmapmat("igraph", "network", "network", db=amap) if( nrow(nats) > 0 ) { todrop <- nats[ is.na(nats[,"toattr"]) , "fromattr" ] na <- na[ !( names(na) %in% todrop ) ] names(na) <- recode(names(na), nats) } if( length(na) > 0 ) { for( naname in names(na) ) network::set.network.attribute(rval, naname, na[[naname]]) } if( is.function(network::get.network.attribute(rval, "layout")) ) warning("network attribute 'layout' is a function, print the result might give errors") rval } intergraph/MD50000644000176200001440000000417314556752432012747 0ustar liggesusers34616adb910e845c5296dfc2f560457d *DESCRIPTION 6cf88f8c077440e04e2e520a36012954 *NAMESPACE 92d06cdec397d6f3dea98b4432e206a7 *R/asDF.R 8ae0b49a84942c4c322dbc5d79df3fd8 *R/asIgraph.R 6a7f6bc446f49a093aa1ac85512dbd5e *R/asNetwork.R 9eb99901c37eae3f594a6c1edd2c189d *R/attrmap.R ead3a4672fff4399ff609f4d15d8b537 *R/dumpAttr.R 568472dddfc792a101de37a798e88942 *R/exNetwork.R cea83041b85fc372f39d80565d80601c *R/intergraph-package.R 562ec6ffeba42b03e25918943a4e9856 *R/netcompare.R 03dd78f463bb74e7aab8bc78c2c1c53b *R/recode.R 0f06ca5e4a89d4b2f147e16ffb1271a2 *R/s3common.R 55ee4e1e0ba7593edad568e1e2323c87 *R/validnet.R 15abadb3b23ba7013592debd4120b17c *README.md e6297a2b7cd4a946501e96e2682c7abb *build/vignette.rds 5bbd63e4b9b6fbc34aa823ece4d4f752 *data/exIgraph.rda 38edbd3925118c09eee4f67783440534 *data/exIgraph2.rda b894dad1d415ffcf6f2ae888195dbc1a *data/exNetwork.rda df753176631c1bae5e750ee6198fe13f *data/exNetwork2.rda 3db86dc7ec35ff882248b16208e06a04 *inst/CITATION 06154629ddf0dd0b089481e407f3bb28 *inst/doc/howto.R 2d79c58ffc926a7e6e2bd3a313471edf *inst/doc/howto.Rmd 51dda7fc31bfcd9dcf2fcaf99b334b1c *inst/doc/howto.html ceddc133ad5f557d1f366533c6e19287 *man/asDF.Rd 81b73294794f4f89885708633b6592de *man/asIgraph.Rd 9c99d2e262ea3129e820eea98a210b6a *man/asNetwork.Rd 28f20544a618923ced685954266850ec *man/attrmap.Rd 666420055c7937552fb119af757c3340 *man/dumpAttr.Rd 34e423ea2dbe9699cbd8df103a286306 *man/exNetwork.Rd 21778b8ed4cfdcda6dbd1c26a033ac4f *man/intergraph-package.Rd 71505d3c947231ba6d9b62a69eb3c315 *man/netcompare.Rd 1931821011571a0e71c34111c9f0460b *tests/testthat.R 39194d6e9992d1aadec1ce3a14749712 *tests/testthat/_snaps/netcompare.md 0d78073aa12bea2c33b681e9fcef558a *tests/testthat/test-asDF.R 190102d3f1b10426172f21ae9d7bcc11 *tests/testthat/test-asIgraph.R aae337c1df887fd292a857ec3298c513 *tests/testthat/test-asNetwork.R ddd8e0c93f16a4f8a53e43d43f3e872e *tests/testthat/test-attrmap.R 4616f5d9ddfdff2ec76e8f1ab557f699 *tests/testthat/test-github_issues.R 8b50f61996384c54343b3d455b683d61 *tests/testthat/test-netcompare.R 5c273a0ec02c51127fe4f500ea92cbea *tests/testthat/test-validnet.R 2d79c58ffc926a7e6e2bd3a313471edf *vignettes/howto.Rmd intergraph/inst/0000755000176200001440000000000014556217121013376 5ustar liggesusersintergraph/inst/doc/0000755000176200001440000000000014556217121014143 5ustar liggesusersintergraph/inst/doc/howto.R0000644000176200001440000000642614556217121015436 0ustar liggesusers## ----setup, include=FALSE----------------------------------------------------- library(intergraph) library(knitr) set.seed(123) ## ----packages----------------------------------------------------------------- library(intergraph) library(network) library(igraph) ## ----summarize-igraph--------------------------------------------------------- summary(exIgraph) summary(exIgraph2) ## ----summarize-network-------------------------------------------------------- exNetwork exNetwork2 ## ----network2igraph----------------------------------------------------------- # check class of 'exNetwork' class(exNetwork) # convert to 'igraph' g <- asIgraph(exNetwork) # check class of the result class(g) ## ----------------------------------------------------------------------------- el.g <- get.edgelist(g) el.n <- as.matrix(exNetwork, "edgelist") identical( as.numeric(el.g), as.numeric(el.n)) ## ----igraph2network----------------------------------------------------------- net <- asNetwork(exIgraph) ## ----------------------------------------------------------------------------- el.g2 <- get.edgelist(exIgraph) el.n2 <- as.matrix(net, "edgelist") identical( as.numeric(el.g2), as.numeric(el.n2)) ## ----attrmap-defaults--------------------------------------------------------- attrmap() ## ----attrmap-example-rules---------------------------------------------------- new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na", tocls="igraph", toattr=NA, stringsAsFactors=FALSE) # combine with the default rules rules <- rbind( attrmap(), new_rule ) rules ## ----attrmap-example---------------------------------------------------------- (ig1 <- asIgraph(exNetwork)) (ig2 <- asIgraph(exNetwork, amap=rules)) # check if "na" was dropped "na" %in% igraph::vertex_attr_names(ig1) "na" %in% igraph::vertex_attr_names(ig2) ## ----asDF--------------------------------------------------------------------- l <- asDF(exIgraph) str(l) ## ----show-edgedb-------------------------------------------------------------- l$edges ## ----show-vertexdb------------------------------------------------------------ l$vertexes ## ----fromdf------------------------------------------------------------------- z <- asNetwork(l$edges, directed=TRUE, l$vertexes) z ## ----showdata-code,eval=FALSE------------------------------------------------- # layout(matrix(1:4, 2, 2, byrow=TRUE)) # op <- par(mar=c(1,1,2,1)) # # compute layout # coords <- layout.fruchterman.reingold(exIgraph) # plot(exIgraph, main="exIgraph", layout=coords) # plot(exIgraph2, main="exIgraph2", layout=coords) # plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords) # plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords) # par(op) ## ----showdata-pic, ref.label="showdata-code",echo=FALSE,fig.height=10,fig.width=10---- layout(matrix(1:4, 2, 2, byrow=TRUE)) op <- par(mar=c(1,1,2,1)) # compute layout coords <- layout.fruchterman.reingold(exIgraph) plot(exIgraph, main="exIgraph", layout=coords) plot(exIgraph2, main="exIgraph2", layout=coords) plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords) plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords) par(op) ## ----session_info------------------------------------------------------------- sessionInfo() intergraph/inst/doc/howto.html0000644000176200001440000034654314556217121016210 0ustar liggesusers Short intergraph tutorial

Short intergraph tutorial

Michał Bojanowski


“Intergraph” is an R package with coercion routines for netowrk data objects. For more information, see

This is a short tutorial showing how to use functions in package “intergraph” using some example network data contained in the package.

1 Loading example data

To show the data, first load the packages.

library(intergraph)
library(network)
## 
## 'network' 1.18.2 (2023-12-04), part of the Statnet Project
## * 'news(package="network")' for changes since last version
## * 'citation("network")' for citation information
## * 'https://statnet.org' for help, support, and other information
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:network':
## 
##     %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
##     get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
##     is.directed, list.edge.attributes, list.vertex.attributes,
##     set.edge.attribute, set.vertex.attribute
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union

Now, these are the summaries of the “igraph” objects:

summary(exIgraph)
## IGRAPH 258c8b4 D--- 15 11 -- 
## + attr: label (v/c), label (e/c)
summary(exIgraph2)
## IGRAPH 66a1bae U--- 15 11 -- 
## + attr: label (v/c), label (e/c)

These are the summaries of the “network” objects:

exNetwork
##  Network attributes:
##   vertices = 15 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 11 
##     missing edges= 0 
##     non-missing edges= 11 
## 
##  Vertex attribute names: 
##     label vertex.names 
## 
##  Edge attribute names: 
##     label
exNetwork2
##  Network attributes:
##   vertices = 15 
##   directed = FALSE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 11 
##     missing edges= 0 
##     non-missing edges= 11 
## 
##  Vertex attribute names: 
##     label vertex.names 
## 
##  Edge attribute names: 
##     label

More information is available in the Appendix.

2 Functions asNetwork and asIgraph

Conversion of network objects between classes “network” and “igraph” can be performed using functions asNetwork and asIgraph.

2.1 network => igraph

Converting “network” objects to “igraph” is done by calling function asIgraph on a “network” object:

# check class of 'exNetwork'
class(exNetwork)
## [1] "network"
# convert to 'igraph'
g <- asIgraph(exNetwork)
# check class of the result
class(g)
## [1] "igraph"

Check if edgelists of the objects are identical

el.g <- get.edgelist(g)
## Warning: `get.edgelist()` was deprecated in igraph 2.0.0.
## ℹ Please use `as_edgelist()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
el.n <- as.matrix(exNetwork, "edgelist")
identical( as.numeric(el.g), as.numeric(el.n))
## [1] TRUE

2.2 igraph => network

Converting “igraph” objects to “network” is done by calling function asNetwork on an “igraph” object:

net <- asNetwork(exIgraph)

Note the warning because of a “non-standard” network attribute layout, which is a function. Printing “network” objects does not handle non-standard attributes very well. However, all the data and attributes are copied correctly.

Check if edgelists of the objects are identical

el.g2 <- get.edgelist(exIgraph)
el.n2 <- as.matrix(net, "edgelist")
identical( as.numeric(el.g2), as.numeric(el.n2))
## [1] TRUE

2.3 Handling attributes

Objects of class “igraph” and “network”, apart from storing actual network data (vertexes and edges), allow for adding attributes of vertexes, edges, and attributes of the network as a whole (called “network attributes” or “graph attributes” in the nomenclatures of packages “network” and “igraph” respectively).

Vertex and edge attributes are used by “igraph” and “network” in a largely similar fashion. However, network-level attributes are used differently. Objects of class “network” use network-level attributes to store various metadata, e.g., network size, whether the network is directed, is bipartite, etc. In “igraph” this information is stored separately.

The above difference affects the way the attributes are copied when we convert “network” and “igraph” objects into one another.

Both functions asNetwork and asIgraph have an additional argument attrmap that is used to specify how vertex, edge, and network attributes are copied. The attrmap argument requires a data frame. Rows of that data frame specify rules of copying/renaming different attributes. The data frame should have the following columns (all of class “character”):

  • type: one of “network”, “vertex” or “edge”, whether the rule applies to network, vertex or edge attribute.
  • fromslc: name of the which we are converting from
  • fromattr: name of the attribute in the object we are converting from
  • tocls: name of the class of the object we are converting to
  • toattr: name of the attribute in the object we are converting to

The default rules are returned by a function attrmap(), these are:

attrmap()
##      type fromcls  fromattr   tocls       toattr
## 1 network network  directed  igraph         <NA>
## 2 network network bipartite  igraph         <NA>
## 3 network network     loops  igraph         <NA>
## 4 network network     mnext  igraph         <NA>
## 5 network network  multiple  igraph         <NA>
## 6 network network         n  igraph         <NA>
## 7 network network     hyper  igraph         <NA>
## 8  vertex  igraph      name network vertex.names

For example, the last row specifies a rule that when an object of class “igraph” is converted to class “network”, then a vertex attribute name in the “igraph” object will be copied to a vertex attribute called vertex.names in the resulting object of class “network.

If the column toattr contains an NA, that means that the corresponding attribute is not copied. For example, the first row specifies a rule that when an object of class “network” is converted to class “igraph”, then a network attribute directed in the “network” object is not copied to the resulting object of class “igraph”.

Users can customize the rules, or add new ones, by constructing similar data frames and supplying them through argument attrmap to functions asIgraph and asNetwork.

As an example let us set the option to always drop the na vertex attribute. First, we need to setup the rule by adding an extra row to the data frame returned by attrmap:

new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na",
                       tocls="igraph", toattr=NA,
                       stringsAsFactors=FALSE)
# combine with the default rules
rules <- rbind( attrmap(), new_rule )
rules
##      type fromcls  fromattr   tocls       toattr
## 1 network network  directed  igraph         <NA>
## 2 network network bipartite  igraph         <NA>
## 3 network network     loops  igraph         <NA>
## 4 network network     mnext  igraph         <NA>
## 5 network network  multiple  igraph         <NA>
## 6 network network         n  igraph         <NA>
## 7 network network     hyper  igraph         <NA>
## 8  vertex  igraph      name network vertex.names
## 9  vertex network        na  igraph         <NA>

Now we can use it with asIgraph:

(ig1 <- asIgraph(exNetwork))
## IGRAPH 8fa0936 D--- 15 11 -- 
## + attr: label (v/c), na (v/l), vertex.names (v/c), label (e/c), na
## | (e/l)
## + edges from 8fa0936:
##  [1]  2-> 1  3-> 1  4-> 1  5-> 1  6-> 7  8-> 9 10->11 11->12 14->12 12->13
## [11] 13->14
(ig2 <- asIgraph(exNetwork, amap=rules))
## IGRAPH d22eb45 D--- 15 11 -- 
## + attr: label (v/c), vertex.names (v/c), label (e/c), na (e/l)
## + edges from d22eb45:
##  [1]  2-> 1  3-> 1  4-> 1  5-> 1  6-> 7  8-> 9 10->11 11->12 14->12 12->13
## [11] 13->14
# check if "na" was dropped
"na" %in% igraph::vertex_attr_names(ig1)
## [1] TRUE
"na" %in% igraph::vertex_attr_names(ig2)
## [1] FALSE

3 Network objects to/from data frames

Function asDF can be used to convert network object (of class “igraph” or “network”) to a list of two data frames:

l <- asDF(exIgraph)
str(l)
## List of 2
##  $ edges   :'data.frame':    11 obs. of  3 variables:
##   ..$ V1   : num [1:11] 2 3 4 5 6 8 10 11 12 13 ...
##   ..$ V2   : num [1:11] 1 1 1 1 7 9 11 12 13 14 ...
##   ..$ label: chr [1:11] "ba" "ca" "da" "ea" ...
##  $ vertexes:'data.frame':    15 obs. of  2 variables:
##   ..$ intergraph_id: int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
##   ..$ label        : chr [1:15] "a" "b" "c" "d" ...

The resulting list has two components edges and vertexes. The edges component is essentially an edge list containing ego and alter ids in the first two columns. The remaining columns store edge attributes (if any). For our example data it is

l$edges
##    V1 V2 label
## 1   2  1    ba
## 2   3  1    ca
## 3   4  1    da
## 4   5  1    ea
## 5   6  7    fg
## 6   8  9    hi
## 7  10 11    jk
## 8  11 12    kl
## 9  12 13    lm
## 10 13 14    mn
## 11 14 12    nl

The vertexes component contains data on vertexes with vertex id (the same that is used in the first two column of edges) is stored in the first two columns. The remaining columns store vertex attributes (if any). For our example data it is:

l$vertexes
##    intergraph_id label
## 1              1     a
## 2              2     b
## 3              3     c
## 4              4     d
## 5              5     e
## 6              6     f
## 7              7     g
## 8              8     h
## 9              9     i
## 10            10     j
## 11            11     k
## 12            12     l
## 13            13     m
## 14            14     n
## 15            15     o

Functions asNetwork and asIgraph can also be used to create network objects from data frames such as those above. The first argument should be an edge list data frame. Optional argument vertices expectes data frames with vertex data (just like l$vertexes). Additionally we need to specify whether the edges should be interpreted as directed or not through the argument directed.

For example, to create an object of class “network” from the dataframes created above from object exIgraph we can:

z <- asNetwork(l$edges, directed=TRUE, l$vertexes)
z
##  Network attributes:
##   vertices = 15 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 11 
##     missing edges= 0 
##     non-missing edges= 11 
## 
##  Vertex attribute names: 
##     label vertex.names 
## 
##  Edge attribute names: 
##     label

This is actually what basically happens when we call asNetwork(exIgraph)


4 Appendix

4.1 Example networks

Package intergraph contains four example networks:

  • Objects exNetwork and exIgraph contain the same directed network as objects of class “network” and “igraph” respectively.
  • Objects exNetwork2 and exIgraph2 contain the same undirected network as objects of class “network” and “igraph” respectively.

All four datasets contain:

  • A vertex attribute label with vertex labels. These are letters from a to o.
  • An edge attribute label with edge labels. These are pasted letters of the adjecent nodes.

We will use them in the examples below.

Networks are shown below using the following code:

layout(matrix(1:4, 2, 2, byrow=TRUE))
op <- par(mar=c(1,1,2,1))
# compute layout
coords <- layout.fruchterman.reingold(exIgraph)
plot(exIgraph, main="exIgraph", layout=coords)
plot(exIgraph2, main="exIgraph2", layout=coords)
plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords)
plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords)
par(op)

4.2 Session information

sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=pl_PL.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=pl_PL.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=pl_PL.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=pl_PL.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Europe/Warsaw
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] igraph_2.0.1.1   network_1.18.2   knitr_1.45       intergraph_2.0-4
## 
## loaded via a namespace (and not attached):
##  [1] crayon_1.5.2         vctrs_0.6.5          cli_3.6.2           
##  [4] rlang_1.1.3          xfun_0.41            highr_0.10          
##  [7] jsonlite_1.8.8       glue_1.7.0           htmltools_0.5.7     
## [10] sass_0.4.8           fansi_1.0.6          rmarkdown_2.25      
## [13] grid_4.3.2           evaluate_0.23        jquerylib_0.1.4     
## [16] tibble_3.2.1         fastmap_1.1.1        yaml_2.3.8          
## [19] lifecycle_1.0.4      compiler_4.3.2       coda_0.19-4         
## [22] pkgconfig_2.0.3      statnet.common_4.9.0 lattice_0.22-5      
## [25] digest_0.6.34        R6_2.5.1             utf8_1.2.4          
## [28] pillar_1.9.0         magrittr_2.0.3       bslib_0.6.1         
## [31] tools_4.3.2          cachem_1.0.8
intergraph/inst/doc/howto.Rmd0000644000176200001440000002020514545555275015763 0ustar liggesusers--- title: "Short `intergraph` tutorial" author: "Michał Bojanowski" output: rmarkdown::html_vignette: toc: true number_sections: true vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{Short intergraph tutorial} %\VignetteEncoding{UTF-8} --- ```{r, setup, include=FALSE} library(intergraph) library(knitr) set.seed(123) ``` - - - "Intergraph" is an R package with coercion routines for netowrk data objects. For more information, see * Homepage on [https://mbojan.github.io/intergraph/](https://mbojan.github.io/intergraph/). * Package development pages on [https://github.com/mbojan/intergraph](https://github.com/mbojan/intergraph). This is a short tutorial showing how to use functions in package "intergraph" using some example network data contained in the package. # Loading example data To show the data, first load the packages. ```{r,packages} library(intergraph) library(network) library(igraph) ``` Now, these are the summaries of the "igraph" objects: ```{r, summarize-igraph} summary(exIgraph) summary(exIgraph2) ``` These are the summaries of the "network" objects: ```{r,summarize-network} exNetwork exNetwork2 ``` More information is available in the Appendix. # Functions `asNetwork` and `asIgraph` Conversion of network objects between classes "network" and "igraph" can be performed using functions `asNetwork` and `asIgraph`. ## network => igraph Converting "network" objects to "igraph" is done by calling function `asIgraph` on a "network" object: ```{r,network2igraph} # check class of 'exNetwork' class(exNetwork) # convert to 'igraph' g <- asIgraph(exNetwork) # check class of the result class(g) ``` Check if edgelists of the objects are identical ```{r} el.g <- get.edgelist(g) el.n <- as.matrix(exNetwork, "edgelist") identical( as.numeric(el.g), as.numeric(el.n)) ``` ## igraph => network Converting "igraph" objects to "network" is done by calling function `asNetwork` on an "igraph" object: ```{r,igraph2network} net <- asNetwork(exIgraph) ``` Note the warning because of a "non-standard" network attribute `layout`, which is a function. Printing "network" objects does not handle non-standard attributes very well. However, all the data and attributes are copied correctly. Check if edgelists of the objects are identical ```{r} el.g2 <- get.edgelist(exIgraph) el.n2 <- as.matrix(net, "edgelist") identical( as.numeric(el.g2), as.numeric(el.n2)) ``` ## Handling attributes Objects of class "igraph" and "network", apart from storing actual network data (vertexes and edges), allow for adding attributes of vertexes, edges, and attributes of the network as a whole (called "network attributes" or "graph attributes" in the nomenclatures of packages "network" and "igraph" respectively). Vertex and edge attributes are used by "igraph" and "network" in a largely similar fashion. However, network-level attributes are used differently. Objects of class "network" use network-level attributes to store various metadata, e.g., network size, whether the network is directed, is bipartite, etc. In "igraph" this information is stored separately. The above difference affects the way the attributes are copied when we convert "network" and "igraph" objects into one another. Both functions `asNetwork` and `asIgraph` have an additional argument `attrmap` that is used to specify how vertex, edge, and network attributes are copied. The `attrmap` argument requires a data frame. Rows of that data frame specify rules of copying/renaming different attributes. The data frame should have the following columns (all of class "character"): * `type`: one of "network", "vertex" or "edge", whether the rule applies to network, vertex or edge attribute. * `fromslc`: name of the which we are *converting from* * `fromattr`: name of the attribute in the object we are converting from * `tocls`: name of the class of the object we are *converting to* * `toattr`: name of the attribute in the object we are converting to The default rules are returned by a function `attrmap()`, these are: ```{r attrmap-defaults} attrmap() ``` For example, the last row specifies a rule that when an object of class "igraph" is converted to class "network", then a vertex attribute `name` in the "igraph" object will be copied to a vertex attribute called `vertex.names` in the resulting object of class "network. If the column `toattr` contains an `NA`, that means that the corresponding attribute is not copied. For example, the first row specifies a rule that when an object of class "network" is converted to class "igraph", then a network attribute `directed` in the "network" object is *not* copied to the resulting object of class "igraph". Users can customize the rules, or add new ones, by constructing similar data frames and supplying them through argument `attrmap` to functions `asIgraph` and `asNetwork`. As an example let us set the option to always drop the `na` vertex attribute. First, we need to setup the rule by adding an extra row to the data frame returned by `attrmap`: ```{r attrmap-example-rules} new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na", tocls="igraph", toattr=NA, stringsAsFactors=FALSE) # combine with the default rules rules <- rbind( attrmap(), new_rule ) rules ``` Now we can use it with `asIgraph`: ```{r attrmap-example} (ig1 <- asIgraph(exNetwork)) (ig2 <- asIgraph(exNetwork, amap=rules)) # check if "na" was dropped "na" %in% igraph::vertex_attr_names(ig1) "na" %in% igraph::vertex_attr_names(ig2) ``` # Network objects to/from data frames Function `asDF` can be used to convert network object (of class "igraph" or "network") to a list of two data frames: ```{r asDF} l <- asDF(exIgraph) str(l) ``` The resulting list has two components `edges` and `vertexes`. The `edges` component is essentially an edge list containing ego and alter ids in the first two columns. The remaining columns store edge attributes (if any). For our example data it is ```{r show-edgedb} l$edges ``` The `vertexes` component contains data on vertexes with vertex id (the same that is used in the first two column of `edges`) is stored in the first two columns. The remaining columns store vertex attributes (if any). For our example data it is: ```{r show-vertexdb} l$vertexes ``` Functions `asNetwork` and `asIgraph` can also be used to create network objects from data frames such as those above. The first argument should be an edge list data frame. Optional argument `vertices` expectes data frames with vertex data (just like `l$vertexes`). Additionally we need to specify whether the edges should be interpreted as directed or not through the argument `directed`. For example, to create an object of class "network" from the dataframes created above from object `exIgraph` we can: ```{r fromdf} z <- asNetwork(l$edges, directed=TRUE, l$vertexes) z ``` This is actually what basically happens when we call `asNetwork(exIgraph)` - - - # Appendix ## Example networks Package intergraph contains four example networks: * Objects `exNetwork` and `exIgraph` contain the same *directed* network as objects of class "network" and "igraph" respectively. * Objects `exNetwork2` and `exIgraph2` contain the same *undirected* network as objects of class "network" and "igraph" respectively. All four datasets contain: * A vertex attribute `label` with vertex labels. These are letters from `a` to `o`. * An edge attribute `label` with edge labels. These are pasted letters of the adjecent nodes. We will use them in the examples below. Networks are shown below using the following code: ```{r showdata-code,eval=FALSE} layout(matrix(1:4, 2, 2, byrow=TRUE)) op <- par(mar=c(1,1,2,1)) # compute layout coords <- layout.fruchterman.reingold(exIgraph) plot(exIgraph, main="exIgraph", layout=coords) plot(exIgraph2, main="exIgraph2", layout=coords) plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords) plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords) par(op) ``` ```{r showdata-pic, ref.label="showdata-code",echo=FALSE,fig.height=10,fig.width=10} ``` ## Session information ```{r, session_info} sessionInfo() ``` intergraph/inst/CITATION0000644000176200001440000000037614470117115014536 0ustar liggesusersbibentry( bibtype = "Manual", title = "{intergraph}: Coercion Routines for Network Data Objects", author = "Michał Bojanowski", year = 2023, url = "https://mbojan.github.io/intergraph/", note = "R package version 2.0-3" )