@@ -366,6 +366,13 @@ decode_communities <- function(community_id,
366366 edge_filter ,
367367 node_filter ) {
368368
369+ if (missing(edge_filter )) {
370+ edge_filter <- NULL
371+ }
372+ if (missing(node_filter )) {
373+ node_filter <- NULL
374+ }
375+
369376 apply_op <- function (vec , op , val ) {
370377 ops <- list (" ==" = `==` ,
371378 " !=" = `!=` ,
@@ -392,7 +399,7 @@ decode_communities <- function(community_id,
392399 }
393400
394401 # consider edges
395- if (nrow (edge_filter )!= 0 ) {
402+ if (is.null (edge_filter )== FALSE ) {
396403 # this is where the edge filter results will be kept
397404 etm <- matrix (data = 0 ,
398405 nrow = nrow(edge_filter ),
@@ -416,29 +423,54 @@ decode_communities <- function(community_id,
416423 }
417424
418425 # now partition based on node-attributes
419- vs <- as_data_frame(x = graph , what = " vertices" )
420- V(graph )$ key <- apply(X = vs [, node_filter $ name , drop = FALSE ],
421- MARGIN = 1 , FUN = paste , collapse = ' |' )
422- sgs <- lapply(
423- X = unique(V(graph )$ key ), g = graph ,
424- FUN = function (x , g ) {
425- # get a subgraph with shared node attributes
426- sg <- subgraph(graph = g , vids = which(V(g )$ key == x ))
427-
428- # find connected components
429- V(sg )$ components <- components(graph = sg )$ membership
430- V(sg )$ component_id <- paste0(V(sg )$ key , ' |' ,
431- V(sg )$ components )
432- V(sg )$ component_id <- as.numeric(as.factor(V(sg )$ component_id ))
433- return (disjoint_union(lapply(
434- X = unique(V(sg )$ component_id ), g = sg ,
435- FUN = function (x , g ) {
436- vids <- which(V(g )$ component_id == x )
437- return (subgraph(graph = g , vids = vids ))
438- })))
439- })
440- sgs <- disjoint_union(sgs )
441-
442- return (sgs )
426+ if (is.null(node_filter )== FALSE ) {
427+ vs <- as_data_frame(x = graph , what = " vertices" )
428+ V(graph )$ key <- apply(X = vs [, node_filter $ name , drop = FALSE ],
429+ MARGIN = 1 , FUN = paste , collapse = ' |' )
430+
431+ sgs <- lapply(
432+ X = unique(V(graph )$ key ), g = graph ,
433+ FUN = function (x , g ) {
434+ # get a subgraph with shared node attributes
435+ sg <- subgraph(graph = g , vids = which(V(g )$ key == x ))
436+
437+ # find connected components
438+ V(sg )$ components <- components(graph = sg )$ membership
439+ V(sg )$ component_id <- paste0(V(sg )$ key , ' |' ,
440+ V(sg )$ components )
441+ V(sg )$ component_id <- as.numeric(as.factor(V(sg )$ component_id ))
442+ return (disjoint_union(lapply(
443+ X = unique(V(sg )$ component_id ), g = sg ,
444+ FUN = function (x , g ) {
445+ vids <- which(V(g )$ component_id == x )
446+ return (subgraph(graph = g , vids = vids ))
447+ })))
448+ })
449+ sgs <- disjoint_union(sgs )
450+ return (sgs )
451+ }
452+ else {
453+ sgs <- lapply(
454+ X = unique(V(graph )$ community ), g = graph ,
455+ FUN = function (x , g ) {
456+ # get a subgraph with shared node attributes
457+ sg <- subgraph(graph = g , vids = which(V(g )$ community == x ))
458+
459+ # find connected components
460+ V(sg )$ components <- components(graph = sg )$ membership
461+ V(sg )$ component_id <- paste0(V(sg )$ key , ' |' ,
462+ V(sg )$ components )
463+ V(sg )$ component_id <- as.numeric(as.factor(V(sg )$ component_id ))
464+ return (disjoint_union(lapply(
465+ X = unique(V(sg )$ component_id ), g = sg ,
466+ FUN = function (x , g ) {
467+ vids <- which(V(g )$ component_id == x )
468+ return (subgraph(graph = g , vids = vids ))
469+ })))
470+ })
471+ sgs <- disjoint_union(sgs )
472+ return (sgs )
473+
474+ }
443475}
444476
0 commit comments