Skip to content

Commit 85ca20d

Browse files
committed
Merge pull request #52 from statnet/ebactiveplot
interactive nw plot
2 parents e1566f7 + f140382 commit 85ca20d

2 files changed

Lines changed: 92 additions & 6 deletions

File tree

inst/shiny/server.R

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ nw <- reactive({
463463
nw_var
464464
})
465465

466+
elist <- reactive({
467+
if(!is.network(nwinit())) return()
468+
as.edgelist(nw())
469+
})
466470

467471
#get coordinates to plot network with
468472
coords <- reactive({
@@ -547,7 +551,7 @@ vcol <- reactive({
547551
if(!is.network(nw())){return()}
548552
nw_var <- nw()
549553
if(input$colorby == 2){
550-
vcol <- 2
554+
vcol <- rep(2, nodes())
551555
} else {
552556
full_list <- get.vertex.attribute(nw_var,input$colorby)
553557
short_list <- sort(unique(full_list))
@@ -1303,24 +1307,93 @@ output$nwplot <- renderPlot({
13031307

13041308
nw_var <- nw()
13051309
color <- adjustcolor(vcol(), alpha.f = input$transp)
1310+
ecolor <- 1
1311+
vborder <- 1
1312+
vcex <- nodesize()
13061313
if(is.bipartite(nw())){
13071314
sides <- c(rep(50, nw()$gal$bipartite),
13081315
rep(3, nodes() - nw()$gal$bipartite))
13091316
} else{
13101317
sides <- 50
13111318
}
13121319

1320+
if(!is.null(values$hoverpoints)){
1321+
if(nrow(values$hoverpoints) > 0){
1322+
nhov <- as.numeric(rownames(values$hoverpoints))
1323+
vcex <- rep(1, nodes())
1324+
vcex[nhov] <- 2
1325+
}
1326+
}
1327+
if(!is.null(values$clickedpoints)){
1328+
if(nrow(values$clickedpoints) > 0){
1329+
nclick <- as.numeric(rownames(values$clickedpoints))
1330+
color <- adjustcolor(vcol(), alpha.f = 0.4)
1331+
color[nclick] <- vcol()[nclick]
1332+
ecolor <- "lightgrey"
1333+
vborder <- rep("lightgrey", nodes())
1334+
vborder[nclick] <- 1
1335+
}
1336+
}
1337+
if(!is.null(values$dblclickpoints)){
1338+
if(nrow(values$dblclickpoints) > 0){
1339+
ndbl <- as.numeric(rownames(values$dblclickpoints))
1340+
neighb <- nw()[ndbl,] == 1
1341+
color <- adjustcolor(vcol(), alpha.f = 0.4)
1342+
color[ndbl] <- vcol()[ndbl]
1343+
ecolor <- rep("lightgrey", nedgesinit())
1344+
ecolor[apply(elist(), MARGIN = 1, FUN = function(x){any(x == ndbl)})] <- "black"
1345+
vborder <- rep("lightgrey", nodes())
1346+
vborder[neighb] <- "black"
1347+
vborder[ndbl] <- "black"
1348+
}
1349+
}
1350+
13131351
par(mar = c(0, 0, 0, 0))
13141352
plot.network(nw_var, coord = coords(),
13151353
displayisolates = input$iso,
13161354
displaylabels = input$vnames,
13171355
vertex.col = color,
1356+
vertex.border = vborder,
13181357
vertex.sides = sides,
1319-
vertex.cex = nodesize())
1358+
vertex.cex = vcex,
1359+
edge.col = ecolor)
13201360
if(input$colorby != 2){
1321-
legend('bottomright', title = input$colorby, legend = legendlabels(), fill = legendfill(),
1322-
bty='n')
1361+
legend('bottomright', title = input$colorby, legend = legendlabels(),
1362+
fill = legendfill(), bty='n')
1363+
}
1364+
1365+
if(!is.null(values$clickedpoints)){
1366+
if(nrow(values$clickedpoints) > 0){
1367+
#isolate(legend("topleft",
1368+
# legend = values$clickedpoints[, c("Names", menuattr())]))
1369+
cx <- values$clickedpoints[, "cx"]
1370+
cy <- values$clickedpoints[, "cy"]
1371+
name <- values$clickedpoints[, "Names"]
1372+
attrlabel <- paste("\n", menuattr())
1373+
text(x = cx, y = cy,
1374+
labels = paste0(name,
1375+
paste(attrlabel, values$clickedpoints[, menuattr()],
1376+
collapse = "")),
1377+
pos = 4, offset = 1)
1378+
}
13231379
}
1380+
1381+
})
1382+
1383+
observeEvent({c(input$plot_click, input$plot_dblclick)}, {
1384+
values$clickedpoints <- nearPoints(nwdf(), input$plot_click,
1385+
xvar = "cx", yvar = "cy",
1386+
threshold = 10, maxpoints = 1)
1387+
})
1388+
observeEvent(input$plot_hover, {
1389+
values$hoverpoints <- nearPoints(nwdf(), input$plot_hover,
1390+
xvar = "cx", yvar = "cy",
1391+
threshold = 10, maxpoints = 1)
1392+
})
1393+
observeEvent({c(input$plot_dblclick, input$plot_click)}, {
1394+
values$dblclickpoints <- nearPoints(nwdf(), input$plot_dblclick,
1395+
xvar = "cx", yvar = "cy",
1396+
threshold = 10, maxpoints = 1)
13241397
})
13251398

13261399

@@ -1343,7 +1416,8 @@ output$nwplotdownload <- downloadHandler(
13431416
}
13441417
)
13451418

1346-
output$attrtbl <- renderDataTable({
1419+
#dataframe of nodes, their attributes, and their coordinates in nwplot
1420+
nwdf <- reactive({
13471421
attrs <- menuattr()
13481422
if(is.na(as.numeric(network.vertex.names(nw()))[1])){
13491423
df <- data.frame(Names = network.vertex.names(nw()))
@@ -1354,6 +1428,13 @@ output$attrtbl <- renderDataTable({
13541428
df[[attrs[i]]] <- get.vertex.attribute(nw(), attrs[i])
13551429
}
13561430
df[["Missing"]] <- get.vertex.attribute(nw(), "na")
1431+
df[["cx"]] <- coords()[,1]
1432+
df[["cy"]] <- coords()[,2]
1433+
df
1434+
})
1435+
1436+
output$attrtbl <- renderDataTable({
1437+
df <- nwdf()
13571438
dt <- df[, c("Names", input$attribcols)]
13581439
dt
13591440
}, options = list(pageLength = 10))

inst/shiny/ui.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,12 @@ fluidRow(
493493
column(7,
494494
tabsetPanel(id='plottabs',
495495
tabPanel('Network Plot', br(),
496-
plotOutput('nwplot')
496+
plotOutput('nwplot', click = "plot_click",
497+
dblclick = dblclickOpts(id = "plot_dblclick"),
498+
hover = hoverOpts(id = "plot_hover", delay = 100,
499+
delayType = "throttle"),
500+
brush = brushOpts(id = "plot_brush")
501+
)
497502
),
498503
tabPanel('Attributes', br(),
499504
dataTableOutput("attrtbl")

0 commit comments

Comments
 (0)