epicontacts
epicontacts copied to clipboard
add conversion to igraph
It'd be useful to have something along the lines of as.igraph(x)
where x
is an epicontact object so that we can readily perform graph operations. One thing to bear in mind is directionality should be preserved.
http://www.repidemicsconsortium.org/epicontacts/reference/as.igraph.epicontacts.html
I thought it did exist. Here's the issue:
> epicontacts:::as.igraph.epicontacts
function (x)
{
all_ids <- data.frame(id = get_id(x, "all"), stringsAsFactors = FALSE)
verts <- dplyr::full_join(x$linelist, all_ids, by = "id")
if ("name" %in% colnames(verts)) {
verts$epicontacts_name <- verts$name
verts$name <- NULL
}
missing_contacts <- anyNA(x$contacts$from) || anyNA(x$contacts$to)
missing_vertex <- anyNA(x$linelist$id)
if (missing_contacts && !missing_vertex) {
verts <- dplyr::add_row(verts, id = NA)
}
net <- igraph::graph_from_data_frame(x$contacts, vertices = verts,
directed = x$directed)
igraph::vertex_attr(net)$id <- igraph::vertex_attr(net)$name
return(net)
}
<bytecode: 0x563029ef5538>
<environment: namespace:epicontacts>
> epicontacts::as.igraph.epicontacts
Error: 'as.igraph.epicontacts' is not an exported object from 'namespace:epicontacts'
>
Make sure it is exported? And that there is a generic as.igraph
(it is defined by igraph)
The issue here is that it IS exported, but igraph needs to be loaded for it to be recognised as a generic:
library('epicontacts')
x <- make_epicontacts(outbreaks::ebola_sim$linelist,
outbreaks::ebola_sim$contacts,
id = "case_id", to = "case_id", from ="infector",
directed = TRUE)
as.igraph(x)
#> Error in as.igraph(x): could not find function "as.igraph"
library('igraph')
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
as.igraph(x)
#> IGRAPH 8ae8e6a DN-- 7047 3800 --
#> + attr: name (v/c), generation (v/n), date_of_infection (v/n),
#> | date_of_onset (v/n), date_of_hospitalisation (v/n),
#> | date_of_outcome (v/n), outcome (v/c), gender (v/c), hospital
#> | (v/c), lon (v/n), lat (v/n), id (v/c), source (e/c)
#> + edges from 8ae8e6a (vertex names):
#> [1] d1fafd->53371b cac51e->f5c3d8 f5c3d8->0f58c4 0f58c4->881bd4
#> [5] 8508df->40ae5f 127d83->f547d6 f5c3d8->d58402 20b688->d8a13d
#> [9] 2ae019->a3c8b8 20b688->974bc1 8508df->6a9004 2ae019->72b905
#> [13] 40ae5f->b8f2fd f1f60f->09e386 f9149b->6285c9 f547d6->865581
#> [17] f547d6->5fe599 f9149b->79ad06 aec8ec->c43190 d58402->900021
#> + ... omitted several edges
Created on 2019-05-07 by the reprex package (v0.2.1)