Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add conversion to igraph #98

Open
thibautjombart opened this issue May 3, 2019 · 3 comments
Open

add conversion to igraph #98

thibautjombart opened this issue May 3, 2019 · 3 comments

Comments

@thibautjombart
Copy link
Contributor

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.

@zkamvar
Copy link
Member

zkamvar commented May 3, 2019

@zkamvar zkamvar closed this as completed May 3, 2019
@thibautjombart thibautjombart reopened this May 3, 2019
@thibautjombart
Copy link
Contributor Author

thibautjombart commented May 3, 2019

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)

@zkamvar
Copy link
Member

zkamvar commented May 7, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants