Skip to content

Commit 1da72fa

Browse files
committed
Merge pull request #53 from statnet/production
attribute histograms
2 parents 85ca20d + 4d2d5fe commit 1da72fa

4 files changed

Lines changed: 91 additions & 36 deletions

File tree

inst/shiny/global.R

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ inlineSelectInput <- function(inputId, label, choices, ...) {
2727
} else {
2828
labeldisp <- "display: inline;"
2929
}
30-
30+
3131
tagList(tags$label(label, `for` = inputId, style = labeldisp),
32-
tags$select(id = inputId, choices = choices, ...,
33-
class = "shiny-bound-input inlineselect",
32+
tags$select(id = inputId, choices = choices, ...,
33+
class = "shiny-bound-input inlineselect",
3434
lapply(choices, tags$option)))
3535
}
3636

@@ -71,8 +71,8 @@ cugstats <- function(x, term, directed, loops) {
7171
}
7272

7373

74-
# Takes an ergm object and gathers some of the information from
75-
# summary.ergm, in preparation to be passed to the function
74+
# Takes an ergm object and gathers some of the information from
75+
# summary.ergm, in preparation to be passed to the function
7676
# coef.comparison.
7777
ergm.info <- function(object) {
7878
coefs <- object$coef
@@ -83,7 +83,7 @@ ergm.info <- function(object) {
8383
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
8484
symbols = c("***", "**", "*", ".", " "), legend = F)
8585
starredcoef <- paste0(format(coefs, digits = 3), signif.stars)
86-
86+
8787
ans <- list()
8888
count <- 1
8989
for (i in terms) {
@@ -96,7 +96,7 @@ ergm.info <- function(object) {
9696
}
9797

9898

99-
# Takes in a list of multiple outputs from ergm.info,
99+
# Takes in a list of multiple outputs from ergm.info,
100100
# formatting it all into a table comparing up to 5 models.
101101
coef.comparison <- function(coeflist) {
102102
nmod <- length(coeflist)
@@ -112,7 +112,7 @@ coef.comparison <- function(coeflist) {
112112
terms <- c(terms[-which(terms == "AIC")], "AIC")
113113
terms <- c(terms[-which(terms == "BIC")], "BIC")
114114
mat <- matrix(nrow = length(terms), ncol = nmod)
115-
115+
116116
for (k in 1:nmod) {
117117
row <- 1
118118
for (l in terms) {
@@ -124,7 +124,7 @@ coef.comparison <- function(coeflist) {
124124
row <- row + 1
125125
}
126126
}
127-
127+
128128
rownames(mat) <- terms
129129
colnames(mat) <- paste0("Model", 1:nmod)
130130
return(print(mat, quote = FALSE))
@@ -136,7 +136,7 @@ stat.comparison <- function(statlist) {
136136
stop("No model summaries were passed to stat.comparison")
137137
if (nmod > 5)
138138
stop("List of length greater than 5 passed to stat.comparison")
139-
139+
140140
statvec <- c()
141141
for (j in 1:nmod) {
142142
for (k in 1:length(statlist[[j]])) {
@@ -145,6 +145,17 @@ stat.comparison <- function(statlist) {
145145
}
146146
}
147147
}
148-
148+
149149
return(statvec)
150150
}
151+
152+
hist.info <- function(x, breaks) {
153+
out <- hist(x, breaks = breaks, plot = FALSE)
154+
barname <- paste(out$breaks[1:2], collapse = "-")
155+
for(i in seq(length(out$breaks) - 2)){
156+
barname <- append(barname, paste(out$breaks[i+1]+1, out$breaks[i+2], sep = "-"))
157+
}
158+
out <- out$counts
159+
names(out) <- barname
160+
return(out)
161+
}

inst/shiny/server.R

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,25 @@ numattr <- reactive({
512512
numattr <- append(numattr,attrib()[i])
513513
}
514514
}}
515-
numattr})
515+
numattr
516+
})
517+
518+
#dataframe of nodes, their attributes, and their coordinates in nwplot
519+
nwdf <- reactive({
520+
attrs <- menuattr()
521+
if(is.na(as.numeric(network.vertex.names(nw()))[1])){
522+
df <- data.frame(Names = network.vertex.names(nw()))
523+
} else {
524+
df <- data.frame(Names = as.numeric(network.vertex.names(nw())))
525+
}
526+
for(i in seq(length(attrs))){
527+
df[[attrs[i]]] <- get.vertex.attribute(nw(), attrs[i])
528+
}
529+
df[["Missing"]] <- get.vertex.attribute(nw(), "na")
530+
df[["cx"]] <- coords()[,1]
531+
df[["cy"]] <- coords()[,2]
532+
df
533+
})
516534

517535
# betweenness centrality of all nodes (for sizing menu)
518536
nodebetw <- reactive({
@@ -1416,34 +1434,52 @@ output$nwplotdownload <- downloadHandler(
14161434
}
14171435
)
14181436

1419-
#dataframe of nodes, their attributes, and their coordinates in nwplot
1420-
nwdf <- reactive({
1421-
attrs <- menuattr()
1422-
if(is.na(as.numeric(network.vertex.names(nw()))[1])){
1423-
df <- data.frame(Names = network.vertex.names(nw()))
1424-
} else {
1425-
df <- data.frame(Names = as.numeric(network.vertex.names(nw())))
1426-
}
1427-
for(i in seq(length(attrs))){
1428-
df[[attrs[i]]] <- get.vertex.attribute(nw(), attrs[i])
1429-
}
1430-
df[["Missing"]] <- get.vertex.attribute(nw(), "na")
1431-
df[["cx"]] <- coords()[,1]
1432-
df[["cy"]] <- coords()[,2]
1433-
df
1437+
output$attrcheck <- renderUI({
1438+
checkboxGroupInput("attrcols",
1439+
label = "Include these attributes",
1440+
choices = c(menuattr(), "Missing"),
1441+
selected = c(menuattr(), "Missing"))
14341442
})
1443+
outputOptions(output, "attrcheck", suspendWhenHidden = FALSE)
14351444

14361445
output$attrtbl <- renderDataTable({
1437-
df <- nwdf()
1438-
dt <- df[, c("Names", input$attribcols)]
1446+
dt <- nwdf()[, c("Names", input$attrcols)]
14391447
dt
14401448
}, options = list(pageLength = 10))
14411449

1442-
output$attrcheck <- renderUI({
1443-
checkboxGroupInput("attribcols",
1444-
label = "Include these attributes in the table",
1445-
choices = c(menuattr(), "Missing"),
1446-
selected = c(menuattr(), "Missing"))
1450+
output$attrhist <- renderPlot({
1451+
nplots <- length(input$attrcols)
1452+
if(nplots == 0){return()}
1453+
attrname <- input$attrcols
1454+
if(nplots == 1){
1455+
par(mfrow = c(1, 1))
1456+
lvls <- length(unique(nwdf()[[attrname]]))
1457+
if(attrname %in% numattr() & lvls > 9){
1458+
tab <- hist.info(nwdf()[[attrname]], breaks = 10)
1459+
} else {
1460+
tab <- table(nwdf()[[attrname]])
1461+
}
1462+
barplot(tab, xlab = attrname, col = histblue)
1463+
} else {
1464+
r <- ceiling(nplots/2)
1465+
par(mfrow = c(r, 2))
1466+
for(a in attrname){
1467+
lvls <- length(unique(nwdf()[[a]]))
1468+
if(a %in% numattr() & lvls > 9){
1469+
tab <- hist.info(nwdf()[[a]], breaks = 10)
1470+
} else {
1471+
tab <- table(nwdf()[[a]])
1472+
}
1473+
barplot(tab, xlab = a, col = histblue)
1474+
}
1475+
}
1476+
})
1477+
1478+
output$attrhistplotspace <- renderUI({
1479+
nplots <- length(input$attrcols)
1480+
r <- ceiling(nplots/2)
1481+
h <- ifelse(r == 1, 400, r * 300)
1482+
plotOutput("attrhist", height = h)
14471483
})
14481484

14491485
#Data to use for null hypothesis overlays in network plots

inst/shiny/ui.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,12 @@ fluidRow(
501501
)
502502
),
503503
tabPanel('Attributes', br(),
504-
dataTableOutput("attrtbl")
504+
conditionalPanel('input.attrview == "table"',
505+
dataTableOutput("attrtbl")
506+
),
507+
conditionalPanel('input.attrview == "histogram"',
508+
uiOutput("attrhistplotspace"))
509+
505510
),
506511
tabPanel('Degree Distribution',
507512
p(class='helper', id='ddhelper', icon('question-circle')),
@@ -745,6 +750,9 @@ fluidRow(
745750
downloadButton('nwplotdownload',
746751
label = "Download Plot", class = "btn-sm")),
747752
conditionalPanel(condition='input.plottabs == "Attributes"',
753+
selectInput("attrview", label = "View attributes in:",
754+
choices = c("table", "histogram")),
755+
br(),
748756
uiOutput("attrcheck")
749757
),
750758
conditionalPanel(condition='input.plottabs == "Degree Distribution"',

inst/shiny/www/alert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ $(document).ready(function(){
196196
}
197197

198198
});
199-
199+
200200
$("#termdocbox").click(function(){
201201
$("#termexpand i").toggleClass("fa-angle-double-down fa-angle-double-up");
202202
if($("#termdocbox").height()<100){

0 commit comments

Comments
 (0)