forked from Statnet-Summer-Project/ndtv-shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.R
265 lines (225 loc) · 9.11 KB
/
test.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
# TODO: Add comment
#
# Author: kirk
###############################################################################
render.animation
function(net, render.par=list(tween.frames=10,show.time=TRUE,show.stats=NULL,extraPlotCmds=NULL,initial.coords=0),plot.par=list(bg='white'),ani.options=list(interval=0.1),render.cache=c('plot.list','none'), verbose=TRUE,...){
if (!is.network(net)){
stop("render.animation requires the first argument to be a network object")
}
# check render.par params
if (is.null(render.par)){
stop("render.animation is missing the 'render.par' argument (a list of rendering parameters).")
}
if (is.null(render.par$tween.frames)){
render.par$tween.frames<-10
}
if (is.null(render.par$show.time)){
render.par$show.time<-TRUE
}
if (is.null(render.par$initial.coords)){
render.par$initial.coords<-matrix(0,ncol=2,nrow=network.size(net))
}
#check if coordinates have already been computed
if (!all(c("animation.x.active","animation.y.active") %in% list.vertex.attributes(net))){
net <- compute.animation(net,verbose=verbose)
}
# temporary hard-coded param to work around plot issue in RStudio
externalDevice<-FALSE
doRStudioHack<-TRUE
if(!is.null(render.par$'do_RStudio_plot_hack')){
doRStudioHack<-render.par$'do_RStudio_plot_hack'
}
if (!is.function(options()$device)){
if (names(dev.cur())=="RStudioGD" & doRStudioHack){
message("RStudio's graphics device is not well supported by ndtv, attempting to open another type of plot window")
# try to open a new platform-appropriate plot window
if (.Platform$OS.type=='windows'){
windows()
} else if(length(grep(R.version$platform,pattern='apple'))>0) # is it mac?
{
quartz()
} else { # must be unix
x11()
}
externalDevice<-TRUE
}
}
# make sure background color is not transparent unless set that way explicitly
if (par("bg")=="transparent" & is.null(plot.par$'bg')){
plot.par$'bg'<-'white'
}
# set high-level plot attributes (bg color, margins, etc)
# and cache initial graphics par settings
origPar<-par(plot.par)
# set animation options
oopts <- ani.options(ani.options)
#figure out what the slicing parameters were
slice.par <- get.network.attribute(net,"slice.par")
if (is.null(slice.par)){
stop("render.animation can not locate the 'slice.par' list of parameters in the input network object")
}
# check plot caching params
render.cache<-match.arg(render.cache)
# cache plotting arguments
plot_params<-list(...)
# define some defaults for ploting args
# label defaults to vertex names
if(is.null(plot_params$label)){
plot_params$label<-function(slice){network.vertex.names(slice)}
}
# xlab defaults to time
if(is.null(plot_params$xlab) & render.par$show.time){
plot_params$xlab <- function(onset,terminus){ifelse(onset==terminus,paste("t=",onset,sep=''),paste("t=",onset,"-",terminus,sep=''))}
}
# but if show stats, use that instead
# TODO: deprecate show.stats in favor of passing in directly for evaluation?
if(!is.null(render.par$show.stats) && render.par$show.stats!=FALSE){
# evaluate a eqn string giving the stats formual
# TODO: this requires that tergm be loaded! give informative warning if not
if(render.par$show.time){
# include the time string in the summary
plot_params$xlab <- eval(parse(text=paste("function(slice,onset,terminus){stats<-summary.statistics.network(slice",render.par$show.stats,")\n return(paste('t=',onset,'-',terminus,' ',paste(rbind(names(stats),stats),collapse=':'),sep='')) }",sep='')))
} else {
plot_params$xlab <- eval(parse(text=paste("function(slice){stats<-summary.statistics.network(slice",render.par$show.stats,")\n return(paste(rbind(names(stats),stats),collapse=':')) }",sep='')))
}
}
#disable jitter by default because it messes things up
if(is.null(plot_params$jitter)){
plot_params$jitter<-FALSE
}
#TODO: how are we doing interpolation?
interp.fun<-coord.interp.smoothstep
#interp.fun<-coord.interp.linear
# compute lists of times that networks will be collapsed
starts <- seq(from=slice.par$start,to=slice.par$end,by=slice.par$interval)
ends <- seq(from=slice.par$start+slice.par$aggregate.dur,to=slice.par$end+slice.par$aggregate.dur,by=slice.par$interval)
#compute coordinate ranges to know how to scale plots
xmin <- aggregate.vertex.attribute.active(net,"animation.x",min)
xmax <- aggregate.vertex.attribute.active(net,"animation.x",max)
ymin <- aggregate.vertex.attribute.active(net,"animation.y",min)
ymax <- aggregate.vertex.attribute.active(net,"animation.y",max)
if (is.null(plot_params$xlim)){
# deal with case of only one coord, so no range
if(xmin==xmax){
xmax<-xmin+1
xmin<-xmin-1
}
plot_params$xlim<-c(xmin,xmax)
}
if(is.null(plot_params$ylim)){
# deal with case of only one coord, so no range
if(ymin==ymax){
ymax<-ymin+1
ymin<-ymin-1
}
plot_params$ylim<-c(ymin,ymax)
}
#set up default coords. If not specified, default will be zero
if(is.numeric(render.par$initial.coords)){
coords<-matrix(render.par$initial.coords,ncol=2,nrow=network.size(net))
}
#compute some starting coords
slice <- network.collapse(net,starts[1],ends[1],rule=slice.par$rule,rm.time.info=FALSE)
activev <- is.active(net,starts[1],ends[1],v=seq_len(network.size(net)),rule=if(slice.par$rule!='all'){'any'})
# start from the coords of the first slice
if (length(slice)>0 & network.size(slice)>0){
coords[activev,1] <-get.vertex.attribute(slice,"animation.x")
coords[activev,2] <-get.vertex.attribute(slice,"animation.y")
#need to update plot params with slice-specific values
# evald_params<-.evaluate_plot_params(plot_params=plot_params,net=net,slice=slice,s=1,onset=starts[1],terminus=ends[1])
#
#
# # set up arguments
# plot_args<-list(x=slice,coord=coords[activev,,drop=FALSE])
# plot_args<-c(plot_args,evald_params)
# # cll the plotting function with appropriate args
# do.call(plot.network, plot_args)
#
# # check if user has passed in extra plotting commands that need to be rendered
# if (!is.null(render.par$extraPlotCmds)){
# eval(render.par$extraPlotCmds)
# }
}# end slice > 0 block
coords2 <- coords
if (render.cache=='plot.list'){
ani.record(reset=TRUE)
}
#move through frames to render them out
for(s in 1:length(starts)){
if (verbose){print(paste("rendering",render.par$tween.frames,"frames for slice",s-1))}
slice <- network.collapse(net,starts[s],ends[s],rule=slice.par$rule,rm.time.info=FALSE)
activev <- is.active(net,starts[s],ends[s],v=seq_len(network.size(net)),rule=if(slice.par$rule!='all'){'any'})
#TODO: draw new slices for intermediate tween frames?
#skip any empty networks
if (length(slice)>0 & network.size(slice)>0){
#need to update plot params with slice-specific values
evald_params<-.evaluate_plot_params(plot_params=plot_params,net=net,slice=slice,s=s,onset=starts[s],terminus=ends[s])
for(t in 1:render.par$tween.frames){
#coords2[activev,1]<-get.vertex.attribute.active(slice,"animation.x",onset=starts[s],terminus=ends[s])
#coords2[activev,2]<-get.vertex.attribute.active(slice,"animation.y",onset=starts[s],terminus=ends[s])
coords2[activev,1]<-get.vertex.attribute(slice,"animation.x")
coords2[activev,2]<-get.vertex.attribute(slice,"animation.y")
# tweenCoords <- coords + ((coords2-coords)*(t/render.par$tween.frames))
tweenCoords <- interp.fun(coords,coords2,t,render.par$tween.frames)
#TODO:what if we want to include innactive nodes
# set up arguments
plot_args<-list(x=slice,coord=tweenCoords[activev,,drop=FALSE])
plot_args<-c(plot_args,evald_params)
# cll the plotting function with appropriate args
do.call(plot.network, plot_args)
# check if user has passed in extra plotting commands that need to be rendered
if (!is.null(render.par$extraPlotCmds)){
eval(render.par$extraPlotCmds)
}
if (render.cache=='plot.list'){
ani.record()
}
}
coords<-coords2;
} else { # end slice > 0 block
# empty network causes plot problems
# draw some blank frames while time passes
evald_params<-.evaluate_plot_params(plot_params=plot_params,net=net,slice=slice,s=s,onset=starts[s],terminus=ends[s])
if(render.par$show.time){
xlab<-evald_params$xlab
} else {
xlab<-NULL
}
singlenet <-network.initialize(1)
for(t in 1:render.par$tween.frames){
plot.network(singlenet,
vertex.cex=0,xlab=xlab)
# check if user has passed in extra plotting commands that need to be rendered
if (!is.null(render.par$extraPlotCmds)){
eval(render.par$extraPlotCmds)
}
if (render.cache=='plot.list'){
ani.record()
}
}
} # end empty network block
}
# reset the graphics params back
par(origPar)
# turn off external device if using one
if (externalDevice){
dev.off()
}
}
function(reset = FALSE, replay.cur = FALSE) {
if (reset) .ani.env$.images = list() else {
## make sure a graphics device has been opened
if (dev.cur() != 1) {
n = length(.ani.env$.images)
.ani.env$.images[[n + 1]] = recordPlot()
} else warning('no current device to record from')
}
if (replay.cur) {
tmp = recordPlot()
dev.hold()
replayPlot(tmp)
dev.flush()
}
invisible(NULL)
}