├── .github ├── .gitignore └── workflows │ ├── R-CMD-check.yaml │ └── test-coverage.yaml ├── revdep ├── problems.md ├── .gitignore ├── cran.md ├── README.md └── failures.md ├── .gitignore ├── data ├── exIgraph.rda ├── exIgraph2.rda ├── exNetwork.rda └── exNetwork2.rda ├── tests ├── testthat.R └── testthat │ ├── test-netcompare.R │ ├── test-attrmap.R │ ├── test-github_issues.R │ ├── _snaps │ └── netcompare.md │ ├── test-asDF.R │ ├── test-validnet.R │ ├── test-asIgraph.R │ └── test-asNetwork.R ├── .Rbuildignore ├── examples ├── asDF.R ├── asNetwork.R ├── dumpAttr.R ├── asIgraph.R ├── package-intergraph.R └── exNetwork.R ├── inst └── CITATION ├── codecov.yml ├── R ├── recode.R ├── s3common.R ├── intergraph-package.R ├── exNetwork.R ├── validnet.R ├── dumpAttr.R ├── attrmap.R ├── asDF.R ├── asIgraph.R ├── asNetwork.R └── netcompare.R ├── intergraph.Rproj ├── NAMESPACE ├── DESCRIPTION ├── README.md ├── man ├── dumpAttr.Rd ├── attrmap.Rd ├── netcompare.Rd ├── asNetwork.Rd ├── intergraph-package.Rd ├── asDF.Rd ├── asIgraph.Rd └── exNetwork.Rd ├── data-raw └── example_data.R ├── NEWS.md └── vignettes └── howto.Rmd /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /revdep/problems.md: -------------------------------------------------------------------------------- 1 | *Wow, no problems at all. :)* -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /data/exIgraph.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbojan/intergraph/HEAD/data/exIgraph.rda -------------------------------------------------------------------------------- /data/exIgraph2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbojan/intergraph/HEAD/data/exIgraph2.rda -------------------------------------------------------------------------------- /data/exNetwork.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbojan/intergraph/HEAD/data/exNetwork.rda -------------------------------------------------------------------------------- /data/exNetwork2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbojan/intergraph/HEAD/data/exNetwork2.rda -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(intergraph) 3 | 4 | test_check("intergraph") 5 | -------------------------------------------------------------------------------- /revdep/.gitignore: -------------------------------------------------------------------------------- 1 | checks 2 | library 3 | checks.noindex 4 | library.noindex 5 | cloud.noindex 6 | data.sqlite 7 | *.html 8 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^data-raw$ 2 | ^examples$ 3 | ^NEWS\.md$ 4 | ^.*\.Rproj$ 5 | ^\.Rproj\.user$ 6 | ^\.github$ 7 | ^revdep$ 8 | ^codecov\.yml$ 9 | -------------------------------------------------------------------------------- /examples/asDF.R: -------------------------------------------------------------------------------- 1 | # using method for 'network' objects 2 | d1 <- asDF(exNetwork) 3 | str(d1) 4 | 5 | # using method for 'igraph' objects 6 | d2 <- asDF(exIgraph) 7 | str(d2) 8 | -------------------------------------------------------------------------------- /revdep/cran.md: -------------------------------------------------------------------------------- 1 | ## revdepcheck results 2 | 3 | We checked 23 reverse dependencies (19 from CRAN + 4 from Bioconductor), comparing R CMD check results across CRAN and dev versions of this package. 4 | 5 | * We saw 0 new problems 6 | * We failed to check 0 packages 7 | 8 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | bibentry( 2 | bibtype = "Manual", 3 | title = "{intergraph}: Coercion Routines for Network Data Objects", 4 | author = "Michał Bojanowski", 5 | year = 2023, 6 | url = "https://mbojan.github.io/intergraph/", 7 | note = "R package version 2.0-3" 8 | ) 9 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /R/recode.R: -------------------------------------------------------------------------------- 1 | # given a vector and a recode matrix replace the values in 'x' that match in 2 | # 'm[,1]' with the corresponding replacements from 'm[,2]' 3 | 4 | recode <- function(x, mat) 5 | { 6 | i <- match(x, mat[,1]) 7 | i2 <- which(!is.na(i)) 8 | i <- i[i2] 9 | rval <- x 10 | rval[i2] <- mat[,2][i] 11 | rval 12 | } 13 | -------------------------------------------------------------------------------- /intergraph.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | -------------------------------------------------------------------------------- /examples/asNetwork.R: -------------------------------------------------------------------------------- 1 | # require package 'network' as 'asNetwork' generic is defined there 2 | if(require(network, quietly=TRUE)) 3 | { 4 | ### demonstrating method for data frames 5 | l <- asDF(exNetwork) 6 | g <- asNetwork( l$edges, vertices=l$vertexes) 7 | stopifnot(all.equal(g, exNetwork)) 8 | 9 | ### method for igraph objects 10 | ig <- asNetwork(exIgraph) 11 | identical( as.numeric(as.matrix(g, "adjacency")), 12 | as.numeric(as.matrix(ig, "adjacency"))) 13 | } 14 | 15 | -------------------------------------------------------------------------------- /examples/dumpAttr.R: -------------------------------------------------------------------------------- 1 | # using 'igraph' object 2 | l <- dumpAttr( exIgraph ) # all attributes 3 | identical( dumpAttr(exIgraph, "network"), l$network ) 4 | identical( dumpAttr(exIgraph, "vertex"), l$vertex ) 5 | identical( dumpAttr(exIgraph, "edge"), l$edge ) 6 | 7 | # using 'network' object 8 | l <- dumpAttr( exNetwork ) # all attributes 9 | identical( dumpAttr(exNetwork, "network"), l$network ) 10 | identical( dumpAttr(exNetwork, "vertex"), l$vertex ) 11 | identical( dumpAttr(exNetwork, "edge"), l$edge ) 12 | -------------------------------------------------------------------------------- /tests/testthat/test-netcompare.R: -------------------------------------------------------------------------------- 1 | test_that("netcompare just works",{ 2 | expect_no_condition( 3 | r <- netcompare(exIgraph, exIgraph) 4 | ) 5 | expect_snapshot(print(r)) 6 | }) 7 | 8 | 9 | 10 | 11 | 12 | # compareAlist ------------------------------------------------------------ 13 | 14 | test_that("compareAlist returns NA when intersection of names sets is empty", { 15 | l1 <- list(a=2, b=1:5, c=1:5, d=3) 16 | l2 <- list(B=1:5, C=5:1) 17 | expect_identical(compareAlist(l1, l2), as.character(NA)) 18 | } ) 19 | -------------------------------------------------------------------------------- /tests/testthat/test-attrmap.R: -------------------------------------------------------------------------------- 1 | # Custom attrmap specifications ------------------------------------------- 2 | 3 | test_that("'na' vertex attribute can be dropped when going network->igraph", { 4 | net <- asNetwork(exIgraph) 5 | a <- attrmap() 6 | r <- data.frame( 7 | type="vertex", 8 | fromcls="network", 9 | fromattr="na", 10 | tocls="igraph", 11 | toattr=NA, 12 | stringsAsFactors=FALSE 13 | ) 14 | aa <- rbind(a, r) 15 | g <- asIgraph(net, amap=aa) 16 | expect_false( "na" %in% igraph::vertex_attr_names(g)) 17 | } ) 18 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(asDF,igraph) 4 | S3method(asDF,network) 5 | S3method(asIgraph,data.frame) 6 | S3method(asIgraph,network) 7 | S3method(asNetwork,data.frame) 8 | S3method(asNetwork,igraph) 9 | S3method(dumpAttr,igraph) 10 | S3method(dumpAttr,network) 11 | S3method(igDirected,igraph) 12 | S3method(igDirected,network) 13 | S3method(igEcount,igraph) 14 | S3method(igEcount,network) 15 | S3method(igVcount,igraph) 16 | S3method(igVcount,network) 17 | S3method(print,netcompare) 18 | S3method(print,netcomparea) 19 | export(asDF) 20 | export(asIgraph) 21 | export(asNetwork) 22 | export(attrmap) 23 | export(dumpAttr) 24 | export(netcompare) 25 | importFrom(network,add.edges.network) 26 | importFrom(network,as.matrix.network) 27 | -------------------------------------------------------------------------------- /tests/testthat/test-github_issues.R: -------------------------------------------------------------------------------- 1 | # GitHub issue 1 ---------------------------------------------------------- 2 | 3 | test_that("NAs are preserved in edge attributes", { 4 | g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) 5 | igraph::E(g)$label <- c(1,2,3,NA,4) 6 | net <- asNetwork(g) 7 | expect_true(any( is.na(network::get.edge.attribute(net, "label")))) 8 | ig <- asIgraph(net) 9 | expect_true(any( is.na(igraph::edge_attr(ig, "label")))) 10 | } ) 11 | 12 | test_that("NAs are preserved in vertex attributes", { 13 | g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) 14 | igraph::V(g)$label <- c(1,2,3,NA,4) 15 | net <- asNetwork(g) 16 | expect_true(any( is.na(network::get.vertex.attribute(net, "label")))) 17 | ig <- asIgraph(net) 18 | expect_true(any( is.na(igraph::vertex_attr(ig, "label")))) 19 | } ) 20 | -------------------------------------------------------------------------------- /R/s3common.R: -------------------------------------------------------------------------------- 1 | 2 | #============================================================================ 3 | # common functions 4 | 5 | igVcount <- function(x, ...) UseMethod("igVcount") 6 | 7 | #' @export 8 | igVcount.igraph <- function(x, ...) 9 | igraph::vcount(x) 10 | 11 | #' @export 12 | igVcount.network <- function(x, ...) 13 | network::network.size(x) 14 | 15 | igEcount <- function(x, ...) UseMethod("igEcount") 16 | 17 | #' @export 18 | igEcount.igraph <- function(x, ...) 19 | igraph::ecount(x) 20 | 21 | #' @export 22 | igEcount.network <- function(x, ...) 23 | network::network.edgecount(x) 24 | 25 | igDirected <- function(x) UseMethod("igDirected") 26 | 27 | #' @export 28 | igDirected.igraph <- function(x) { 29 | igraph::is_directed(x) 30 | } 31 | 32 | #' @export 33 | igDirected.network <- function(x) { 34 | network::is.directed(x) 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: intergraph 2 | Type: Package 3 | Title: Coercion Routines for Network Data Objects 4 | Version: 2.0-5 5 | Authors@R: person("Michał", "Bojanowski", role=c("aut", "cre"), 6 | email="michal2992@gmail.com", comment=c(ORCID="0000-0001-7503-852X")) 7 | Description: Functions implemented in this package allow to coerce (i.e. 8 | convert) network data between classes provided by other R packages. 9 | Currently supported classes are those defined in packages: network and 10 | igraph. 11 | URL: https://mbojan.github.io/intergraph/ 12 | BugReports: https://github.com/mbojan/intergraph/issues 13 | Imports: 14 | network (>= 1.4-2), 15 | igraph (>= 0.6-0), 16 | utils 17 | Suggests: 18 | rmarkdown, 19 | knitr, 20 | roxygen2, 21 | testthat, 22 | tibble 23 | VignetteBuilder: knitr 24 | License: GPL-3 25 | LazyLoad: yes 26 | LazyData: yes 27 | RoxygenNote: 7.3.1 28 | Encoding: UTF-8 29 | Depends: 30 | R (>= 2.10) 31 | Config/testthat/edition: 3 32 | -------------------------------------------------------------------------------- /tests/testthat/_snaps/netcompare.md: -------------------------------------------------------------------------------- 1 | # netcompare just works 2 | 3 | Code 4 | print(r) 5 | Output 6 | 7 | Identical adjacency matrices: 8 | TRUE 9 | 10 | Network-level features: 11 | target current 12 | vcount 15 15 13 | ecount 11 11 14 | directed TRUE TRUE 15 | 16 | Presence of network-level attributes 17 | target current 18 | n 0 0 19 | Common attributes comparison (TRUE=identical) 20 | No common attributes 21 | 22 | Presence of vertex-level attributes 23 | target current 24 | n 1 1 25 | label TRUE TRUE 26 | Common attributes comparison (TRUE=identical) 27 | label : TRUE 28 | 29 | Presence of edge-level attributes 30 | target current 31 | n 1 1 32 | label TRUE TRUE 33 | Common attributes comparison (TRUE=identical) 34 | label : TRUE 35 | 36 | -------------------------------------------------------------------------------- /examples/asIgraph.R: -------------------------------------------------------------------------------- 1 | ### using 'asIgraph' on objects of class 'network' 2 | 3 | g <- asIgraph(exNetwork) 4 | 5 | # compare adjacency matrices 6 | netmat <- as.matrix(exNetwork, "adjacency") 7 | imat <- as.matrix(g, "adjacency") 8 | # drop the dimnames in 'netmat' 9 | dimnames(netmat) <- NULL 10 | # compare 11 | identical( netmat, imat ) 12 | 13 | 14 | ### using 'asIgraph' on data.frames 15 | 16 | # data frame with vertex ids and vertex attributes 17 | v <- 1:4 18 | vd <- data.frame(id = v + 5, label=letters[1:4]) 19 | print(vd) 20 | 21 | # edge list (first two columns) and edge attributes 22 | e <- c(1,2, 2,3, 3,4, 4,1) 23 | ed <- data.frame(id1 = e[seq(1,8, by=2)]+5, id2=e[seq(2, 8, by=2)]+5, a=letters[1:4]) 24 | print(ed) 25 | 26 | # build the network 27 | # without vertex attributes 28 | g <- asIgraph(ed, directed=FALSE) 29 | # with vertex attributes 30 | gv <- asIgraph(ed, vertices=vd, directed=FALSE) 31 | 32 | # NOTE: Even though vertex ids start at 6 the network has 4 nodes: 33 | range(vd$id) # min and max of node ids 34 | if(require(igraph)) igraph::vcount(gv) # number of nodes in 'gv' 35 | -------------------------------------------------------------------------------- /examples/package-intergraph.R: -------------------------------------------------------------------------------- 1 | # example of converting objects between classes 'network' and 'igraph' 2 | # needs packages igraph and network attached 3 | if( require(network) & require(igraph) ) 4 | { 5 | 6 | ### convert 'network' -> 'igraph' 7 | 8 | # example network as object of class "network" 9 | summary(exNetwork) 10 | 11 | # convert to class "igraph" 12 | g <- asIgraph(exNetwork) 13 | 14 | # check if 'exNetwork' and 'g' are the same 15 | # (dropping some aux attributes) 16 | all.equal( structure(as.matrix(exNetwork, "edgelist"), n=NULL, vnames=NULL), 17 | igraph::as_edgelist(g) ) 18 | 19 | # compare results using 'netcompare' 20 | netcompare(exNetwork, g) 21 | 22 | 23 | ### convert 'igraph' -> 'network' 24 | 25 | # example network as object of class "igraph" 26 | summary(exIgraph) 27 | 28 | # convert to class "network" 29 | gg <- asNetwork(exIgraph) 30 | 31 | # check if they are the same 32 | # (dropping some attributes) 33 | all.equal( get.edgelist(exIgraph), 34 | structure(as.matrix(gg, "edgelist"), n=NULL, vnames=NULL)) 35 | netcompare(exIgraph, gg) 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `intergraph`: Coercion Routines for Network Data Objects 2 | 3 | 4 | [![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) 5 | [![CRAN status](https://www.r-pkg.org/badges/version/intergraph)](https://CRAN.R-project.org/package=intergraph) 6 | [![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/intergraph)](https://www.r-pkg.org/pkg/intergraph) 7 | [![DOI](https://zenodo.org/badge/doi/10.5281/zenodo.19148.svg)](https://dx.doi.org/10.5281/zenodo.19148) 8 | [![Codecov test coverage](https://codecov.io/gh/mbojan/intergraph/branch/master/graph/badge.svg)](https://app.codecov.io/gh/mbojan/intergraph?branch=master) 9 | 10 | 11 | This is an R package implementing methods for converting network data objects 12 | between classes defined in other packages. Currently supported classes: 13 | network, igraph. 14 | 15 | See projects web page on [mbojan.github.io/intergraph](https://mbojan.github.io/intergraph/). 16 | 17 | 18 | 19 | # Install 20 | 21 | Install stable version from CRAN or development version with 22 | 23 | ```r 24 | remotes::install_github("mbojan/intergraph") 25 | ``` 26 | 27 | 28 | # License 29 | 30 | GPL-3. 31 | 32 | -------------------------------------------------------------------------------- /R/intergraph-package.R: -------------------------------------------------------------------------------- 1 | #' Coercion Routines for Network Data Objects 2 | #' 3 | #' This package contains methods for coercion between various classes used to 4 | #' represent network data in R. 5 | #' 6 | #' Functions implemented in this package allow to coerce (i.e. convert) network 7 | #' data between classes provided by other R packages. Currently supported 8 | #' classes are: "network" from package \pkg{network}, "igraph" from package 9 | #' \pkg{igraph}. 10 | #' 11 | #' The main functions are: 12 | #' \itemize{ 13 | #' \item \code{\link{asNetwork}} and its methods to create objects of class 14 | #' "network". 15 | #' 16 | #' \item \code{\link{asIgraph}} and its methods to create objects of class 17 | #' "igraph". 18 | #' } 19 | #' See their help pages for more information and examples. 20 | #' 21 | #' As all the supported packages are written using S3 methods, so are the 22 | #' methods in this package. 23 | #' 24 | #' If you find this package useful in your work please cite it. Type 25 | #' \code{citation(package="intergraph")} for the information how to do that. 26 | #' 27 | #' @importFrom network as.matrix.network 28 | #' @importFrom network add.edges.network 29 | #' 30 | #' @name intergraph-package 31 | #' 32 | #' @author Written and maintained by Michal Bojanowski \email{m.bojanowski@@icm.edu.pl}. 33 | #' 34 | #' @keywords package 35 | #' 36 | #' @example examples/package-intergraph.R 37 | "_PACKAGE" 38 | -------------------------------------------------------------------------------- /man/dumpAttr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dumpAttr.R 3 | \name{dumpAttr} 4 | \alias{dumpAttr} 5 | \alias{dumpAttr.network} 6 | \alias{dumpAttr.igraph} 7 | \title{Dump network attributes to a list} 8 | \usage{ 9 | dumpAttr(x, ...) 10 | 11 | \method{dumpAttr}{network}(x, type = c("all", "network", "vertex", "edge"), ...) 12 | 13 | \method{dumpAttr}{igraph}(x, type = c("all", "network", "vertex", "edge"), ...) 14 | } 15 | \arguments{ 16 | \item{x}{network object} 17 | 18 | \item{\dots}{other arguments from/to other methods} 19 | 20 | \item{type}{character, type of attributes to dump} 21 | } 22 | \value{ 23 | If \code{type} is one of "network", "vertex" or "edge" then a list of 24 | corresponding attributes. 25 | 26 | If \code{type} is "all" then lists of lists of attributes. 27 | } 28 | \description{ 29 | Given a network return a list of all the attributes. 30 | } 31 | \examples{ 32 | # using 'igraph' object 33 | l <- dumpAttr( exIgraph ) # all attributes 34 | identical( dumpAttr(exIgraph, "network"), l$network ) 35 | identical( dumpAttr(exIgraph, "vertex"), l$vertex ) 36 | identical( dumpAttr(exIgraph, "edge"), l$edge ) 37 | 38 | # using 'network' object 39 | l <- dumpAttr( exNetwork ) # all attributes 40 | identical( dumpAttr(exNetwork, "network"), l$network ) 41 | identical( dumpAttr(exNetwork, "vertex"), l$vertex ) 42 | identical( dumpAttr(exNetwork, "edge"), l$edge ) 43 | } 44 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - uses: r-lib/actions/setup-pandoc@v2 35 | 36 | - uses: r-lib/actions/setup-r@v2 37 | with: 38 | r-version: ${{ matrix.config.r }} 39 | http-user-agent: ${{ matrix.config.http-user-agent }} 40 | use-public-rspm: true 41 | 42 | - uses: r-lib/actions/setup-r-dependencies@v2 43 | with: 44 | extra-packages: any::rcmdcheck 45 | needs: check 46 | 47 | - uses: r-lib/actions/check-r-package@v2 48 | with: 49 | upload-snapshots: true 50 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage 10 | 11 | jobs: 12 | test-coverage: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: any::covr 27 | needs: coverage 28 | 29 | - name: Test coverage 30 | run: | 31 | covr::codecov( 32 | quiet = FALSE, 33 | clean = FALSE, 34 | install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package") 35 | ) 36 | shell: Rscript {0} 37 | 38 | - name: Show testthat output 39 | if: always() 40 | run: | 41 | ## -------------------------------------------------------------------- 42 | find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true 43 | shell: bash 44 | 45 | - name: Upload test results 46 | if: failure() 47 | uses: actions/upload-artifact@v3 48 | with: 49 | name: coverage-test-failures 50 | path: ${{ runner.temp }}/package 51 | -------------------------------------------------------------------------------- /R/exNetwork.R: -------------------------------------------------------------------------------- 1 | #'Sample network structure 2 | #' 3 | #'An examples of networks together with network, edge and vertex attributes 4 | #'used primarly for testing. The same networks are stored in objects of class 5 | #'\code{network} and \code{igraph}. 6 | #' 7 | #'Vertices and edges has attribute \code{label}. For vertices these are simply 8 | #'letters from "a" to "o". For edges these are two-letter sequences 9 | #'corresponding to the neightboring vertices, i.e. the label for the edges 10 | #'linking nodes "b" and "c" will be "bc". The order is irrelevant. 11 | #' 12 | #'In the \code{exNetwork} object the \code{label} attribute is also copied to 13 | #'the \code{vertex.names} attribute to facilitate plotting. 14 | #' 15 | #'The \code{exIgraph} object has additional graph attribute \code{layout} so 16 | #'that by default Fruchterman-Reingold placement is used for plotting. 17 | #' 18 | #'@name exNetwork 19 | #'@aliases exNetwork exIgraph exNetwork2 exIgraph2 20 | #'@docType data 21 | #'@format \describe{ \item{exNetwork,exNetwork2}{is of class \code{network}} 22 | #'\item{exIgraph,exIgraph2}{is of class \code{igraph}} } Objects 23 | #'\code{exNetwork} and \code{exIgraph} store directed version of the network. 24 | #'Objects \code{exNetwork2} and \code{exIgraph2} store the undirected version: 25 | #'all direction information from the edges is removed. 26 | #' 27 | #'The network consists of 15 vertices and 11 edges. \itemize{ \item Vertex 1 is 28 | #'an isolate. \item Vertices 2-6 constitute a star with vertex 2 as a center. 29 | #'\item Vertices 7-8 and 9-10 make two dyads \item Vertcies 11, 12, 13,14 and 30 | #'15 make a stem-and-leaf network. } 31 | #'@keywords datasets 32 | #'@example examples/exNetwork.R 33 | NULL 34 | 35 | -------------------------------------------------------------------------------- /tests/testthat/test-asDF.R: -------------------------------------------------------------------------------- 1 | # From 'network' objects -------------------------------------------------- 2 | 3 | test_that("asDF(exNetwork) returns data frames with proper no of rows", { 4 | expect_silent( 5 | l <- asDF(exNetwork) 6 | ) 7 | # Created edgelist has appropriate number of edges 8 | expect_equal(nrow(l$edges), network::network.edgecount(exNetwork)) 9 | # Created vertex data frame has correct number of vertices 10 | expect_equal(nrow(l$vertexes), network::network.size(exNetwork)) 11 | }) 12 | 13 | test_that("asDF(exNetwork2) returns data frames with proper no of rows", { 14 | expect_silent( 15 | l <- asDF(exNetwork2) 16 | ) 17 | # Created edgelist has appropriate number of edges 18 | expect_equal( nrow(l$edges), network::network.edgecount(exNetwork)) 19 | # Created vertex data frame has correct number of vertices 20 | expect_equal( nrow(l$vertexes), network::network.size(exNetwork)) 21 | }) 22 | 23 | 24 | 25 | # From 'igraph' objects --------------------------------------------------- 26 | 27 | test_that("asDF(exIgraph) returns data frames with proper no of rows", { 28 | expect_silent( 29 | l <- asDF(exIgraph) 30 | ) 31 | # Created edgelist has appropriate number of edges 32 | expect_equal(nrow(l$edges), igraph::ecount(exIgraph)) 33 | # Created vertex data frame has correct number of vertices 34 | expect_equal(nrow(l$vertexes), igraph::vcount(exIgraph)) 35 | }) 36 | 37 | test_that("asDF(exIgraph2) returns data frames with proper no of rows", { 38 | expect_silent( 39 | l <- asDF(exIgraph2) 40 | ) 41 | # Created edgelist has appropriate number of edges 42 | expect_equal( nrow(l$edges), igraph::ecount(exIgraph2)) 43 | # Created vertex data frame has correct number of vertices 44 | expect_equal( nrow(l$vertexes), igraph::vcount(exIgraph2)) 45 | }) 46 | 47 | -------------------------------------------------------------------------------- /revdep/README.md: -------------------------------------------------------------------------------- 1 | # Platform 2 | 3 | |field |value | 4 | |:--------|:------------------------------------------| 5 | |version |R version 4.3.2 (2023-10-31 ucrt) | 6 | |os |Windows 10 x64 (build 19045) | 7 | |system |x86_64, mingw32 | 8 | |ui |RTerm | 9 | |language |en | 10 | |collate |Polish_Poland.utf8 | 11 | |ctype |Polish_Poland.utf8 | 12 | |tz |Europe/Warsaw | 13 | |date |2024-02-01 | 14 | |pandoc |2.18 @ C:\PROGRA~3\CHOCOL~1\bin\pandoc.exe | 15 | 16 | # Dependencies 17 | 18 | |package |old |new |Δ | 19 | |:--------------|:--------|:--------|:--| 20 | |intergraph |2.0-3 |2.0-4 |* | 21 | |cli |3.6.2 |3.6.2 | | 22 | |coda |0.19-4.1 |0.19-4.1 | | 23 | |cpp11 |0.4.7 |0.4.7 | | 24 | |fansi |1.0.6 |1.0.6 | | 25 | |glue |1.7.0 |1.7.0 | | 26 | |igraph |2.0.1.1 |2.0.1.1 | | 27 | |lifecycle |1.0.4 |1.0.4 | | 28 | |magrittr |2.0.3 |2.0.3 | | 29 | |network |1.18.2 |1.18.2 | | 30 | |pillar |1.9.0 |1.9.0 | | 31 | |pkgconfig |2.0.3 |2.0.3 | | 32 | |rlang |1.1.3 |1.1.3 | | 33 | |statnet.common |4.9.0 |4.9.0 | | 34 | |tibble |3.2.1 |3.2.1 | | 35 | |utf8 |1.2.4 |1.2.4 | | 36 | |vctrs |0.6.5 |0.6.5 | | 37 | 38 | # Revdeps 39 | 40 | ## Failed to check (2) 41 | 42 | |package |version |error |warning |note | 43 | |:--------|:-------|:-----|:-------|:----| 44 | |CEMiTool |? | | | | 45 | |gatom |? | | | | 46 | 47 | -------------------------------------------------------------------------------- /tests/testthat/test-validnet.R: -------------------------------------------------------------------------------- 1 | # Validating edge lists --------------------------------------------------- 2 | 3 | test_that("Valid edgelist passes validation", { 4 | df1 <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) 5 | r1 <- intergraph:::validateEL(df1) 6 | expect_equal(df1, r1) 7 | } ) 8 | 9 | test_that("Edgelist with single column throws error", { 10 | expect_error( intergraph:::validateEL( data.frame(ego=1:5))) 11 | } ) 12 | 13 | test_that("Edgelist with NA (invalid) gives warning", { 14 | df2 <- data.frame( ego = 1:5, alter = c(2,NA,2,5,4)) 15 | expect_warning( intergraph:::validateEL(df2) ) 16 | } ) 17 | 18 | 19 | 20 | # Validating vertex data frames ------------------------------------------- 21 | 22 | test_that("Valid vertex DB passes validation", { 23 | df1 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) 24 | expect_equal(df1, validateVDB(df1)) 25 | } ) 26 | 27 | 28 | test_that("Empty vertex DB throws error", { 29 | df2 <- data.frame(id=numeric(0), x=character(0)) 30 | expect_error( validateVDB(df2) ) 31 | } ) 32 | 33 | test_that("Vertex DB with duplicated ids throws error", { 34 | df3 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) 35 | df3$id[3] <- 1 36 | expect_error( validateVDB(df3)) 37 | } ) 38 | 39 | test_that("NAs in vertex ids gives warning", { 40 | df4 <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) 41 | df4$id[2] <- NA 42 | expect_warning( validateVDB(df4)) 43 | } ) 44 | 45 | 46 | 47 | 48 | 49 | # Validating vdb vs edb --------------------------------------------------- 50 | 51 | test_that("Valid edgelist and vertex DB pass validation", { 52 | edb <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) 53 | vdb <- data.frame(id=1:5, x=c(1,2,3,2,1), y=c(3,2,1,2,3)) 54 | expect_true( validNetDB(edb, vdb)) 55 | } ) 56 | 57 | 58 | test_that("Edgelist with some ids not present in VDB throw error", { 59 | elist <- data.frame( ego = 1:5, alter = c(2,3,2,5,4)) 60 | vdb <- data.frame(id=1:4, x=c(1,2,3,4), y=c(3,2,1,2)) 61 | expect_error( validNetDB(elist, vdb)) 62 | } ) 63 | -------------------------------------------------------------------------------- /man/attrmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/attrmap.R 3 | \name{attrmap} 4 | \alias{attrmap} 5 | \title{Network attribute copying/renaming table} 6 | \usage{ 7 | attrmap(newdf = NULL) 8 | } 9 | \arguments{ 10 | \item{newdf}{data.frame, new set of copy/rename rules, see Details} 11 | } 12 | \value{ 13 | If \code{newdf} is NULL, a data frame with the current copy/rename 14 | rules. Otherwise \code{newdf} are set as new rules and the old ones are 15 | returned invisibly, much like \code{\link{par}}. 16 | } 17 | \description{ 18 | Setting and retrieving rules through which network/edge/vertex attributes 19 | are copied or renamed when converting network objects. 20 | } 21 | \details{ 22 | Different classes for network data use different attribute names to store 23 | the same information. Some of the classes define attributes not used by 24 | other classes. This function is used to retrieve or set the rules in which 25 | these attributes are copied, renamed or dropped when converting network 26 | objects. 27 | 28 | The rules are stored in a data frame with the following columns (all of mode 29 | character): 30 | \describe{ 31 | \item{type}{type, or level of the attribute, one of "vertex", "edge" or 32 | "network"} 33 | \item{fromcls}{name of the class which is being coerced} 34 | \item{fromattr}{name of the "source" attribute} 35 | \item{tocls}{name of the class to which coercion is being done} 36 | \item{toattr}{name of the attribute in the result of coercion, or NA} 37 | } 38 | When converting network \code{x}, of class \code{xclass} to network \code{y} 39 | of class \code{yclass} the coercion method looks up the rows in this table 40 | for which \code{fromcls=xclass} and \code{tocls=yclass}. If network \code{x} 41 | has any of the attributes listed in the \code{fromattr} in the resulting 42 | subset of the table then, it is renamed to the corresponding value of 43 | \code{toattr}. If \code{toattr} is \code{NA} the attribute is dropped from 44 | the result. 45 | } 46 | \examples{ 47 | 48 | # Current values 49 | attrmap() 50 | 51 | } 52 | \seealso{ 53 | This is used by \code{\link{asIgraph}} and \code{\link{asNetwork}} 54 | } 55 | -------------------------------------------------------------------------------- /R/validnet.R: -------------------------------------------------------------------------------- 1 | # Validating data frames containing edge database (edge list with edge 2 | # attributes) and vertex data bases (vertices with vertex attributes) 3 | 4 | 5 | # Validates edge list 6 | validateEL <- function(x) 7 | { 8 | # must be data.frame 9 | stopifnot(inherits(x, "data.frame")) 10 | # at least two columns 11 | if (ncol(x) < 2) { 12 | stop("the data frame should contain at least two columns") 13 | } 14 | # Handling NAs 15 | if (any(is.na(x[,1:2]))) { 16 | warning("In first two columns of `x' `NA' elements were replaced with string \"NA\"") 17 | x[,1:2][is.na(x[,1:2])] <- "NA" 18 | } 19 | x 20 | } 21 | 22 | 23 | # validate vertex database 24 | validateVDB <- function(x) 25 | { 26 | stopifnot(inherits(x, "data.frame")) 27 | # empty data frame 28 | if(nrow(x) == 0) 29 | stop("vertex data frame has no rows") 30 | # duplicated vertex ids 31 | dups <- duplicated(x[,1]) 32 | if( any(dups) ) 33 | stop(paste("duplicated ids in vertex db:", paste(x[dups,1], collapse=", "))) 34 | # Handling NAs 35 | isna <- is.na(x[,1]) 36 | if (any(isna)) 37 | { 38 | warning("in `vertices[,1]' `NA' elements were replaced with string \"NA\"") 39 | x[isna, 1] <- "NA" 40 | } 41 | x 42 | } 43 | 44 | 45 | 46 | # validate edge DB versus vertex DB 47 | # returns TRUE or vector of warnings 48 | validNetDB <- function(edb, vdb, test=FALSE) 49 | { 50 | edb <- validateEL(edb) 51 | vdb <- validateVDB(vdb) 52 | errors <- NULL 53 | # TODO ids in el missing in vdb 54 | uvids <- unique(c(edb[,1], edb[,2])) 55 | i <- uvids %in% vdb[,1] 56 | if(!all(i)) 57 | errors <- c(errors, paste("some vertex ids in edge db are not found in vertex db:", 58 | paste(uvids[!i], collapse=", "))) 59 | # return 60 | if(is.null(errors)) return(TRUE) 61 | if(test) 62 | return(errors) 63 | else 64 | { 65 | msg <- "vertex and edge data frames are incompatible:" 66 | if(length(errors) > 1L) 67 | stop(paste(msg, paste(paste(seq_along(errors), errors, sep=": ")), 68 | collapse="\n")) 69 | else stop(msg, " ", errors) 70 | } 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /data-raw/example_data.R: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Code used to generate example networks 'exIgraph', 'exNetwork', 'exIgraph2', 3 | # 'exNetwork2'. 4 | #============================================================================ 5 | 6 | requireNamespace("usethis") 7 | library(intergraph) 8 | library(igraph) 9 | 10 | # directed igraph 11 | g <- igraph::graph( c(2,1, 3,1, 4,1, 5,1, # star 12 | 6,7, 8,9, # two dyads 13 | 10,11, 11,12, 12,13, 13,14, 14,12), # stem-leaf 14 | n=15, directed=TRUE) 15 | # add some vertex attributes 16 | vl <- letters[seq(1, vcount(g))] 17 | g <- igraph::set.vertex.attribute(g, "label", value=vl) 18 | # add some edge attributes 19 | m <- igraph::get.edgelist(g) 20 | l <- matrix(vl[m], ncol=2) 21 | el <- apply(l, 1, paste, collapse="") 22 | g <- igraph::set.edge.attribute(g, "label", value=el) 23 | rm(vl, l, m, el) 24 | exIgraph <- g 25 | 26 | # undirected igraph 27 | exIgraph2 <- igraph::as.undirected(exIgraph) 28 | exIgraph2 <- igraph::set.edge.attribute(exIgraph2, "label", 29 | value=igraph::get.edge.attribute(exIgraph, "label")) 30 | 31 | 32 | # copy as a 'network' object through adjacency matrix 33 | m <- igraph::get.adjacency(exIgraph, sparse=FALSE) 34 | g <- network::network(m, vertex.attr=list(label=V(exIgraph)$label), 35 | vertex.attrnames="label", directed=TRUE) 36 | network::set.vertex.attribute(g, "vertex.names", value=V(exIgraph)$label) 37 | network::set.edge.attribute(g, "label", igraph::get.edge.attribute(exIgraph, "label")) 38 | exNetwork <- network::network.copy(g) 39 | 40 | # copy as a 'network' object through adjacency matrix 41 | m <- igraph::get.adjacency(exIgraph2, sparse=FALSE) 42 | g <- network::network(m, vertex.attr=list(label=V(exIgraph2)$label), 43 | vertex.attrnames="label", directed=FALSE) 44 | network::set.vertex.attribute(g, "vertex.names", value=V(exIgraph2)$label) 45 | network::set.edge.attribute(g, "label", igraph::get.edge.attribute(exIgraph2, "label")) 46 | exNetwork2 <- network::network.copy(g) 47 | 48 | # save objects to files 49 | usethis::use_data(exIgraph, overwrite = TRUE) 50 | usethis::use_data(exIgraph2, overwrite = TRUE) 51 | usethis::use_data(exNetwork, overwrite = TRUE) 52 | usethis::use_data(exNetwork2, overwrite = TRUE) 53 | -------------------------------------------------------------------------------- /man/netcompare.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/netcompare.R 3 | \name{netcompare} 4 | \alias{netcompare} 5 | \title{Comparing and testing network objects} 6 | \usage{ 7 | netcompare(target, current, test = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{target, current}{network objects, currently \code{network} and 11 | \code{igraph} classes are supported} 12 | 13 | \item{test}{logical, whether to perform the test or return comparison data, 14 | see Details} 15 | 16 | \item{\dots}{other arguments, currently ignored} 17 | } 18 | \value{ 19 | Depending on the value of \code{test} either an object of class 20 | \code{netcompare} containing the results of all the tests (if 21 | \code{test=FALSE}) or (if \code{test=TRUE}) a logical whether or not the 22 | networks are (nearly) the same. 23 | } 24 | \description{ 25 | Compare or test network objects for (near) equality. 26 | } 27 | \details{ 28 | Arguments \code{target} and \code{current} can be network objects of one of 29 | the supported classes. They do not have to be of the same class though. 30 | 31 | The function does a series of comparisons between \code{target} and 32 | \code{current}: 33 | 34 | \enumerate{ 35 | \item The network structure comparison is made based on adjacency matrices 36 | (mind this when using for huge networks). 37 | 38 | \item Network/edge/vertex attributes are checked for presence in both 39 | objects. 40 | 41 | \item Common network/edge/vertex attribures are checked for equality. 42 | } 43 | All the results are collected in a list of class \code{netcompare} with an 44 | associated \code{print} method. 45 | 46 | If \code{test} is TRUE then instead of the detailed test results the function 47 | returns TRUE or FALSE depending on some of the checks resulted positively. 48 | Currently attribute checks are ignored, i.e. what is taken into account is: 49 | 50 | \enumerate{ 51 | \item Equivalence of adjacency matrices 52 | \item Directed / undirected character of the network 53 | \item Edge set size 54 | \item Vertex set size 55 | } 56 | } 57 | \examples{ 58 | 59 | netcompare( asIgraph(exNetwork), exNetwork) 60 | netcompare( asIgraph(exNetwork), exNetwork, test=TRUE) 61 | 62 | } 63 | \seealso{ 64 | \code{\link{all.equal}}, \code{\link{identical}} 65 | } 66 | -------------------------------------------------------------------------------- /R/dumpAttr.R: -------------------------------------------------------------------------------- 1 | #' Dump network attributes to a list 2 | #' 3 | #' Given a network return a list of all the attributes. 4 | #' 5 | #' 6 | #' @aliases dumpAttr dumpAttr.network dumpAttr.igraph 7 | #' @param x network object 8 | #' @param type character, type of attributes to dump 9 | #' @param \dots other arguments from/to other methods 10 | #' @return If \code{type} is one of "network", "vertex" or "edge" then a list of 11 | #' corresponding attributes. 12 | #' 13 | #' If \code{type} is "all" then lists of lists of attributes. 14 | #' 15 | #' @export 16 | #' @example examples/dumpAttr.R 17 | 18 | dumpAttr <- function(x, ...) UseMethod("dumpAttr") 19 | 20 | #' @method dumpAttr network 21 | #' @export 22 | #' @rdname dumpAttr 23 | dumpAttr.network <- function(x, type=c("all", "network", "vertex", "edge"), ...) 24 | { 25 | type <- match.arg(type) 26 | if(type == "all") 27 | { 28 | n <- c("network", "vertex", "edge") 29 | rval <- lapply( n, function(nam) dumpAttr(x, type=nam)) 30 | names(rval) <- n 31 | return(rval) 32 | } else 33 | { 34 | type <- match.arg(type) 35 | nam <- switch(type, 36 | network = network::list.network.attributes(x), 37 | edge = network::list.edge.attributes(x), 38 | vertex = network::list.vertex.attributes(x) ) 39 | rval <- switch( type, 40 | network = lapply( nam, function(a) network::get.network.attribute(x, a)), 41 | edge = lapply( nam, function(a) network::get.edge.attribute(x$mel, a)), 42 | vertex = lapply( nam, function(a) network::get.vertex.attribute(x, a)) ) 43 | names(rval) <- nam 44 | return(rval) 45 | } 46 | } 47 | 48 | 49 | #' @method dumpAttr igraph 50 | #' @export 51 | #' @rdname dumpAttr 52 | dumpAttr.igraph <- function(x, type=c("all", "network", "vertex", "edge"), ...) 53 | { 54 | type <- match.arg(type) 55 | if(type == "all") 56 | { 57 | n <- c("network", "vertex", "edge") 58 | rval <- lapply( n, function(nam) dumpAttr(x, type=nam)) 59 | names(rval) <- n 60 | return(rval) 61 | } else 62 | { 63 | nams <- switch( type, 64 | network = igraph::graph_attr_names(x), 65 | edge = igraph::edge_attr_names(x), 66 | vertex = igraph::vertex_attr_names(x) ) 67 | rval <- switch( type, 68 | network = lapply( nams, function(a) igraph::graph_attr(x, a) ), 69 | edge = lapply( nams, function(a) igraph::edge_attr(x, a) ), 70 | vertex = lapply( nams, function(a) igraph::vertex_attr(x, a) ) ) 71 | names(rval) <- nams 72 | return(rval) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /man/asNetwork.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/asNetwork.R 3 | \name{asNetwork} 4 | \alias{asNetwork} 5 | \alias{asNetwork.data.frame} 6 | \alias{asNetwork.igraph} 7 | \title{Convert objects to class "network"} 8 | \usage{ 9 | asNetwork(x, ...) 10 | 11 | \method{asNetwork}{data.frame}(x, directed = TRUE, vertices = NULL, ...) 12 | 13 | \method{asNetwork}{igraph}(x, amap = attrmap(), ...) 14 | } 15 | \arguments{ 16 | \item{x}{an R object to be coerced, see Details for the description of 17 | available methods} 18 | 19 | \item{\dots}{other arguments from/to other methods} 20 | 21 | \item{directed}{logical, whether the created network should be directed} 22 | 23 | \item{vertices}{NULL or data frame, optional data frame containing vertex 24 | attributes} 25 | 26 | \item{amap}{data.frame with attribute copy/rename rules, see 27 | \code{\link{attrmap}}} 28 | } 29 | \value{ 30 | Object of class "network". 31 | } 32 | \description{ 33 | Convert objects to class "network" 34 | } 35 | \details{ 36 | This is a generic function which dispatches on argument \code{x}. It creates 37 | objects of class "network" from other R objects. 38 | 39 | The method for data frames is inspired by the similar function in package 40 | \pkg{igraph}: \code{\link[igraph]{graph.data.frame}}. It assumes that first 41 | two columns of \code{x} constitute an edgelist. The remaining columns are 42 | interpreted as edge attributes. Optional argument \code{vertices} allows for 43 | including vertex attributes. The first column is assumed to vertex id, the 44 | same that is used in the edge list. The remaining colums are interpreted as 45 | vertex attributes. 46 | 47 | The method for objects of class "igraph" takes the network of that class and 48 | converts it to data frames using \code{\link{asDF}}. The network is recreated 49 | in class "network" using \code{asNetwork.data.frame}. The function currently 50 | does not support bipartite "igraph" networks. 51 | } 52 | \examples{ 53 | # require package 'network' as 'asNetwork' generic is defined there 54 | if(require(network, quietly=TRUE)) 55 | { 56 | ### demonstrating method for data frames 57 | l <- asDF(exNetwork) 58 | g <- asNetwork( l$edges, vertices=l$vertexes) 59 | stopifnot(all.equal(g, exNetwork)) 60 | 61 | ### method for igraph objects 62 | ig <- asNetwork(exIgraph) 63 | identical( as.numeric(as.matrix(g, "adjacency")), 64 | as.numeric(as.matrix(ig, "adjacency"))) 65 | } 66 | 67 | } 68 | \seealso{ 69 | \code{\link[igraph]{graph.data.frame}} 70 | 71 | \code{\link{asIgraph}} for conversion in the other direction. 72 | } 73 | -------------------------------------------------------------------------------- /man/intergraph-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intergraph-package.R 3 | \docType{package} 4 | \name{intergraph-package} 5 | \alias{intergraph} 6 | \alias{intergraph-package} 7 | \title{Coercion Routines for Network Data Objects} 8 | \description{ 9 | This package contains methods for coercion between various classes used to 10 | represent network data in R. 11 | } 12 | \details{ 13 | Functions implemented in this package allow to coerce (i.e. convert) network 14 | data between classes provided by other R packages. Currently supported 15 | classes are: "network" from package \pkg{network}, "igraph" from package 16 | \pkg{igraph}. 17 | 18 | The main functions are: 19 | \itemize{ 20 | \item \code{\link{asNetwork}} and its methods to create objects of class 21 | "network". 22 | 23 | \item \code{\link{asIgraph}} and its methods to create objects of class 24 | "igraph". 25 | } 26 | See their help pages for more information and examples. 27 | 28 | As all the supported packages are written using S3 methods, so are the 29 | methods in this package. 30 | 31 | If you find this package useful in your work please cite it. Type 32 | \code{citation(package="intergraph")} for the information how to do that. 33 | } 34 | \examples{ 35 | # example of converting objects between classes 'network' and 'igraph' 36 | # needs packages igraph and network attached 37 | if( require(network) & require(igraph) ) 38 | { 39 | 40 | ### convert 'network' -> 'igraph' 41 | 42 | # example network as object of class "network" 43 | summary(exNetwork) 44 | 45 | # convert to class "igraph" 46 | g <- asIgraph(exNetwork) 47 | 48 | # check if 'exNetwork' and 'g' are the same 49 | # (dropping some aux attributes) 50 | all.equal( structure(as.matrix(exNetwork, "edgelist"), n=NULL, vnames=NULL), 51 | igraph::as_edgelist(g) ) 52 | 53 | # compare results using 'netcompare' 54 | netcompare(exNetwork, g) 55 | 56 | 57 | ### convert 'igraph' -> 'network' 58 | 59 | # example network as object of class "igraph" 60 | summary(exIgraph) 61 | 62 | # convert to class "network" 63 | gg <- asNetwork(exIgraph) 64 | 65 | # check if they are the same 66 | # (dropping some attributes) 67 | all.equal( get.edgelist(exIgraph), 68 | structure(as.matrix(gg, "edgelist"), n=NULL, vnames=NULL)) 69 | netcompare(exIgraph, gg) 70 | } 71 | } 72 | \seealso{ 73 | Useful links: 74 | \itemize{ 75 | \item \url{https://mbojan.github.io/intergraph/} 76 | \item Report bugs at \url{https://github.com/mbojan/intergraph/issues} 77 | } 78 | 79 | } 80 | \author{ 81 | Written and maintained by Michal Bojanowski \email{m.bojanowski@icm.edu.pl}. 82 | } 83 | \keyword{package} 84 | -------------------------------------------------------------------------------- /examples/exNetwork.R: -------------------------------------------------------------------------------- 1 | 2 | if(require(network, quietly=TRUE) ) print(exNetwork) 3 | if( require(igraph, quietly=TRUE) ) print(exIgraph) 4 | 5 | 6 | # showing-off 'network' versions 7 | if(require(network, quietly=TRUE)) 8 | { 9 | op <- par(mar=c(1,1,1,1)) 10 | layout( matrix(1:2, 1, 2, byrow=TRUE) ) 11 | # need to change the family to device default because of faulty 'igraph' 12 | plot(exNetwork, main="Directed, class 'network'", displaylabels=TRUE) 13 | plot(exNetwork2, main="Undirected, class 'network'", displaylabels=TRUE) 14 | par(op) 15 | } 16 | 17 | # not running because of a bug in 'igraph': plot.igraph wants to set default 18 | # font for vertex labels to 'serif', which is not supported on all devices 19 | if(FALSE) { 20 | # showing-off 'igraph' versions 21 | if(require(igraph, quietly=TRUE)) 22 | { 23 | op <- par(mar=c(1,1,1,1)) 24 | layout( matrix(1:2, 1, 2, byrow=TRUE) ) 25 | plot(exIgraph, main="Directed, class 'igraph'") 26 | plot(exIgraph2, main="Undirected, class 'igraph'") 27 | par(op) 28 | } 29 | } 30 | 31 | # The data was generated with the following code 32 | if(FALSE) { 33 | # directed igraph 34 | g <- igraph::graph( c(2,1, 3,1, 4,1, 5,1, # star 35 | 6,7, 8,9, # two dyads 36 | 10,11, 11,12, 12,13, 13,14, 14,12), # stem-leaf 37 | n=14, directed=TRUE) 38 | # add some vertex attributes 39 | vl <- letters[seq(1, vcount(g))] 40 | g <- igraph::set_vertex_attr(g, "label", value=vl) 41 | # add some edge attributes 42 | m <- igraph::as_edgelist(g) 43 | l <- matrix(vl[m+1], ncol=2) 44 | el <- apply(l, 1, paste, collapse="") 45 | g <- igraph::set_edge_attr(g, "label", value=el) 46 | g <- igraph::set_graph_attr(g, "layout", igraph::layout_with_fr) 47 | rm(vl, l, m, el) 48 | exIgraph <- g 49 | 50 | # undirected igraph 51 | exIgraph2 <- igraph::as.undirected(exIgraph) 52 | exIgraph2 <- igraph::set_edge_attr(exIgraph2, "label", 53 | value=igraph::edge_attr(exIgraph, "label")) 54 | 55 | 56 | # copy as a 'network' object through adjacency matrix 57 | m <- igraph::as_adjacency_matrix(exIgraph) 58 | g <- network::network(m, vertex.attr=list(label=vattr(exIgraph, "label")), 59 | vertex.attrnames="label", directed=TRUE) 60 | network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph, "label")) 61 | network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph, "label")) 62 | exNetwork <- network::network.copy(g) 63 | 64 | # copy as a 'network' object through adjacency matrix 65 | m <- igraph::as_adjacency_matrix(exIgraph2) 66 | g <- network::network(m, vertex.attr=list(label=vattr(exIgraph2, "label")), 67 | vertex.attrnames="label", directed=FALSE) 68 | network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph2, "label")) 69 | network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph2, "label")) 70 | exNetwork2 <- network::network.copy(g) 71 | } 72 | 73 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # intergraph 2.0-4 2 | 3 | ## Internals 4 | 5 | Thank you @krlmlr! 6 | 7 | - Changes induced by **igraph** 2.0.0 release: deprecating functions with dots in names (#36, #40). 8 | - Internal S3 methods are properly exported (#38, #39). 9 | - Don't use `@docType package` (#37) 10 | 11 | 12 | 13 | # intergraph 2.0-3 14 | 15 | ## User-visible changes 16 | 17 | - Per #33, remove `as.matrix.igraph()` as it has been moved to **igraph**. Adjust the tests accordingly. 18 | - Fix `netcompare()` and `compareAttributes()` (#34 and #35). 19 | 20 | 21 | ## Internals 22 | 23 | - Fixed #21, #28 related to data frame vs tibble differences. 24 | - Update tests to use **testthat**. 25 | - Use **covr** for test coverage. 26 | - Change email address to Gmail and add ORCID to DESCRIPTION. 27 | - Resave data using `usethis::use_data()`. 28 | - Refactor tests of `attrmap()`, `asIgraph()` and `asNetwork()` (#34) 29 | 30 | 31 | 32 | 33 | # intergraph 2.0-2 34 | 35 | Small updates due to `igraph` update to 1.0-0. 36 | 37 | * Uprgaded included igraph objects in `data` to new data type (see `igraph::upgrade_graph`). 38 | * Fixed tests to use `igraph::identical_graphs`. 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | # intergraph 2.0-1 47 | 48 | ## User visible changes 49 | 50 | * Package moved from R-Forge to GitHub. 51 | 52 | * Custom rules fo attribute copying using `attrmap()` should work now. Argument for supplying custom rules to `asIgraph` and `asNetwork` is renamed from `attrmap` to amap (was triggering "promise already under evaluation" errors". 53 | 54 | * Dropped graph/network attribute `layout` that contained a function for Fruchterman-Reingold algorithm. It was triggering warnings when printing `network` objects. Now F-R is default in "igraph" anyway. 55 | 56 | * Added 'howto' vignette demonstrating basic usage. 57 | 58 | 59 | ## Internals 60 | 61 | * Added more tests. 62 | 63 | * Updated `CITATION`. Uses `meta` object if available. 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | # intergraph 2.0-0 73 | 74 | ## User visible changes 75 | 76 | * Renamed main functions to `asIgraph` and `asNetwork`. It was very hard to provide just methods for `as.igraph` and `as.network` without having to attach both packages at the same time. Having both attached triggers function name conflicts. 77 | 78 | ## Internals 79 | 80 | * Whole package converted to devtools/roxygen2 setup. 81 | 82 | 83 | 84 | 85 | 86 | 87 | # intergraph 1.3-0 88 | 89 | First public version of the package. Main functions are: 90 | 91 | * A method for generic `as.network` defined in package "network". 92 | * Generic function `as.igraph` with methods. 93 | * Function `asDF` for converting networks to a (list of) data frame(s) for edges and vertices. 94 | * Function `dumpAttr` for dumping all attributes as listed by `list.vertex.attributes`, `list.edge.attributes`, `list.network.attributes`, and `list.graph.attributes`. 95 | 96 | -------------------------------------------------------------------------------- /man/asDF.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/asDF.R 3 | \name{asDF} 4 | \alias{asDF} 5 | \alias{asDF.network} 6 | \alias{asDF.igraph} 7 | \title{Convert network to data frame(s)} 8 | \usage{ 9 | asDF(object, ...) 10 | 11 | \method{asDF}{network}(object, ...) 12 | 13 | \method{asDF}{igraph}(object, ...) 14 | } 15 | \arguments{ 16 | \item{object}{R object representing a network, see below for available 17 | methods} 18 | 19 | \item{\dots}{other arguments passed to/from other methods} 20 | } 21 | \value{ 22 | List with two components: 23 | \describe{ 24 | \item{\code{edges}}{containing an edge list data frame at first two columns 25 | and edge attributes on further ones.} 26 | \item{\code{vertexes}}{with vertex id in the first column, named \code{id} 27 | and any vertex attributes in the other columns.} 28 | } 29 | } 30 | \description{ 31 | Convert a network data object to, possibly two, data frames: a data frame 32 | with an edge list with edge attributes (if any), and a data frame of vertexes 33 | with vertex attributes (if any). This is a generic function, see below for 34 | available methods. 35 | } 36 | \details{ 37 | Currently there are methods for \code{object} being in one of the following 38 | classes: "network", "igraph". 39 | 40 | The function first gets the graph edge list using the appropriate function 41 | depending on the class of \code{object} (see below). Edge attributes, if 42 | any, are then extracted using \code{\link{dumpAttr}} and added to it. 43 | 44 | The vertex data frame is constructed with a vertex id as a sequence of 45 | integer numbers. Details are method-specific, see below. Vertex attributes 46 | are extracted with \code{\link{dumpAttr}} and added to this data frame. 47 | 48 | Method-specific notes: 49 | 50 | For objects of class "network". Objects of this class store the vertex ids 51 | as integer numbers. There is also an attribute "vertex.names" which is 52 | always created when using graph constructors provided in the package 53 | \pkg{network}. \code{asDF} adds "vertex.names" to the vertex data frame as 54 | a normal attribute and does not use it as a vertex id in the edge list. 55 | 56 | The edge list is created using \code{\link[network]{as.matrix.network}} 57 | function and contains integer vertex ids. 58 | 59 | Objects of class "igraph", as provided by the \pkg{igraph} package. Vertex 60 | ids in these objects integers starting from 1 (in \pkg{igraph} version prior 61 | to 0.6-0 vertex ids started from 0). However, it is also possible to provide 62 | a vertex attribute "name". It is added to the vertex data frame as a normal 63 | vertex attribute and is not used on the edge list data frame. 64 | 65 | The edge list is created using \code{\link[igraph]{get.edgelist}} function 66 | with argument \code{names} set to \code{FALSE} so that integer vertex ids 67 | are used. 68 | } 69 | \examples{ 70 | # using method for 'network' objects 71 | d1 <- asDF(exNetwork) 72 | str(d1) 73 | 74 | # using method for 'igraph' objects 75 | d2 <- asDF(exIgraph) 76 | str(d2) 77 | } 78 | -------------------------------------------------------------------------------- /tests/testthat/test-asIgraph.R: -------------------------------------------------------------------------------- 1 | # From data.frames -------------------------------------------------------- 2 | 3 | test_that("Disassembling to d.f and assembling back to igraph gives the same result", { 4 | # convert to data frames 5 | l <- asDF(exIgraph) 6 | # assemble back 7 | g <- asIgraph( l$edges, vertices=l$vertexes) 8 | expect_true( igraph::identical_graphs(g, exIgraph) ) 9 | } ) 10 | 11 | 12 | 13 | test_that("Providing non-existing name to 'vnames' throws an error", { 14 | ## testing 'vnames' argument 15 | # non-existent column in 'vertices' 16 | expect_error( 17 | asIgraph(l$edges, vertices=l$vertexes, vnames="foo") 18 | ) 19 | } ) 20 | 21 | 22 | 23 | test_that("Vertex names are properly set via 'vnames' argument for directed network", { 24 | # existing column in 'vertices' 25 | l <- asDF(exIgraph) 26 | g <- asIgraph( l$edges, vertices=l$vertexes, vnames="label") 27 | expect_equal( l$vertexes$label, igraph::vertex_attr(g, "name")) 28 | } ) 29 | 30 | 31 | 32 | test_that("Vertex names are properly set via 'vnames' argument for undirected network", { 33 | ## above tests but for the undirected network 34 | ## convert to data frames and assemble back to igraph object 35 | l <- asDF(exIgraph2) 36 | g <- asIgraph( l$edges, vertices=l$vertexes, directed=FALSE) 37 | expect_true( igraph::identical_graphs(g, exIgraph2) ) 38 | } ) 39 | 40 | 41 | 42 | # From tibbles ------------------------------------------------------------ 43 | 44 | test_that("Igraph is created from edgelist as tibble", { 45 | edb <- tibble::tibble( 46 | from = 1:4, 47 | to = 2:5 48 | ) 49 | expect_silent( 50 | net <- asIgraph(edb) 51 | ) 52 | expect_equal( igraph::vcount(net), 5) 53 | expect_equal( igraph::ecount(net), 4) 54 | }) 55 | 56 | test_that("Network is created from tibbles", { 57 | edb <- tibble::tibble( 58 | from = 1:4, 59 | to = 2:5 60 | ) 61 | vdb <- tibble::tibble( 62 | id = 1:5, 63 | ch = letters[1:5] 64 | ) 65 | expect_silent( net <- asIgraph(edb, vertices=vdb)) 66 | expect_equal( igraph::vcount(net), 5) 67 | expect_equal( igraph::ecount(net), 4) 68 | }) 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | # From networks ----------------------------------------------------------- 79 | 80 | test_that("Conversion for exNetwork is OK tested with netcompare", { 81 | # directed network 82 | res <- netcompare( asIgraph(exNetwork), exNetwork, test=TRUE ) 83 | expect_true(res) 84 | } ) 85 | 86 | test_that("Conversion for exNetwork2 is OK tested with netcompare", { 87 | # undirected network 88 | res2 <- netcompare( asIgraph(exNetwork2), exNetwork2, test=TRUE ) 89 | expect_true(res2) 90 | } ) 91 | 92 | 93 | test_that("Conversion of bipartite networks is not yet supported", { 94 | ### bipartite network (not yet supported) 95 | m <- matrix(0, ncol=2, nrow=3) 96 | m[1,1] <- m[2,1] <- m[1,2] <- m[3,2] <- 1 97 | net <- network::network(t(m), bipartite=TRUE, directed=FALSE) 98 | expect_error(asIgraph(net)) 99 | } ) 100 | -------------------------------------------------------------------------------- /tests/testthat/test-asNetwork.R: -------------------------------------------------------------------------------- 1 | # From data.frames -------------------------------------------------------- 2 | 3 | test_that("Disassembling directed network to d.f. and assembling back to network works", { 4 | l <- asDF(exNetwork) 5 | g <- asNetwork( l$edges, vertices=l$vertexes) 6 | expect_identical(g, exNetwork) 7 | } ) 8 | 9 | test_that("Disassembling undirected network to d.f. and assembling back to network works", { 10 | l <- asDF(exNetwork2) 11 | g <- asNetwork( l$edges, vertices=l$vertexes, directed=FALSE) 12 | expect_identical(g, exNetwork2) 13 | } ) 14 | 15 | test_that("Disassembling directed igraph to d.f. and assembling back to network: edgecount", { 16 | l <- asDF(exIgraph) 17 | g <- asNetwork(l$edges, vertices=l$vertexes) 18 | expect_equal( network::network.edgecount(g), nrow(l$edges)) 19 | } ) 20 | 21 | test_that("Disassembling directed igraph to d.f. and assembling back to network: vcount", { 22 | l <- asDF(exIgraph) 23 | g <- asNetwork(l$edges, vertices=l$vertexes) 24 | expect_equal( network::network.size(g), nrow(l$vertexes)) 25 | } ) 26 | 27 | test_that("Disassembling undirected igraph to d.f. and assembling back to network: edgecount", { 28 | l <- asDF(exIgraph2) 29 | g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE) 30 | expect_equal( network::network.edgecount(g), nrow(l$edges)) 31 | } ) 32 | 33 | test_that("Disassembling undirected igraph to d.f. and assembling back to network: vcount", { 34 | l <- asDF(exIgraph2) 35 | g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE) 36 | expect_equal( network::network.size(g), nrow(l$vertexes)) 37 | } ) 38 | 39 | 40 | 41 | 42 | # From igraphs ------------------------------------------------------------ 43 | 44 | test_that("Directed igraphs via netcompare", { 45 | res <- netcompare( asNetwork(exIgraph), exIgraph, test=TRUE ) 46 | expect_true(res) 47 | } ) 48 | 49 | test_that("Undirected igraphs via netcompare", { 50 | res2 <- netcompare( asNetwork(exIgraph2), exIgraph2, test=TRUE ) 51 | expect_true(res2) 52 | } ) 53 | 54 | test_that("NAs in igraph vertex labels are copied (bug 1926)", { 55 | # network with NA on edge attribute 56 | g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE) 57 | igraph::E(g)$label <- c(1,2,3,NA,4) 58 | # convert to 'network' 59 | net <- asNetwork(g) 60 | # warning that NA is converted to "NA"? 61 | expect_true( is.na( network::get.edge.attribute(net, "label")[4] ) ) 62 | } ) 63 | 64 | 65 | 66 | # From tibbles ------------------------------------------------------------ 67 | 68 | test_that("Network is created from edgelist as tibble", { 69 | edb <- tibble::tibble( 70 | from = 1:4, 71 | to = 2:5 72 | ) 73 | expect_silent( 74 | net <- asNetwork(edb) 75 | ) 76 | expect_equal( network::network.size(net), 5) 77 | expect_equal( network::network.edgecount(net), 4) 78 | }) 79 | 80 | test_that("Network is created from tibbles", { 81 | edb <- tibble::tibble( 82 | from = 1:4, 83 | to = 2:5 84 | ) 85 | vdb <- tibble::tibble( 86 | id = 1:5, 87 | ch = letters[1:5], 88 | stringsAsFactors = FALSE 89 | ) 90 | expect_silent( net <- asNetwork(edb, vertices=vdb)) 91 | }) 92 | -------------------------------------------------------------------------------- /man/asIgraph.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/asIgraph.R 3 | \name{asIgraph} 4 | \alias{asIgraph} 5 | \alias{asIgraph.network} 6 | \alias{asIgraph.data.frame} 7 | \title{Coerce an object to class "igraph"} 8 | \usage{ 9 | asIgraph(x, ...) 10 | 11 | \method{asIgraph}{network}(x, amap = attrmap(), ...) 12 | 13 | \method{asIgraph}{data.frame}(x, directed = TRUE, vertices = NULL, vnames = NULL, ...) 14 | } 15 | \arguments{ 16 | \item{x}{R object to be converted} 17 | 18 | \item{\dots}{other arguments from/to other methods} 19 | 20 | \item{amap}{data.frame with attribute copy/rename rules, see 21 | \code{\link{attrmap}}} 22 | 23 | \item{directed}{logical, whether the created network should be directed} 24 | 25 | \item{vertices}{NULL or data frame, optional data frame containing vertex 26 | attributes} 27 | 28 | \item{vnames}{character, name of the column in \code{vertices} to be used as 29 | a \code{name} vertex attribute, if \code{NULL} no vertex names are created} 30 | } 31 | \value{ 32 | Object of class "igraph". 33 | } 34 | \description{ 35 | Coerce objects to class "igraph". 36 | } 37 | \details{ 38 | \code{asIgraph} is a generic function with methods written for data frames 39 | and objects of class "network". 40 | 41 | If \code{x} is a data frame, the method used is a wrapper around 42 | \code{\link[igraph]{graph.data.frame}} in package \pkg{igraph}. The 43 | \code{vnames} argument was added so that the user can specify which vertex 44 | attribute from the data frame supplied through \code{vertices} argument is 45 | used for vertex names (the \code{name} attribute in \code{igraph} objects) in 46 | the returned result. By default the vertex names are not created. 47 | 48 | If \code{x} is of class "network" (package \pkg{network}) the function 49 | uses \code{\link{asDF}} to extract data on edges and vertex with their 50 | attributes (if present). Network attributes are extracted as well. Not all 51 | vertex/edge/network attributes are worth preserving though. Attributes are 52 | copied, dropped or renamed based on rules given in the \code{amap} 53 | argument, see \code{\link{attrmap}} for details. The function currently does 54 | not support objects that represent neither bipartite networks nor 55 | hypergraphs. 56 | } 57 | \examples{ 58 | ### using 'asIgraph' on objects of class 'network' 59 | 60 | g <- asIgraph(exNetwork) 61 | 62 | # compare adjacency matrices 63 | netmat <- as.matrix(exNetwork, "adjacency") 64 | imat <- as.matrix(g, "adjacency") 65 | # drop the dimnames in 'netmat' 66 | dimnames(netmat) <- NULL 67 | # compare 68 | identical( netmat, imat ) 69 | 70 | 71 | ### using 'asIgraph' on data.frames 72 | 73 | # data frame with vertex ids and vertex attributes 74 | v <- 1:4 75 | vd <- data.frame(id = v + 5, label=letters[1:4]) 76 | print(vd) 77 | 78 | # edge list (first two columns) and edge attributes 79 | e <- c(1,2, 2,3, 3,4, 4,1) 80 | ed <- data.frame(id1 = e[seq(1,8, by=2)]+5, id2=e[seq(2, 8, by=2)]+5, a=letters[1:4]) 81 | print(ed) 82 | 83 | # build the network 84 | # without vertex attributes 85 | g <- asIgraph(ed, directed=FALSE) 86 | # with vertex attributes 87 | gv <- asIgraph(ed, vertices=vd, directed=FALSE) 88 | 89 | # NOTE: Even though vertex ids start at 6 the network has 4 nodes: 90 | range(vd$id) # min and max of node ids 91 | if(require(igraph)) igraph::vcount(gv) # number of nodes in 'gv' 92 | } 93 | \seealso{ 94 | \code{\link[igraph]{graph.data.frame}} 95 | } 96 | -------------------------------------------------------------------------------- /R/attrmap.R: -------------------------------------------------------------------------------- 1 | #' Network attribute copying/renaming table 2 | #' 3 | #' Setting and retrieving rules through which network/edge/vertex attributes 4 | #' are copied or renamed when converting network objects. 5 | #' 6 | #' Different classes for network data use different attribute names to store 7 | #' the same information. Some of the classes define attributes not used by 8 | #' other classes. This function is used to retrieve or set the rules in which 9 | #' these attributes are copied, renamed or dropped when converting network 10 | #' objects. 11 | #' 12 | #' The rules are stored in a data frame with the following columns (all of mode 13 | #' character): 14 | #' \describe{ 15 | #' \item{type}{type, or level of the attribute, one of "vertex", "edge" or 16 | #' "network"} 17 | #' \item{fromcls}{name of the class which is being coerced} 18 | #' \item{fromattr}{name of the "source" attribute} 19 | #' \item{tocls}{name of the class to which coercion is being done} 20 | #' \item{toattr}{name of the attribute in the result of coercion, or NA} 21 | #' } 22 | #' When converting network \code{x}, of class \code{xclass} to network \code{y} 23 | #' of class \code{yclass} the coercion method looks up the rows in this table 24 | #' for which \code{fromcls=xclass} and \code{tocls=yclass}. If network \code{x} 25 | #' has any of the attributes listed in the \code{fromattr} in the resulting 26 | #' subset of the table then, it is renamed to the corresponding value of 27 | #' \code{toattr}. If \code{toattr} is \code{NA} the attribute is dropped from 28 | #' the result. 29 | #' 30 | #' @param newdf data.frame, new set of copy/rename rules, see Details 31 | #' 32 | #' @return If \code{newdf} is NULL, a data frame with the current copy/rename 33 | #' rules. Otherwise \code{newdf} are set as new rules and the old ones are 34 | #' returned invisibly, much like \code{\link{par}}. 35 | #' 36 | #' @seealso This is used by \code{\link{asIgraph}} and \code{\link{asNetwork}} 37 | #' 38 | #' @export 39 | #' 40 | #' @examples 41 | #' 42 | #'# Current values 43 | #'attrmap() 44 | #' 45 | attrmap <- function(newdf=NULL) 46 | { 47 | cur <- utils::getFromNamespace(".attrmap", "intergraph") 48 | if( is.null(newdf) ) 49 | { 50 | # return current 51 | return(cur) 52 | } else 53 | { 54 | # assign new 55 | utils::assignInNamespace(".attrmap", newdf, "intergraph") 56 | # return old invisibly 57 | invisible(cur) 58 | } 59 | } 60 | 61 | # subset of attrmap depending on type and from/to classes 62 | attrmapmat <- function(from, to, atype, db=attrmap()) 63 | { 64 | i <- with(db, type==atype & fromcls==from & tocls==to) 65 | as.matrix( db[i, c("fromattr", "toattr")] ) 66 | } 67 | 68 | # attribute renaming table 69 | .attrmap <- matrix(ncol=5, byrow=TRUE, c( 70 | "network" , "network" , "directed" , "igraph" , NA , 71 | "network" , "network" , "bipartite" , "igraph" , NA , 72 | "network" , "network" , "loops" , "igraph" , NA , 73 | "network" , "network" , "mnext" , "igraph" , NA , 74 | "network" , "network" , "multiple" , "igraph" , NA , 75 | "network" , "network" , "n" , "igraph" , NA , 76 | "network" , "network" , "hyper" , "igraph" , NA , 77 | "vertex" , "igraph" , "name" , "network" , "vertex.names" 78 | ) ) 79 | .attrmap <- as.data.frame(.attrmap, stringsAsFactors=FALSE) 80 | names(.attrmap) <- c("type", "fromcls", "fromattr", "tocls", "toattr") 81 | -------------------------------------------------------------------------------- /R/asDF.R: -------------------------------------------------------------------------------- 1 | #' Convert network to data frame(s) 2 | #' 3 | #' Convert a network data object to, possibly two, data frames: a data frame 4 | #' with an edge list with edge attributes (if any), and a data frame of vertexes 5 | #' with vertex attributes (if any). This is a generic function, see below for 6 | #' available methods. 7 | #' 8 | #' Currently there are methods for \code{object} being in one of the following 9 | #' classes: "network", "igraph". 10 | #' 11 | #' The function first gets the graph edge list using the appropriate function 12 | #' depending on the class of \code{object} (see below). Edge attributes, if 13 | #' any, are then extracted using \code{\link{dumpAttr}} and added to it. 14 | #' 15 | #' The vertex data frame is constructed with a vertex id as a sequence of 16 | #' integer numbers. Details are method-specific, see below. Vertex attributes 17 | #' are extracted with \code{\link{dumpAttr}} and added to this data frame. 18 | #' 19 | #' Method-specific notes: 20 | #' 21 | #' @param object R object representing a network, see below for available 22 | #' methods 23 | #' 24 | #' @param \dots other arguments passed to/from other methods 25 | #' 26 | #' @return List with two components: 27 | #' \describe{ 28 | #' \item{\code{edges}}{containing an edge list data frame at first two columns 29 | #' and edge attributes on further ones.} 30 | #' \item{\code{vertexes}}{with vertex id in the first column, named \code{id} 31 | #' and any vertex attributes in the other columns.} 32 | #' } 33 | #' 34 | #' @export 35 | #' 36 | #' @example examples/asDF.R 37 | #' 38 | asDF <- function(object, ...) UseMethod("asDF") 39 | 40 | 41 | 42 | 43 | 44 | #' @method asDF network 45 | #' @export 46 | #' @rdname asDF 47 | #' @details 48 | #' For objects of class "network". Objects of this class store the vertex ids 49 | #' as integer numbers. There is also an attribute "vertex.names" which is 50 | #' always created when using graph constructors provided in the package 51 | #' \pkg{network}. \code{asDF} adds "vertex.names" to the vertex data frame as 52 | #' a normal attribute and does not use it as a vertex id in the edge list. 53 | #' 54 | #' The edge list is created using \code{\link[network]{as.matrix.network}} 55 | #' function and contains integer vertex ids. 56 | asDF.network <- function(object, ...) 57 | { 58 | # get edge list and substitute vertex names 59 | dfedge <- as.data.frame(network::as.matrix.network(object, "edgelist"), 60 | stringsAsFactors=FALSE) 61 | # add edge attributes, if any 62 | eattr <- dumpAttr(object, "edge") 63 | if( length(eattr) > 0 ) 64 | dfedge <- cbind(dfedge, as.data.frame(eattr, stringsAsFactors=FALSE)) 65 | # make vertex data frame 66 | dfvertex <- data.frame(intergraph_id=seq(1, network::network.size(object))) 67 | # add vertex attributes if any 68 | vattr <- dumpAttr(object, "vertex") 69 | if( length(vattr) > 0 ) 70 | dfvertex <- cbind( dfvertex, as.data.frame(vattr, stringsAsFactors=FALSE)) 71 | list(edges=dfedge, vertexes=dfvertex) 72 | } 73 | 74 | 75 | 76 | 77 | 78 | #' @method asDF igraph 79 | #' @export 80 | #' @rdname asDF 81 | #' @details 82 | #' Objects of class "igraph", as provided by the \pkg{igraph} package. Vertex 83 | #' ids in these objects integers starting from 1 (in \pkg{igraph} version prior 84 | #' to 0.6-0 vertex ids started from 0). However, it is also possible to provide 85 | #' a vertex attribute "name". It is added to the vertex data frame as a normal 86 | #' vertex attribute and is not used on the edge list data frame. 87 | #' 88 | #' The edge list is created using \code{\link[igraph]{get.edgelist}} function 89 | #' with argument \code{names} set to \code{FALSE} so that integer vertex ids 90 | #' are used. 91 | asDF.igraph <- function(object, ...) 92 | { 93 | # get edgelist 94 | dfedge <- as.data.frame(igraph::as_edgelist(object, names=FALSE), 95 | stringsAsFactors=FALSE) 96 | # add edge attributes, if any 97 | eattr <- dumpAttr(object, "edge") 98 | if( length(eattr) > 0 ) 99 | dfedge <- cbind(dfedge, as.data.frame(eattr, stringsAsFactors=FALSE)) 100 | # make vertex data frame 101 | dfvertex <- data.frame(intergraph_id=seq(1, igraph::vcount(object) )) 102 | # add vertex attributes, if any 103 | vattr <- dumpAttr(object, "vertex") 104 | if( length(vattr) > 0 ) 105 | dfvertex <- cbind( dfvertex, as.data.frame(vattr, stringsAsFactors=FALSE)) 106 | list(edges=dfedge, vertexes=dfvertex) 107 | } 108 | -------------------------------------------------------------------------------- /R/asIgraph.R: -------------------------------------------------------------------------------- 1 | #' Coerce an object to class "igraph" 2 | #' 3 | #' Coerce objects to class "igraph". 4 | #' 5 | #' \code{asIgraph} is a generic function with methods written for data frames 6 | #' and objects of class "network". 7 | #' 8 | #' If \code{x} is a data frame, the method used is a wrapper around 9 | #' \code{\link[igraph]{graph.data.frame}} in package \pkg{igraph}. The 10 | #' \code{vnames} argument was added so that the user can specify which vertex 11 | #' attribute from the data frame supplied through \code{vertices} argument is 12 | #' used for vertex names (the \code{name} attribute in \code{igraph} objects) in 13 | #' the returned result. By default the vertex names are not created. 14 | #' 15 | #' If \code{x} is of class "network" (package \pkg{network}) the function 16 | #' uses \code{\link{asDF}} to extract data on edges and vertex with their 17 | #' attributes (if present). Network attributes are extracted as well. Not all 18 | #' vertex/edge/network attributes are worth preserving though. Attributes are 19 | #' copied, dropped or renamed based on rules given in the \code{amap} 20 | #' argument, see \code{\link{attrmap}} for details. The function currently does 21 | #' not support objects that represent neither bipartite networks nor 22 | #' hypergraphs. 23 | #' 24 | #' @param x R object to be converted 25 | #' @param directed logical, whether the created network should be directed 26 | #' @param amap data.frame with attribute copy/rename rules, see 27 | #' \code{\link{attrmap}} 28 | #' @param vertices NULL or data frame, optional data frame containing vertex 29 | #' attributes 30 | #' @param vnames character, name of the column in \code{vertices} to be used as 31 | #' a \code{name} vertex attribute, if \code{NULL} no vertex names are created 32 | #' @param \dots other arguments from/to other methods 33 | #' 34 | #' @return Object of class "igraph". 35 | #' 36 | #' @seealso \code{\link[igraph]{graph.data.frame}} 37 | #' 38 | #' @export 39 | #' 40 | #' @example examples/asIgraph.R 41 | #' 42 | 43 | asIgraph <- function(x, ...) UseMethod("asIgraph") 44 | 45 | 46 | 47 | 48 | 49 | #' @method asIgraph network 50 | #' @export 51 | #' @rdname asIgraph 52 | asIgraph.network <- function(x, amap=attrmap(), ...) 53 | { 54 | object <- x 55 | # hypergraphs not supported 56 | if(network::is.hyper(object)) 57 | stop("hypergraphs are not supported") 58 | if(network::is.bipartite(object)) 59 | stop("bipartite networks are not supported") 60 | na <- dumpAttr(object, "network") 61 | l <- asDF(object) 62 | 63 | ### prepare edge attributes 64 | eats <- attrmapmat("network", "igraph", "edge", db=amap) 65 | # drop some 66 | todrop <- eats[ is.na(eats[,"toattr"]) , "fromattr" ] 67 | edges <- l$edges[ !( names(l$edges) %in% todrop ) ] 68 | # rename some 69 | names(edges) <- recode(names(edges), eats) 70 | 71 | ### prepare vertex attributes 72 | vats <- attrmapmat("network", "igraph", "vertex", db=amap) 73 | # drop some 74 | todrop <- vats[ is.na(vats[,"toattr"]) , "fromattr" ] 75 | vertexes <- l$vertexes[ !( names(l$vertexes) %in% todrop ) ] 76 | # rename some 77 | names(vertexes) <- recode(names(vertexes), vats) 78 | 79 | ### make 'igraph' object 80 | rval <- asIgraph( edges, 81 | directed=network::is.directed(object), 82 | vertices=vertexes, ...) 83 | 84 | ### apply/rename/drop network attributes 85 | nats <- attrmapmat("network", "igraph", "network", db=amap) 86 | todrop <- nats[ is.na(nats[,"toattr"]) , "fromattr" ] 87 | na <- na[ - which( names(na) %in% todrop ) ] 88 | names(na) <- recode(names(na), nats) 89 | if( length(na) > 0 ) 90 | { 91 | for( naname in names(na) ) 92 | rval <- igraph::set_graph_attr(rval, naname, na[[naname]]) 93 | } 94 | rval 95 | } 96 | 97 | 98 | #' @method asIgraph data.frame 99 | #' @export 100 | #' @rdname asIgraph 101 | asIgraph.data.frame <- function(x, directed=TRUE, vertices=NULL, vnames=NULL, ...) 102 | { 103 | object <- x 104 | rval <- igraph::graph_from_data_frame( object, directed=directed, 105 | vertices=vertices) 106 | if(is.null(vnames)) 107 | { 108 | rval <- igraph::delete_vertex_attr(rval, "name") 109 | } else 110 | { 111 | if( !(vnames %in% names(vertices)) ) 112 | stop("no column ", vnames, " in 'vertices'") 113 | rval <- igraph::set_vertex_attr(rval, "name", value=vertices[[vnames]]) 114 | } 115 | rval 116 | } 117 | -------------------------------------------------------------------------------- /man/exNetwork.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/exNetwork.R 3 | \docType{data} 4 | \name{exNetwork} 5 | \alias{exNetwork} 6 | \alias{exIgraph} 7 | \alias{exNetwork2} 8 | \alias{exIgraph2} 9 | \title{Sample network structure} 10 | \format{ 11 | \describe{ \item{exNetwork,exNetwork2}{is of class \code{network}} 12 | \item{exIgraph,exIgraph2}{is of class \code{igraph}} } Objects 13 | \code{exNetwork} and \code{exIgraph} store directed version of the network. 14 | Objects \code{exNetwork2} and \code{exIgraph2} store the undirected version: 15 | all direction information from the edges is removed. 16 | 17 | The network consists of 15 vertices and 11 edges. \itemize{ \item Vertex 1 is 18 | an isolate. \item Vertices 2-6 constitute a star with vertex 2 as a center. 19 | \item Vertices 7-8 and 9-10 make two dyads \item Vertcies 11, 12, 13,14 and 20 | 15 make a stem-and-leaf network. } 21 | } 22 | \description{ 23 | An examples of networks together with network, edge and vertex attributes 24 | used primarly for testing. The same networks are stored in objects of class 25 | \code{network} and \code{igraph}. 26 | } 27 | \details{ 28 | Vertices and edges has attribute \code{label}. For vertices these are simply 29 | letters from "a" to "o". For edges these are two-letter sequences 30 | corresponding to the neightboring vertices, i.e. the label for the edges 31 | linking nodes "b" and "c" will be "bc". The order is irrelevant. 32 | 33 | In the \code{exNetwork} object the \code{label} attribute is also copied to 34 | the \code{vertex.names} attribute to facilitate plotting. 35 | 36 | The \code{exIgraph} object has additional graph attribute \code{layout} so 37 | that by default Fruchterman-Reingold placement is used for plotting. 38 | } 39 | \examples{ 40 | 41 | if(require(network, quietly=TRUE) ) print(exNetwork) 42 | if( require(igraph, quietly=TRUE) ) print(exIgraph) 43 | 44 | 45 | # showing-off 'network' versions 46 | if(require(network, quietly=TRUE)) 47 | { 48 | op <- par(mar=c(1,1,1,1)) 49 | layout( matrix(1:2, 1, 2, byrow=TRUE) ) 50 | # need to change the family to device default because of faulty 'igraph' 51 | plot(exNetwork, main="Directed, class 'network'", displaylabels=TRUE) 52 | plot(exNetwork2, main="Undirected, class 'network'", displaylabels=TRUE) 53 | par(op) 54 | } 55 | 56 | # not running because of a bug in 'igraph': plot.igraph wants to set default 57 | # font for vertex labels to 'serif', which is not supported on all devices 58 | if(FALSE) { 59 | # showing-off 'igraph' versions 60 | if(require(igraph, quietly=TRUE)) 61 | { 62 | op <- par(mar=c(1,1,1,1)) 63 | layout( matrix(1:2, 1, 2, byrow=TRUE) ) 64 | plot(exIgraph, main="Directed, class 'igraph'") 65 | plot(exIgraph2, main="Undirected, class 'igraph'") 66 | par(op) 67 | } 68 | } 69 | 70 | # The data was generated with the following code 71 | if(FALSE) { 72 | # directed igraph 73 | g <- igraph::graph( c(2,1, 3,1, 4,1, 5,1, # star 74 | 6,7, 8,9, # two dyads 75 | 10,11, 11,12, 12,13, 13,14, 14,12), # stem-leaf 76 | n=14, directed=TRUE) 77 | # add some vertex attributes 78 | vl <- letters[seq(1, vcount(g))] 79 | g <- igraph::set_vertex_attr(g, "label", value=vl) 80 | # add some edge attributes 81 | m <- igraph::as_edgelist(g) 82 | l <- matrix(vl[m+1], ncol=2) 83 | el <- apply(l, 1, paste, collapse="") 84 | g <- igraph::set_edge_attr(g, "label", value=el) 85 | g <- igraph::set_graph_attr(g, "layout", igraph::layout_with_fr) 86 | rm(vl, l, m, el) 87 | exIgraph <- g 88 | 89 | # undirected igraph 90 | exIgraph2 <- igraph::as.undirected(exIgraph) 91 | exIgraph2 <- igraph::set_edge_attr(exIgraph2, "label", 92 | value=igraph::edge_attr(exIgraph, "label")) 93 | 94 | 95 | # copy as a 'network' object through adjacency matrix 96 | m <- igraph::as_adjacency_matrix(exIgraph) 97 | g <- network::network(m, vertex.attr=list(label=vattr(exIgraph, "label")), 98 | vertex.attrnames="label", directed=TRUE) 99 | network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph, "label")) 100 | network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph, "label")) 101 | exNetwork <- network::network.copy(g) 102 | 103 | # copy as a 'network' object through adjacency matrix 104 | m <- igraph::as_adjacency_matrix(exIgraph2) 105 | g <- network::network(m, vertex.attr=list(label=vattr(exIgraph2, "label")), 106 | vertex.attrnames="label", directed=FALSE) 107 | network::set.vertex.attribute(g, "vertex.names", value=vattr(exIgraph2, "label")) 108 | network::set.edge.attribute(g, "label", igraph::edge_attr(exIgraph2, "label")) 109 | exNetwork2 <- network::network.copy(g) 110 | } 111 | 112 | } 113 | \keyword{datasets} 114 | -------------------------------------------------------------------------------- /R/asNetwork.R: -------------------------------------------------------------------------------- 1 | #' Convert objects to class "network" 2 | #' 3 | #' Convert objects to class "network" 4 | #' 5 | #' This is a generic function which dispatches on argument \code{x}. It creates 6 | #' objects of class "network" from other R objects. 7 | #' 8 | #' The method for data frames is inspired by the similar function in package 9 | #' \pkg{igraph}: \code{\link[igraph]{graph.data.frame}}. It assumes that first 10 | #' two columns of \code{x} constitute an edgelist. The remaining columns are 11 | #' interpreted as edge attributes. Optional argument \code{vertices} allows for 12 | #' including vertex attributes. The first column is assumed to vertex id, the 13 | #' same that is used in the edge list. The remaining colums are interpreted as 14 | #' vertex attributes. 15 | #' 16 | #' The method for objects of class "igraph" takes the network of that class and 17 | #' converts it to data frames using \code{\link{asDF}}. The network is recreated 18 | #' in class "network" using \code{asNetwork.data.frame}. The function currently 19 | #' does not support bipartite "igraph" networks. 20 | #' 21 | #' @param x an R object to be coerced, see Details for the description of 22 | #' available methods 23 | #' @param amap data.frame with attribute copy/rename rules, see 24 | #' \code{\link{attrmap}} 25 | #' @param directed logical, whether the created network should be directed 26 | #' @param vertices NULL or data frame, optional data frame containing vertex 27 | #' attributes 28 | #' @param \dots other arguments from/to other methods 29 | #' @return Object of class "network". 30 | #' @seealso \code{\link[igraph]{graph.data.frame}} 31 | #' 32 | #' \code{\link{asIgraph}} for conversion in the other direction. 33 | #' 34 | #' @export 35 | #' 36 | #' @example examples/asNetwork.R 37 | #' 38 | 39 | asNetwork <- function(x, ...) UseMethod("asNetwork") 40 | 41 | #' @method asNetwork data.frame 42 | #' @export 43 | #' @rdname asNetwork 44 | asNetwork.data.frame <- function(x, directed=TRUE, vertices=NULL, ...) 45 | { 46 | edb <- validateEL( as.data.frame(x) ) 47 | # got vertex DB? 48 | if(!is.null(vertices)) 49 | { 50 | vdb <- validateVDB( as.data.frame(vertices) ) 51 | stopifnot(validNetDB(edb, vdb)) 52 | } 53 | # number of vertices 54 | if(is.null(vertices)) nv <- length(unique(c(edb[,1], edb[,2]))) 55 | else nv <- nrow(vertices) 56 | # create an empty network object 57 | rval <- network::network.initialize(nv, directed=directed, hyper=FALSE, 58 | multiple=any(duplicated(edb[,1:2])), 59 | loops=any(edb[,1] == edb[,2])) 60 | # add edges 61 | rval <- network::add.edges(rval, as.list(edb[,1]), as.list(edb[,2])) 62 | # add edge attribbutes 63 | if( ncol(edb) > 2) 64 | for(i in seq(3, ncol(edb))) 65 | { 66 | rval <- network::set.edge.attribute(rval, attrname=names(edb)[i], value=edb[,i]) 67 | } 68 | # vertex attributes 69 | if( !is.null(vertices) && ncol(vertices) > 1 ) 70 | { 71 | for( i in seq(2, ncol(vdb)) ) 72 | { 73 | rval <- network::set.vertex.attribute(rval, attrname=names(vdb)[i], 74 | value=vdb[,i]) 75 | } 76 | } 77 | rval 78 | } 79 | 80 | 81 | #' @method asNetwork igraph 82 | #' @export 83 | #' @rdname asNetwork 84 | asNetwork.igraph <- function(x, amap=attrmap(), ...) 85 | { 86 | object <- x 87 | na <- dumpAttr(object, "network") 88 | l <- asDF(object) 89 | 90 | ### prepare edge attributes 91 | eats <- attrmapmat("igraph", "network", "edge", db=amap) 92 | if( nrow(eats) > 0 ) 93 | { 94 | # drop some 95 | todrop <- eats[ is.na(eats[,"toattr"]) , "fromattr" ] 96 | edges <- l$edges[ !( names(l$edges) %in% todrop ) ] 97 | # rename some 98 | names(edges) <- recode(names(edges), eats) 99 | } else 100 | { 101 | edges <-l$edges 102 | } 103 | 104 | ### prepare vertex attributes 105 | vats <- attrmapmat("igraph", "network", "vertex", db=amap) 106 | if( nrow(vats) > 0 ) 107 | { 108 | # drop some 109 | todrop <- vats[ is.na(vats[,"toattr"]) , "fromattr" ] 110 | vertexes <- l$vertexes[ !( names(l$vertexes) %in% todrop ) ] 111 | # rename some 112 | names(vertexes) <- recode(names(vertexes), vats) 113 | } else 114 | { 115 | vertexes <- l$vertexes 116 | } 117 | 118 | ### make 'igraph' object 119 | rval <- asNetwork( edges, 120 | directed = igraph::is_directed(object), 121 | multiple = igraph::any_multiple(object), 122 | loops = igraph::any_loop(object), 123 | vertices = vertexes, ...) 124 | 125 | ### apply/rename/drop network attributes 126 | nats <- attrmapmat("igraph", "network", "network", db=amap) 127 | if( nrow(nats) > 0 ) 128 | { 129 | todrop <- nats[ is.na(nats[,"toattr"]) , "fromattr" ] 130 | na <- na[ !( names(na) %in% todrop ) ] 131 | names(na) <- recode(names(na), nats) 132 | } 133 | if( length(na) > 0 ) 134 | { 135 | for( naname in names(na) ) 136 | network::set.network.attribute(rval, naname, na[[naname]]) 137 | } 138 | if( is.function(network::get.network.attribute(rval, "layout")) ) 139 | warning("network attribute 'layout' is a function, print the result might give errors") 140 | rval 141 | } 142 | -------------------------------------------------------------------------------- /revdep/failures.md: -------------------------------------------------------------------------------- 1 | # CEMiTool 2 | 3 |
4 | 5 | * Version: 6 | * GitHub: https://github.com/mbojan/intergraph 7 | * Source code: NA 8 | * Number of recursive dependencies: 0 9 | 10 |
11 | 12 | ## Error before installation 13 | 14 | ### Devel 15 | 16 | ``` 17 | 18 | There is a binary version available but the source version is later: 19 | binary source needs_compilation 20 | DBI 1.2.0 1.2.1 FALSE 21 | 22 | package 'AnnotationDbi' successfully unpacked and MD5 sums checked 23 | package 'ape' successfully unpacked and MD5 sums checked 24 | package 'aplot' successfully unpacked and MD5 sums checked 25 | package 'askpass' successfully unpacked and MD5 sums checked 26 | package 'backports' successfully unpacked and MD5 sums checked 27 | ... 28 | package 'processx' successfully unpacked and MD5 sums checked 29 | package 'promises' successfully unpacked and MD5 sums checked 30 | package 'ps' successfully unpacked and MD5 sums checked 31 | package 'purrr' successfully unpacked and MD5 sums checked 32 | package 'quantreg' successfully unpacked and MD5 sums checked 33 | package 'qvalue' successfully unpacked and MD5 sums checked 34 | package 'R6' successfully unpacked and MD5 sums checked 35 | package 'rappdirs' successfully unpacked and MD5 sums checked 36 | package 'RColorBrewer' successfully unpacked and MD5 sums checked 37 | package 'Rcpp' successfully unpacked and MD5 sums checked 38 | 39 | 40 | 41 | 42 | 43 | ``` 44 | ### CRAN 45 | 46 | ``` 47 | 48 | There is a binary version available but the source version is later: 49 | binary source needs_compilation 50 | DBI 1.2.0 1.2.1 FALSE 51 | 52 | package 'AnnotationDbi' successfully unpacked and MD5 sums checked 53 | package 'ape' successfully unpacked and MD5 sums checked 54 | package 'aplot' successfully unpacked and MD5 sums checked 55 | package 'askpass' successfully unpacked and MD5 sums checked 56 | package 'backports' successfully unpacked and MD5 sums checked 57 | ... 58 | package 'processx' successfully unpacked and MD5 sums checked 59 | package 'promises' successfully unpacked and MD5 sums checked 60 | package 'ps' successfully unpacked and MD5 sums checked 61 | package 'purrr' successfully unpacked and MD5 sums checked 62 | package 'quantreg' successfully unpacked and MD5 sums checked 63 | package 'qvalue' successfully unpacked and MD5 sums checked 64 | package 'R6' successfully unpacked and MD5 sums checked 65 | package 'rappdirs' successfully unpacked and MD5 sums checked 66 | package 'RColorBrewer' successfully unpacked and MD5 sums checked 67 | package 'Rcpp' successfully unpacked and MD5 sums checked 68 | 69 | 70 | 71 | 72 | 73 | ``` 74 | # gatom 75 | 76 |
77 | 78 | * Version: 79 | * GitHub: https://github.com/mbojan/intergraph 80 | * Source code: NA 81 | * Number of recursive dependencies: 0 82 | 83 |
84 | 85 | ## Error before installation 86 | 87 | ### Devel 88 | 89 | ``` 90 | 91 | There is a binary version available but the source version is later: 92 | binary source needs_compilation 93 | DBI 1.2.0 1.2.1 FALSE 94 | 95 | package 'AnnotationDbi' successfully unpacked and MD5 sums checked 96 | package 'askpass' successfully unpacked and MD5 sums checked 97 | package 'backports' successfully unpacked and MD5 sums checked 98 | package 'base64enc' successfully unpacked and MD5 sums checked 99 | package 'BH' successfully unpacked and MD5 sums checked 100 | ... 101 | package 'waldo' successfully unpacked and MD5 sums checked 102 | package 'withr' successfully unpacked and MD5 sums checked 103 | package 'xfun' successfully unpacked and MD5 sums checked 104 | package 'XML' successfully unpacked and MD5 sums checked 105 | package 'XVector' successfully unpacked and MD5 sums checked 106 | package 'yaml' successfully unpacked and MD5 sums checked 107 | package 'zlibbioc' successfully unpacked and MD5 sums checked 108 | 109 | The downloaded binary packages are in 110 | C:\Users\mbojanowski\AppData\Local\Temp\RtmpodA9uD\downloaded_packages 111 | 112 | 113 | trying URL 'https://bioconductor.org/packages/3.18/bioc/bin/windows/contrib/4.3/BioNet_1.62.0.zip' 114 | Content type 'application/zip' length 1389769 bytes (1.3 MB) 115 | ================================================== 116 | downloaded 1.3 MB 117 | 118 | trying URL 'https://bioconductor.org/packages/3.18/bioc/bin/windows/contrib/4.3/graph_1.80.0.zip' 119 | Content type 'application/zip' length 2168832 bytes (2.1 MB) 120 | ================================================== 121 | downloaded 2.1 MB 122 | 123 | ... 124 | ** help 125 | *** installing help indices 126 | ** building package indices 127 | ** testing if installed package can be loaded from temporary location 128 | ** testing if installed package can be loaded from final location 129 | ** testing if installed package keeps a record of temporary installation path 130 | * DONE (reactome.db) 131 | 132 | The downloaded source packages are in 133 | 'C:\Users\mbojanowski\AppData\Local\Temp\RtmpodA9uD\downloaded_packages' 134 | 135 | 136 | ``` 137 | ### CRAN 138 | 139 | ``` 140 | 141 | There is a binary version available but the source version is later: 142 | binary source needs_compilation 143 | DBI 1.2.0 1.2.1 FALSE 144 | 145 | package 'AnnotationDbi' successfully unpacked and MD5 sums checked 146 | package 'askpass' successfully unpacked and MD5 sums checked 147 | package 'backports' successfully unpacked and MD5 sums checked 148 | package 'base64enc' successfully unpacked and MD5 sums checked 149 | package 'BH' successfully unpacked and MD5 sums checked 150 | ... 151 | package 'waldo' successfully unpacked and MD5 sums checked 152 | package 'withr' successfully unpacked and MD5 sums checked 153 | package 'xfun' successfully unpacked and MD5 sums checked 154 | package 'XML' successfully unpacked and MD5 sums checked 155 | package 'XVector' successfully unpacked and MD5 sums checked 156 | package 'yaml' successfully unpacked and MD5 sums checked 157 | package 'zlibbioc' successfully unpacked and MD5 sums checked 158 | 159 | The downloaded binary packages are in 160 | C:\Users\mbojanowski\AppData\Local\Temp\RtmpodA9uD\downloaded_packages 161 | 162 | 163 | trying URL 'https://bioconductor.org/packages/3.18/bioc/bin/windows/contrib/4.3/BioNet_1.62.0.zip' 164 | Content type 'application/zip' length 1389769 bytes (1.3 MB) 165 | ================================================== 166 | downloaded 1.3 MB 167 | 168 | trying URL 'https://bioconductor.org/packages/3.18/bioc/bin/windows/contrib/4.3/graph_1.80.0.zip' 169 | Content type 'application/zip' length 2168832 bytes (2.1 MB) 170 | ================================================== 171 | downloaded 2.1 MB 172 | 173 | ... 174 | ** help 175 | *** installing help indices 176 | ** building package indices 177 | ** testing if installed package can be loaded from temporary location 178 | ** testing if installed package can be loaded from final location 179 | ** testing if installed package keeps a record of temporary installation path 180 | * DONE (reactome.db) 181 | 182 | The downloaded source packages are in 183 | 'C:\Users\mbojanowski\AppData\Local\Temp\RtmpodA9uD\downloaded_packages' 184 | 185 | 186 | ``` 187 | -------------------------------------------------------------------------------- /R/netcompare.R: -------------------------------------------------------------------------------- 1 | #' Comparing and testing network objects 2 | #' 3 | #' Compare or test network objects for (near) equality. 4 | #' 5 | #' Arguments \code{target} and \code{current} can be network objects of one of 6 | #' the supported classes. They do not have to be of the same class though. 7 | #' 8 | #' The function does a series of comparisons between \code{target} and 9 | #' \code{current}: 10 | #' 11 | #' \enumerate{ 12 | #' \item The network structure comparison is made based on adjacency matrices 13 | #' (mind this when using for huge networks). 14 | #' 15 | #' \item Network/edge/vertex attributes are checked for presence in both 16 | #' objects. 17 | #' 18 | #' \item Common network/edge/vertex attribures are checked for equality. 19 | #' } 20 | 21 | #' All the results are collected in a list of class \code{netcompare} with an 22 | #' associated \code{print} method. 23 | #' 24 | #' If \code{test} is TRUE then instead of the detailed test results the function 25 | #' returns TRUE or FALSE depending on some of the checks resulted positively. 26 | #' Currently attribute checks are ignored, i.e. what is taken into account is: 27 | #' 28 | #' \enumerate{ 29 | #' \item Equivalence of adjacency matrices 30 | #' \item Directed / undirected character of the network 31 | #' \item Edge set size 32 | #' \item Vertex set size 33 | #' } 34 | #' 35 | #' @param target,current network objects, currently \code{network} and 36 | #' \code{igraph} classes are supported 37 | #' 38 | #' @param test logical, whether to perform the test or return comparison data, 39 | #' see Details 40 | #' 41 | #' @param \dots other arguments, currently ignored 42 | #' 43 | #' @return Depending on the value of \code{test} either an object of class 44 | #' \code{netcompare} containing the results of all the tests (if 45 | #' \code{test=FALSE}) or (if \code{test=TRUE}) a logical whether or not the 46 | #' networks are (nearly) the same. 47 | #' 48 | #' @seealso \code{\link{all.equal}}, \code{\link{identical}} 49 | #' 50 | #' @export 51 | #' 52 | #' @examples 53 | #' 54 | #'netcompare( asIgraph(exNetwork), exNetwork) 55 | #'netcompare( asIgraph(exNetwork), exNetwork, test=TRUE) 56 | #' 57 | netcompare <- function(target, current, test=FALSE, ...) 58 | { 59 | # trivial checks 60 | rval <- list() 61 | # class 62 | rval$class <- c(target=class(target), current=class(current)) 63 | # number of vertices 64 | rval$vcount <- c(target=igVcount(target), current=igVcount(current)) 65 | # number of edges 66 | rval$ecount <- c( target=igEcount(target), current=igEcount(current)) 67 | # directedness 68 | rval$directed <- c( target=igDirected(target), current=igDirected(current)) 69 | # compare adjacency matrices 70 | rval$identical_am <- compareEdges(target, current) 71 | # compare attributes 72 | targeta <- dumpAttr(target) 73 | currenta <- dumpAttr(current) 74 | rval$network <- compareAttributes( targeta$network, currenta$network) 75 | rval$vertex <- compareAttributes( targeta$vertex, currenta$vertex) 76 | rval$edge <- compareAttributes( targeta$edge, currenta$edge) 77 | rval <- structure(rval, class="netcompare") 78 | if(test) 79 | compareTest(rval) 80 | else return(rval) 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | #' @export 89 | print.netcompare <- function(x, ...) 90 | { 91 | cat("\n") 92 | cat("Identical adjacency matrices:\n") 93 | cat( paste(x$identical_am, collapse=", "), "\n", fill=TRUE, labels=" ") 94 | cat("Network-level features:\n") 95 | m <- do.call("rbind", lapply(x[c("vcount", "ecount", "directed")], format)) 96 | print(m, quote=FALSE) 97 | cat("\n") 98 | cat("Presence of network-level attributes\n") 99 | print(x$network) 100 | cat("\n") 101 | cat("Presence of vertex-level attributes\n") 102 | print(x$vertex) 103 | cat("\n") 104 | cat("Presence of edge-level attributes\n") 105 | print(x$edge) 106 | } 107 | 108 | # comparing and testing for (near) equality of networks 109 | # 110 | # Result of comparison: 111 | # 112 | # 1. computed network-level comparisons 113 | # 114 | # 2. built-in network-level comparisons 115 | # 116 | # 3. attributes 117 | # 118 | # 3a. attribute presence 119 | # 120 | # 3b. identical common attributes 121 | # 122 | # NOTE: Makes use of non-exported generic functions 123 | 124 | 125 | 126 | compareTest <- function(object) 127 | { 128 | stopifnot(inherits(object, "netcompare")) 129 | rval <- logical() 130 | rval["adjacency"] <- object$identical_am 131 | rval["vcount"] <- object$vcount[1] == object$vcount[2] 132 | rval["ecount"] <- object$ecount[1] == object$ecount[2] 133 | rval["directed"] <- object$directed[1] == object$directed[2] 134 | all(rval) 135 | } 136 | 137 | 138 | compareEdges <- function(target, current, use.names=FALSE) 139 | { 140 | op <- igraph::igraph_options(sparsematrices = FALSE) 141 | on.exit(igraph::igraph_options(op)) 142 | tr <- try(utils::getS3method("as.matrix", class=class(target)), silent=TRUE) 143 | if(inherits(tr, "try-error")) 144 | stop("cannot find 'as.matrix' method for class ", dQuote(class(target))) 145 | tr <- try(utils::getS3method("as.matrix", class=class(current)), silent=TRUE) 146 | if(inherits(tr, "try-error")) 147 | stop("cannot find 'as.matrix' method for class ", dQuote(class(current))) 148 | mtar <- as.matrix(target, "adjacency") 149 | mcur <- as.matrix(current, "adjacency") 150 | # compare matrices (no dimnames) 151 | if(use.names) 152 | all.equal(mtar, mcur) 153 | else all.equal( structure(mtar, dimnames=NULL), structure(mcur, 154 | dimnames=NULL) ) 155 | } 156 | 157 | 158 | # compare common components of a list (by name) 159 | # return a list of all.equal results 160 | compareAlist <- function(target, current) 161 | { 162 | # common components 163 | nams <- intersect(names(target), names(current)) 164 | if( length(nams) == 0 ) 165 | return(as.character(NA)) 166 | rval <- lapply(nams, function(n) all.equal( target[[n]], current[[n]]) ) 167 | names(rval) <- nams 168 | rval 169 | } 170 | 171 | 172 | 173 | # Compare lists of attributes (as returned by 'dumpAttr') 174 | 175 | compareAttributes <- function(target, current) 176 | { 177 | rval <- list() 178 | # compare number of attributes 179 | rval$n <- c(target=length(target), current=length(current)) 180 | # Check for attributes by name 181 | pre <- list() 182 | u <- union(names(target), names(current)) 183 | if(length(u) == 0L) { 184 | pre <- matrix(NA, 0, 2, dimnames = list(NULL, c("target", "current"))) 185 | } else { 186 | r <- t(sapply(u, function(a) 187 | c( a %in% names(target), 188 | a %in% names(current) ) 189 | )) 190 | pre <- c(pre, list(r)) 191 | pre <- do.call("rbind", pre) 192 | dimnames(pre) <- list(rownames(pre), c("target", "current")) 193 | } 194 | rval$presence <- pre 195 | rval$bycomp <- compareAlist(target, current) 196 | structure(rval, class="netcomparea") 197 | } 198 | 199 | 200 | # Print method for the result of 'compareAttributes' 201 | #' @export 202 | print.netcomparea <- function(x, ...) 203 | { 204 | m <- do.call("rbind", lapply( x[c("n", "presence")], format)) 205 | print(m, quote=FALSE) 206 | cat("Common attributes comparison (TRUE=identical)\n") 207 | if( identical( x$bycomp, as.character(NA)) ) 208 | { 209 | cat(" No common attributes\n") 210 | } else 211 | { 212 | l <- sapply(x$bycomp, paste, collapse=", ") 213 | for(i in seq(along=l)) 214 | cat(names(l)[i], ":", l[i], fill=TRUE, labels=" ") 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /vignettes/howto.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Short `intergraph` tutorial" 3 | author: "Michał Bojanowski" 4 | output: 5 | rmarkdown::html_vignette: 6 | toc: true 7 | number_sections: true 8 | vignette: > 9 | %\VignetteEngine{knitr::rmarkdown} 10 | %\VignetteIndexEntry{Short intergraph tutorial} 11 | %\VignetteEncoding{UTF-8} 12 | --- 13 | 14 | 17 | 18 | ```{r, setup, include=FALSE} 19 | library(intergraph) 20 | library(knitr) 21 | 22 | set.seed(123) 23 | ``` 24 | 25 | - - - 26 | 27 | 28 | 29 | "Intergraph" is an R package with coercion routines for netowrk data objects. For more information, see 30 | 31 | * Homepage on [https://mbojan.github.io/intergraph/](https://mbojan.github.io/intergraph/). 32 | * Package development pages on [https://github.com/mbojan/intergraph](https://github.com/mbojan/intergraph). 33 | 34 | This is a short tutorial showing how to use functions in package "intergraph" 35 | using some example network data contained in the package. 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | # Loading example data 44 | 45 | To show the data, first load the packages. 46 | 47 | ```{r,packages} 48 | library(intergraph) 49 | library(network) 50 | library(igraph) 51 | ``` 52 | 53 | Now, these are the summaries of the "igraph" objects: 54 | ```{r, summarize-igraph} 55 | summary(exIgraph) 56 | summary(exIgraph2) 57 | ``` 58 | 59 | These are the summaries of the "network" objects: 60 | ```{r,summarize-network} 61 | exNetwork 62 | exNetwork2 63 | ``` 64 | 65 | More information is available in the Appendix. 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | # Functions `asNetwork` and `asIgraph` 79 | 80 | Conversion of network objects between classes "network" and "igraph" can be 81 | performed using functions `asNetwork` and `asIgraph`. 82 | 83 | 84 | ## network => igraph 85 | 86 | Converting "network" objects to "igraph" is done by calling function 87 | `asIgraph` on a "network" object: 88 | ```{r,network2igraph} 89 | # check class of 'exNetwork' 90 | class(exNetwork) 91 | # convert to 'igraph' 92 | g <- asIgraph(exNetwork) 93 | # check class of the result 94 | class(g) 95 | ``` 96 | 97 | Check if edgelists of the objects are identical 98 | ```{r} 99 | el.g <- get.edgelist(g) 100 | el.n <- as.matrix(exNetwork, "edgelist") 101 | identical( as.numeric(el.g), as.numeric(el.n)) 102 | ``` 103 | 104 | 105 | ## igraph => network 106 | 107 | Converting "igraph" objects to "network" is done by calling function 108 | `asNetwork` on an "igraph" object: 109 | ```{r,igraph2network} 110 | net <- asNetwork(exIgraph) 111 | ``` 112 | Note the warning because of a "non-standard" network attribute `layout`, which 113 | is a function. Printing "network" objects does not handle non-standard 114 | attributes very well. However, all the data and attributes are copied 115 | correctly. 116 | 117 | Check if edgelists of the objects are identical 118 | ```{r} 119 | el.g2 <- get.edgelist(exIgraph) 120 | el.n2 <- as.matrix(net, "edgelist") 121 | identical( as.numeric(el.g2), as.numeric(el.n2)) 122 | ``` 123 | 124 | 125 | 126 | ## Handling attributes 127 | 128 | Objects of class "igraph" and "network", apart from storing actual network data 129 | (vertexes and edges), allow for adding attributes of vertexes, edges, and 130 | attributes of the network as a whole (called "network attributes" or "graph 131 | attributes" in the nomenclatures of packages "network" and "igraph" 132 | respectively). 133 | 134 | Vertex and edge attributes are used by "igraph" and "network" in a largely 135 | similar fashion. However, network-level attributes are used differently. 136 | Objects of class "network" use network-level attributes to store various 137 | metadata, e.g., network size, whether the network is directed, is bipartite, etc. 138 | In "igraph" this information is stored separately. 139 | 140 | The above difference affects the way the attributes are copied when we convert 141 | "network" and "igraph" objects into one another. 142 | 143 | Both functions `asNetwork` and `asIgraph` have an additional argument `attrmap` 144 | that is used to specify how vertex, edge, and network attributes are copied. 145 | The `attrmap` argument requires a data frame. Rows of that data frame specify 146 | rules of copying/renaming different attributes. The data frame should have the 147 | following columns (all of class "character"): 148 | 149 | * `type`: one of "network", "vertex" or "edge", whether the rule applies to 150 | network, vertex or edge attribute. 151 | * `fromslc`: name of the which we are *converting from* 152 | * `fromattr`: name of the attribute in the object we are converting from 153 | * `tocls`: name of the class of the object we are *converting to* 154 | * `toattr`: name of the attribute in the object we are converting to 155 | 156 | The default rules are returned by a function `attrmap()`, these are: 157 | 158 | ```{r attrmap-defaults} 159 | attrmap() 160 | ``` 161 | 162 | For example, the last row specifies a rule that when an object of class 163 | "igraph" is converted to class "network", then a vertex attribute `name` in the 164 | "igraph" object will be copied to a vertex attribute called `vertex.names` in 165 | the resulting object of class "network. 166 | 167 | If the column `toattr` contains an `NA`, that means that the corresponding 168 | attribute is not copied. For example, the first row specifies a rule that when 169 | an object of class "network" is converted to class "igraph", then a network 170 | attribute `directed` in the "network" object is *not* copied to the resulting 171 | object of class "igraph". 172 | 173 | Users can customize the rules, or add new ones, by constructing similar 174 | data frames and supplying them through argument `attrmap` to functions 175 | `asIgraph` and `asNetwork`. 176 | 177 | 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`: 178 | 179 | ```{r attrmap-example-rules} 180 | new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na", 181 | tocls="igraph", toattr=NA, 182 | stringsAsFactors=FALSE) 183 | # combine with the default rules 184 | rules <- rbind( attrmap(), new_rule ) 185 | rules 186 | ``` 187 | 188 | Now we can use it with `asIgraph`: 189 | 190 | ```{r attrmap-example} 191 | (ig1 <- asIgraph(exNetwork)) 192 | (ig2 <- asIgraph(exNetwork, amap=rules)) 193 | 194 | # check if "na" was dropped 195 | "na" %in% igraph::vertex_attr_names(ig1) 196 | "na" %in% igraph::vertex_attr_names(ig2) 197 | ``` 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | # Network objects to/from data frames 208 | 209 | Function `asDF` can be used to convert network object (of class "igraph" or "network") 210 | to a list of two data frames: 211 | 212 | ```{r asDF} 213 | l <- asDF(exIgraph) 214 | str(l) 215 | ``` 216 | 217 | The resulting list has two components `edges` and `vertexes`. The `edges` component is essentially 218 | an edge list containing ego and alter ids in the first two columns. The remaining columns store 219 | edge attributes (if any). For our example data it is 220 | 221 | ```{r show-edgedb} 222 | l$edges 223 | ``` 224 | 225 | The `vertexes` component contains data on vertexes with vertex id (the same 226 | that is used in the first two column of `edges`) is stored in the first two 227 | columns. The remaining columns store vertex attributes (if any). For our 228 | example data it is: 229 | 230 | ```{r show-vertexdb} 231 | l$vertexes 232 | ``` 233 | 234 | Functions `asNetwork` and `asIgraph` can also be used to create network objects 235 | from data frames such as those above. The first argument should be an edge list data frame. 236 | Optional argument `vertices` expectes data frames with vertex data (just like `l$vertexes`). 237 | Additionally we need to specify whether the edges should be interpreted as directed or not 238 | through the argument `directed`. 239 | 240 | For example, to create an object of class "network" from the dataframes created above from 241 | object `exIgraph` we can: 242 | 243 | ```{r fromdf} 244 | z <- asNetwork(l$edges, directed=TRUE, l$vertexes) 245 | z 246 | ``` 247 | 248 | This is actually what basically happens when we call `asNetwork(exIgraph)` 249 | 250 | 251 | 252 | - - - 253 | 254 | 255 | # Appendix 256 | 257 | ## Example networks 258 | 259 | Package intergraph contains four example networks: 260 | 261 | * Objects `exNetwork` and `exIgraph` contain the same *directed* network as objects of class "network" and "igraph" respectively. 262 | * Objects `exNetwork2` and `exIgraph2` contain the same *undirected* network as objects of class "network" and "igraph" respectively. 263 | 264 | All four datasets contain: 265 | 266 | * A vertex attribute `label` with vertex labels. These are letters from `a` to `o`. 267 | * An edge attribute `label` with edge labels. These are pasted letters of the adjecent nodes. 268 | 269 | We will use them in the examples below. 270 | 271 | 272 | Networks are shown below using the following code: 273 | 274 | ```{r showdata-code,eval=FALSE} 275 | layout(matrix(1:4, 2, 2, byrow=TRUE)) 276 | op <- par(mar=c(1,1,2,1)) 277 | # compute layout 278 | coords <- layout.fruchterman.reingold(exIgraph) 279 | plot(exIgraph, main="exIgraph", layout=coords) 280 | plot(exIgraph2, main="exIgraph2", layout=coords) 281 | plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords) 282 | plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords) 283 | par(op) 284 | ``` 285 | 286 | ```{r showdata-pic, ref.label="showdata-code",echo=FALSE,fig.height=10,fig.width=10} 287 | ``` 288 | 289 | 290 | 291 | 292 | ## Session information 293 | 294 | 295 | ```{r, session_info} 296 | sessionInfo() 297 | ``` 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | --------------------------------------------------------------------------------