-
Notifications
You must be signed in to change notification settings - Fork 5
/
Netreader.R
356 lines (339 loc) · 14.9 KB
/
Netreader.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#' Netreader
#'
#' \code{Netreader} is an RStudio-Addin that allows you to read network files. So far, only plaintext files are supported (e.g. csv,tsv,...).
#'
#' @details To run the addin, select \code{Netreader} from the Addins-menu within RStudio.
#' @return \code{Netreader} returns the created network as igraph object.
#' @import miniUI
#' @import shiny
#' @import rstudioapi
#' @importFrom igraph graph_from_adjacency_matrix graph_from_data_frame vcount vertex_attr_names set_vertex_attr get.vertex.attribute
#' @name Netreader
NULL
Netreader <- function() {
rv <- reactiveValues(g = NULL, pathN = NULL, pathA = NULL, code = NULL)
# ui ----
ui <- miniPage(
tags$head(
tags$style(HTML("hr {border-top: 1px solid #000000;}"))
),
tags$script(jscodeWidth),
tags$script(jscodeHeight),
tags$style(type = "text/css", ".selectize-dropdown{ width: 200px !important; }"),
tags$style(type = "text/css", ".form-group.shiny-input-container{width:50%;}"),
tags$style(type = "text/css", "#preview {background-color: rgba(0,0,0,0.50); color: white;}"),
tags$style(type = "text/css", "#previewA {background-color: rgba(0,0,0,0.50); color: white;}"),
tags$style(type = "text/css", "#netpreview {background-color: rgba(0,0,0,0.50); color: white;}"),
tags$style(type = "text/css", "#netpreviewA {background-color: rgba(0,0,0,0.50); color: white;}"),
tags$style(type = "text/css", "#codereview {background-color: rgba(0,0,0,1); color: white;}"),
tags$style(type = "text/css", "#readit {background-color: rgba(30,144,255,1); color: white}"),
tags$style(type = "text/css", "#readitA {background-color: rgba(30,144,255,1); color: white}"),
tags$style(type = "text/css", "#netfile {background-color: rgba(30,144,255,1); color: white}"),
tags$style(type = "text/css", "#attrfile {background-color: rgba(30,144,255,1); color: white}"),
gadgetTitleBar("Netreader"),
miniTabstripPanel(
selected = "Import Network",
miniTabPanel("Import Network",
icon = icon("bezier-curve"),
fillRow(
height = "30px", width = "100%",
strong("Choose network file")
),
fillRow(
height = "50px", width = "50%",
# fileInput("netfile", "Choose network file")
actionButton("netfile", "Browse...")
),
fillRow(
height = "50px", width = "75%",
verbatimTextOutput("netfilePath")
),
fillRow(
height = line.height, width = "100%",
h4("File Preview (first 5 lines)")
),
fillRow(
height = "120px", width = "100%",
verbatimTextOutput("preview")
),
fillRow(
height = line.height, width = "50%",
checkboxInput("colnames", "Header", value = FALSE),
checkboxInput("rownames", "Rownames", value = FALSE),
checkboxInput("quotes", "Quotes", value = FALSE),
checkboxInput("directed", "Directed", value = FALSE)
),
fillRow(
height = line.height, width = "50%",
radioButtons("readfct", "network format", choices = c("edgelist", "adjacency matrix")),
radioButtons("valsep", "file delimiter", choices = c("comma" = ",", "space" = " ", "tab" = "\t"))
),
hr(),
fillRow(
height = line.height, width = "100%",
actionButton("readit", "Import Network"),
textAreaInput("text", label = "", value = "", placeholder = "enter name", height = "35px")
),
fillRow(
height = "120px", width = "100%",
verbatimTextOutput("netpreview")
)
),
# attributes ----
miniTabPanel("Add Attributes",
icon = icon("list-ol"),
fillRow(
height = "30px", width = "100%",
strong("Choose attribute file")
),
fillRow(
height = "50px", width = "100%",
# fileInput("attrfile", "Choose attribute file")
actionButton("attrfile", "Browse...")
),
fillRow(
height = "50px", width = "75%",
verbatimTextOutput("attrfilePath")
),
fillRow(
height = line.height, width = "100%",
h4("File Preview (first 5 lines)")
),
fillRow(
height = "120px", width = "100%",
verbatimTextOutput("previewA")
),
fillRow(
height = line.height, width = "50%",
checkboxInput("colnamesA", "Header", value = TRUE),
checkboxInput("quotesA", "Quotes", value = FALSE),
radioButtons("valsepA", "file delimiter", choices = c("comma" = ",", "space" = " ", "tab" = "\t"))
),
tags$hr(),
fillRow(
height = line.height, width = "100%",
actionButton("readitA", "Import Attributes")
),
fillRow(
height = "120px", width = "100%",
verbatimTextOutput("netpreviewA")
)
),
# show code ----
miniTabPanel("Review Code",
icon = icon("code"),
verbatimTextOutput("codereview")
)
)
)
# server ----
server <- function(input, output, session) {
# choose netfile path ----
observeEvent(input$netfile, {
rv$pathN <- file.choose()
})
# choose attrfile path ----
observeEvent(input$attrfile, {
rv$pathA <- file.choose()
})
output$netfilePath <- renderPrint(
if (!is.null(rv$pathN)) {
cat(rv$pathN)
} else {
cat("no file selected")
}
)
output$attrfilePath <- renderPrint(
if (!is.null(rv$pathA)) {
cat(rv$pathA)
} else {
cat("no file selected")
}
)
# file preview ----
output$preview <- renderText({
# inFile <- input$netfile
inFile <- rv$pathN
if (is.null(inFile)) {
return(NULL)
}
txt <- readLines(inFile, n = 5)
txt <- paste(txt, collapse = "\n")
txt
})
# attribute preview ----
output$previewA <- renderText({
# inFile <- input$attrfile
inFile <- rv$pathA
if (is.null(inFile)) {
return(NULL)
}
txt <- readLines(inFile, n = 5)
txt <- paste(txt, collapse = "\n")
txt
})
# network preview ----
output$netpreview <- renderPrint({
g <- rv$g
if (is.null(g)) {
return(cat("no network created yet."))
}
summary(g)
})
# network2 preview ----
output$netpreviewA <- renderPrint({
g <- rv$g
if (is.null(g)) {
return(cat("no network created yet."))
}
summary(g)
})
# codeoutput ----
output$codereview <- renderPrint({
cat(rv$code)
})
# read network ----
observeEvent(input$readit, {
inFile <- rv$pathN
q <- ifelse(input$quotes, "\"", "")
if (input$rownames) {
A <- tryCatch(
utils::read.table(inFile,
header = input$colnames,
row.names = 1,
sep = input$valsep, quote = q,
stringsAsFactors = FALSE
),
error = function(e) NULL
)
head <- "library(igraph)\n\n# load raw network data ----\n"
cmd <- paste0(
"A <- utils::read.table(file = '", inFile, "'",
",\n header = ", input$colnames, ", row.names = 1",
", sep = '", input$valsep, "'", ", quote = '", q, "', stringsAsFactors = FALSE)\n"
)
rv$code <- paste(head, cmd)
} else {
A <- tryCatch(
utils::read.table(inFile,
header = input$colnames,
sep = input$valsep, quote = q,
stringsAsFactors = FALSE
),
error = function(e) NULL
)
head <- "library(igraph)\n# load raw network data ----\n"
cmd <- paste0(
"A <- utils::read.table(file = '", inFile, "'",
",\n header = ", input$colnames, ", sep = '", input$valsep, "'",
", quote = '", q, "', stringsAsFactors = FALSE)\n"
)
rv$code <- paste0(head, cmd)
}
if (is.null(A)) {
showNotification("something went wrong reading the file. Check your settings", type = "error", duration = 2)
} else {
if (input$readfct == "adjacency matrix") {
mode <- ifelse(input$directed, "directed", "undirected")
g <- tryCatch(graph_from_adjacency_matrix(as.matrix(A), mode = mode, weighted = "weight"), error = function(e) NULL)
if (is.null(g)) {
showNotification("something went wrong creating the network.", type = "error", duration = 2)
} else {
head <- "# create network ----\n"
cmd <- paste0("g <- graph_from_adjacency_matrix(as.matrix(A),mode = '", mode, "')\n")
rv$code <- paste0(rv$code, head, cmd)
rv$g <- g
showNotification("network data successfully imported", type = "message", duration = 2)
}
} else if (input$readfct == "edgelist") {
mode <- ifelse(input$directed, T, F)
g <- tryCatch(graph_from_data_frame(A, directed = mode), error = function(e) NULL)
if (is.null(g)) {
showNotification("something went wrong creating the network.", type = "error", duration = 2)
} else {
head <- "# create network ----\n"
cmd <- paste0("g <- graph_from_data_frame(A,directed = ", mode, ")\n")
rv$code <- paste0(rv$code, "\n", head, cmd)
rv$g <- g
showNotification("network data successfully imported", type = "message", duration = 2)
}
}
}
})
# read attributes ----
observeEvent(input$readitA, {
if (is.null(rv$g)) {
showNotification("please import a network first", type = "error", duration = 2)
} else {
# inFile <- input$attrfile
inFile <- rv$pathA
q <- ifelse(input$quotesA, "\"", "")
A <- tryCatch(
utils::read.table(inFile,
header = input$colnamesA,
sep = input$valsepA, quote = q,
stringsAsFactors = FALSE
),
error = function(e) NULL
)
head <- "# load raw attribute data ----\n"
cmd <- paste0(
"attrs <- utils::read.table(file = '", inFile, "'",
",\n header = ", input$colnamesA, ", sep = '", input$valsepA, "'",
", quote = '", q, "', stringsAsFactors = FALSE)\n"
)
rv$code <- paste0(rv$code, "\n", head, cmd)
if (is.null(A)) {
showNotification("something went wrong reading the file. Check your settings", type = "error", duration = 2)
} else {
if (nrow(A) != vcount(rv$g)) {
showNotification("The number of rows does not match the number of nodes in the network", type = "error", duration = 2)
} else {
if ("name" %in% vertex_attr_names(rv$g)) {
vnames <- get.vertex.attribute(rv$g, "name")
identCol <- which(apply(A, 2, function(x) all(x %in% vnames)))[1]
anames <- A[, identCol]
A <- A[, -identCol, drop = FALSE]
perm <- match(vnames, anames)
for (attr in names(A)) {
rv$g <- set_vertex_attr(rv$g, name = attr, value = A[[attr]][perm])
}
head <- "# add attributes to network ----\n"
cmd <- AttrNameImport
rv$code <- paste0(rv$code, "\n", head, cmd)
} else {
showNotification("network does not have a name attribute.\nmatching by row number instead", type = "warning", duration = 2)
for (attr in names(A)) {
rv$g <- set_vertex_attr(rv$g, name = attr, value = A[[attr]])
}
head <- "# add attributes to network ----\n"
cmd <- AttrRowImport
rv$code <- paste0(rv$code, "\n", head, cmd)
}
showNotification("Attributes successfully imported", type = "message", duration = 2)
}
}
}
})
# cancel ----
observeEvent(input$cancel, {
invisible(stopApp())
})
# done ----
observeEvent(input$done, {
if (input$text == "") {
showNotification("Please enter a variable name", type = "warning", duration = 2)
} else {
eval(parse(text = paste0("assign(\"", input$text, "\",rv$g", ",envir = .GlobalEnv)")))
invisible(stopApp())
}
})
}
viewer <- dialogViewer(dialogName = "Netreader", width = 990, height = 900)
runGadget(ui, server, stopOnCancel = FALSE, viewer = viewer)
}
#' @export
#' @rdname Netreader
NetreaderAddin <- function() {
Netreader()
}