[codeface] [PATCH 2/6] Factor out helper functions from vis.clusters.r

  • From: Wolfgang Mauerer <wolfgang.mauerer@xxxxxxxxxxxxxxxxx>
  • To: <codeface@xxxxxxxxxxxxx>
  • Date: Sat, 21 Nov 2015 21:45:56 +0100

prepare.clusters and gen.clusters.list are also useful
outside the web front-end, so move them to clusters.r.

Signed-off-by: Wolfgang Mauerer <wolfgang.mauerer@xxxxxxxxxxxxxxxxx>
---
codeface/R/clusters.r | 42 +++++++++++++++++++++++++++++++++
codeface/R/shiny/widgets/vis.clusters.r | 42 ---------------------------------
2 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/codeface/R/clusters.r b/codeface/R/clusters.r
index 7d3896e..586e5a0 100644
--- a/codeface/R/clusters.r
+++ b/codeface/R/clusters.r
@@ -69,3 +69,45 @@ annotate.cluster <- function(g) {
## page rank distribution and mean page rank.
return(g)
}
+
+prepare.clusters <- function(con, pid, range.id) {
+ l <- query.cluster.ids.con(con, pid, range.id, "Spin Glass Community")
+ clusters.list <- gen.clusters.list(l, con)
+
+ ## Sort the clusters by number of vertices
+ sizes <- sapply(clusters.list, vcount)
+ clusters.list <- clusters.list[sort(sizes, index.return=TRUE,
decreasing=TRUE)$ix]
+
+ max.length <- 8
+ if (length(clusters.list) < max.length) {
+ max.length <- length(clusters.list)
+ }
+
+ return(clusters.list[1:max.length])
+}
+
+gen.clusters.list <- function(l, con) {
+ clusters.list <- lapply(1:length(l), function(i) {
+ g <- construct.cluster(con, l[[i]])
+
+ ## Self-loops in the proximity analysis can become very strong;
+ ## the resulting edges then typically destroy the visualisation
+ ## completely. Get rid of them, thus..
+ ## NOTE: simplify must be called before the cluster is annotated
+ ## because the function
+ if(!is.null(g)) {
+ g <- simplify(g, remove.loops=TRUE)
+ }
+
+ return(g)
+ })
+
+ ## Remove empty clusters
+ clusters.list[sapply(clusters.list, is.null)] <- NULL
+
+ clusters.list <- lapply(clusters.list, function(g) {
+ return(annotate.cluster(g))
+ })
+
+ return(clusters.list)
+}
diff --git a/codeface/R/shiny/widgets/vis.clusters.r
b/codeface/R/shiny/widgets/vis.clusters.r
index a782f54..3c3a286 100644
--- a/codeface/R/shiny/widgets/vis.clusters.r
+++ b/codeface/R/shiny/widgets/vis.clusters.r
@@ -24,53 +24,11 @@ source("../../clusters.r", chdir=TRUE)
## Create overviews about the types of collaboration graphs appearing in
## projects.

-gen.clusters.list <- function(l, con) {
- clusters.list <- lapply(1:length(l), function(i) {
- g <- construct.cluster(con, l[[i]])
-
- ## Self-loops in the proximity analysis can become very strong;
- ## the resulting edges then typically destroy the visualisation
- ## completely. Get rid of them, thus.
- ## NOTE: simplify must be called before the cluster is annotated
- ## because the function
- if(!is.null(g)) {
- g <- simplify(g, remove.loops=TRUE)
- }
-
- return(g)
- })
-
- ## Remove empty clusters
- clusters.list[sapply(clusters.list, is.null)] <- NULL
-
- clusters.list <- lapply(clusters.list, function(g) {
- return(annotate.cluster(g))
- })
-
- return(clusters.list)
-}
-
do.plot <- function(g) {
V(g)$name <- NA
plot(g)
}

-prepare.clusters <- function(con, pid, range.id) {
- l <- query.cluster.ids.con(con, pid, range.id, "Spin Glass Community")
- clusters.list <- gen.clusters.list(l, con)
-
- ## Sort the clusters by number of vertices
- sizes <- sapply(clusters.list, vcount)
- clusters.list <- clusters.list[sort(sizes, index.return=TRUE,
decreasing=TRUE)$ix]
-
- max.length <- 8
- if (length(clusters.list) < max.length) {
- max.length <- length(clusters.list)
- }
-
- return(clusters.list[1:max.length])
-}
-
do.cluster.plots <- function(clusters.list) {
par(mfcol=c(2,4))
for (i in 1:length(clusters.list)) {
--
2.1.4


Other related posts:

  • » [codeface] [PATCH 2/6] Factor out helper functions from vis.clusters.r - Wolfgang Mauerer