-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
demo.R
208 lines (183 loc) · 6.71 KB
/
demo.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
#' Run igraph demos, step by step
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `igraphdemo()` was renamed to `igraph_demo()` to create a more
#' consistent API.
#' @inheritParams igraph_demo
#' @keywords internal
#' @export
igraphdemo <- function(which) { # nocov start
lifecycle::deprecate_soft("2.0.0", "igraphdemo()", "igraph_demo()")
igraph_demo(which = which)
} # nocov end
# IGraph R package
# Copyright (C) 2005-2012 Gabor Csardi <csardi.gabor@gmail.com>
# 334 Harvard street, Cambridge, MA 02139 USA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#
###################################################################
#' Run igraph demos, step by step
#'
#' Run one of the accompanying igraph demos, somewhat interactively, using a Tk
#' window.
#'
#' This function provides a somewhat nicer interface to igraph demos that come
#' with the package, than the standard [demo()] function. igraph
#' demos are divided into chunks and `igraph_demo()` runs them chunk by
#' chunk, with the possibility of inspecting the workspace between two chunks.
#'
#' The `tcltk` package is needed for `igraph_demo()`.
#'
#' @param which If not given, then the names of the available demos are listed.
#' Otherwise it should be either a filename or the name of an igraph demo.
#' @return Returns `NULL`, invisibly.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @seealso [demo()]
#' @family demo
#' @export
#' @keywords graphs
#' @examples
#'
#' igraph_demo()
#' if (interactive() && requireNamespace("tcltk", quietly = TRUE)) {
#' igraph_demo("centrality")
#' }
#'
igraph_demo <- function(which) {
if (missing(which)) {
demodir <- system.file("demo", package = "igraph")
if (demodir == "") {
stop("Could not find igraph demos, broken igraph installation?")
}
return(sub("\\.R$", "", list.files(demodir)))
}
if (!grepl("\\.R$", which)) {
which <- paste(which, sep = ".", "R")
}
if (!file.exists(which) && !grepl("^/", which)) {
which <- system.file(paste("demo", sep = "/", which), package = "igraph")
}
if (which == "" || !file.exists(which)) {
stop("Could not find demo file")
}
.igraphdemo.next <- function(top, txt) {
act <- as.character(tcltk::tktag.nextrange(txt, "active", "0.0"))
if (length(act) == 0) {
return()
}
options(keep.source = TRUE)
text <- tcltk::tclvalue(tcltk::tkget(txt, act[1], act[2]))
cat("=======================================================\n")
expr <- parse(text = text)
for (i in seq_along(expr)) {
co <- as.character(attributes(expr)$srcref[[i]])
co[1] <- paste("> ", sep = "", co[1])
if (length(co) > 1) {
co[-1] <- paste(" +", sep = "", co[-1])
}
cat(co, sep = "\n")
res <- withVisible(eval(expr[[i]], envir = .GlobalEnv))
if (res$visible) {
print(res$value)
}
}
cat("> -------------------------------------------------------\n")
cat(options()$prompt)
tcltk::tktag.remove(txt, "activechunk", act[1], act[2])
tcltk::tktag.remove(txt, "active", act[1], act[2])
nex <- as.character(tcltk::tktag.nextrange(txt, "activechunk", act[1]))
if (length(nex) != 0) {
tcltk::tktag.add(txt, "active", nex[1], nex[2])
tcltk::tksee(txt, paste(sep = "", as.numeric(nex[2]), ".0"))
tcltk::tksee(txt, paste(sep = "", as.numeric(nex[1]), ".0"))
}
}
.igraphdemo.close <- function(top) {
tcltk::tkdestroy(top)
}
.igraphdemo.reset <- function(top, txt, which) {
demolines <- readLines(which)
demolines <- demolines[!grepl("^pause\\(\\)$", demolines)]
demolines <- paste(" ", sep = "", demolines)
ch <- grep("^[ ]*###", demolines)
ch <- c(ch, length(demolines) + 1)
if (length(ch) == 1) {
cli::cli_warn("Demo source file does not contain chunks.")
} else {
demolines <- demolines[ch[1]:length(demolines)]
ch <- grep("^[ ]*###", demolines)
ch <- c(ch, length(demolines) + 1)
}
tcltk::tkconfigure(txt, state = "normal")
tcltk::tkdelete(txt, "0.0", "end")
tcltk::tkinsert(txt, "insert", paste(demolines, collapse = "\n"))
tcltk::tkconfigure(txt, state = "disabled")
for (i in seq_along(ch[-1])) {
from <- paste(sep = "", ch[i], ".0")
to <- paste(sep = "", ch[i + 1] - 1, ".0")
tcltk::tktag.add(txt, "chunk", from, to)
tcltk::tktag.add(txt, "activechunk", from, to)
}
tcltk::tktag.configure(txt, "chunk", "-borderwidth", "1")
tcltk::tktag.configure(txt, "chunk", "-relief", "sunken")
if (length(ch) >= 2) {
tcltk::tktag.add(
txt, "active", paste(sep = "", ch[1], ".0"),
paste(sep = "", ch[2] - 1, ".0")
)
tcltk::tktag.configure(txt, "active", "-foreground", "red")
tcltk::tktag.configure(txt, "active", "-background", "lightgrey")
}
comm <- grep("^#", demolines)
for (i in comm) {
tcltk::tktag.add(
txt, "comment", paste(sep = "", i, ".0"),
paste(sep = "", i, ".end")
)
}
tcltk::tktag.configure(txt, "comment", "-font", "bold")
tcltk::tktag.configure(txt, "comment", "-foreground", "darkolivegreen")
}
top <- tcltk::tktoplevel(background = "lightgrey")
tcltk::tktitle(top) <- paste("igraph demo:", which)
main.menu <- tcltk::tkmenu(top)
tcltk::tkadd(main.menu, "command", label = "Close", command = function() {
.igraphdemo.close(top)
})
tcltk::tkadd(main.menu, "command", label = "Reset", command = function() {
.igraphdemo.reset(top, txt, which)
})
tcltk::tkconfigure(top, "-menu", main.menu)
scr <- tcltk::tkscrollbar(top,
repeatinterval = 5,
command = function(...) tcltk::tkyview(txt, ...)
)
txt <- tcltk::tktext(top,
yscrollcommand = function(...) tcltk::tkset(scr, ...),
width = 80, height = 40
)
but <- tcltk::tkbutton(top, text = "Next", command = function() {
.igraphdemo.next(top, txt)
})
tcltk::tkpack(but, side = "bottom", fill = "x", expand = 0)
tcltk::tkpack(scr, side = "right", fill = "y", expand = 0)
tcltk::tkpack(txt, side = "left", fill = "both", expand = 1)
.igraphdemo.reset(top, txt, which)
invisible()
}