-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.R
341 lines (278 loc) · 11.6 KB
/
server.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
##############################
# Shiny App: SLIDER - Software for LongItudinal Data Exploration with R
# Server
##############################
shinyServer(function(input, output, session) {
# load data ----
baseData <- reactiveValues(df = NULL, dataSource = NULL)
data(mvad)
observeEvent(input$loadTestData,{
baseData$dataSource <- "exampleData"
})
observe({
req(input$fileInput)
baseData$dataSource <- input$fileInput$datapath
})
observe({
req(baseData$dataSource)
if (baseData$dataSource == "exampleData"){
dataTest <- mvad[ , c(1, 15:86)]
dataTest <- dataTest[ , c(1, seq(4, 66, 12))]
dataTest <- colwise(as.character)(dataTest[ , 2:7])
columnList <- colnames(dataTest)
UpdateExample(session = session, mycolumns = columnList)
baseData$df <- dataTest
} else {
userData <- read.csv(file = input$fileInput$datapath,
sep = input$sepcol,
quote = input$quote,
dec = input$sepdec,
encoding = input$encodtab,
stringsAsFactor = FALSE,
check.names = FALSE)
columnList <- c(colnames(userData))
UpdateOwn(session = session, mycolumns = columnList)
baseData$df<- userData
}
})
UpdateExample <- function(session, mycolumns){
updateSelectInput(session = session,
inputId = "timecol",
choices = mycolumns,
selected = paste0("Sep.", 93:98))
updateSelectInput(session = session,
inputId = "weightcol",
choices = c("", mycolumns))
updateSelectInput(session = session,
inputId = "factcol1",
choices = c("", mycolumns))
updateSelectInput(session = session,
inputId = "factcol2",
choices = c("", mycolumns))
}
UpdateOwn <- function(session, mycolumns){
updateSelectInput(session = session,
inputId = "timecol",
choices = mycolumns)
updateSelectInput(session = session,
inputId = "weightcol",
choices = c("", mycolumns))
updateSelectInput(session = session,
inputId = "factcol1",
choices = c("", mycolumns))
updateSelectInput(session = session,
inputId = "factcol2",
choices = c("", mycolumns))
}
observe({
req(selecData())
if(length(input$timecol) > 1){
maxFreq <- RoundAccur(val = max(table(c(unlist(selecData()$TBL)))) / (ncol(selecData()$TBL) + 1), acc = 10)
updateSliderInput(session = session,
inputId = "sliderthreshold",
value = 10,
min = 0,
max = maxFreq,
step = 1)
} else {
return()
}
})
# select data by factors ----
selecData <- reactive({
req(baseData$df, input$timecol)
if(length(input$timecol) > 1 && (input$timecol %in% colnames(baseData$df))){
# filter rows and select columns
if (!is.null(input$factmod1) & is.null(input$factmod2)) {
dataSel <- baseData$df[baseData$df[, input$factcol1] %in% input$factmod1, ]
} else if (is.null(input$factmod1) & !is.null(input$factmod2)){
dataSel <- baseData$df[baseData$df[, input$factcol2] %in% input$factmod2, ]
} else if (!is.null(input$factmod1) & !is.null(input$factmod2)){
dataSel <- baseData$df[((baseData$df[, input$factcol1] %in% input$factmod1) & (baseData$df[, input$factcol2] %in% input$factmod2)), ]
} else {
dataSel <- baseData$df
}
timeSteps <- dataSel[, input$timecol]
# select weights
if(input$weightcol == ""){
wgtVec <- rep(1, times = nrow(dataSel))
} else {
wgtVec <- as.vector(dataSel[, input$weightcol])
}
# convert into factor
if(is.character(timeSteps[ , 1]) == TRUE){
uniqueVal <- sort(unique(unlist(timeSteps)))
timeSteps <- colwise(factor, levels = uniqueVal, labels = uniqueVal)(timeSteps)
} else {
uniqueVal <- sort(unique(unlist(timeSteps)))
timeSteps <- colwise(factor, levels = uniqueVal, labels = paste("CAT_", uniqueVal, sep = ""))(timeSteps)
}
return(list(TBL = timeSteps, WGT = wgtVec))
} else {
return()
}
})
# create table of flows for the slide plot
createFlowsTab <- reactive({
req(baseData$df, input$timecol)
df <- selecData()
getFlows <- GetCrossFlows(df = df$TBL, wgtvar = df$WGT)
return(getFlows)
})
# create state sequence object (TraMineR)
createSts <- reactive({
req(baseData$df, input$timecol)
df <- selecData()
stsobj <- CreateSeq(df$TBL, wgt = df$WGT)
})
# outputs ----
# dynamic sliders
output$selectmod1 <- renderUI({
req(input$factcol1)
uniqueModalities1 <- sort(as.character(unique(baseData$df[,input$factcol1])))
modalitiesList1 <- uniqueModalities1
selectInput(inputId = "factmod1", label = "Choose 1st group", choices = modalitiesList1, multiple = TRUE)
})
output$selectmod2 <- renderUI({
req(input$factcol2)
uniqueModalities2 <- sort(as.character(unique(baseData$df[,input$factcol2])))
modalitiesList2 <- uniqueModalities2
selectInput(inputId = "factmod2", label = "Choose 2nd group", choices = modalitiesList2, multiple = TRUE)
})
# data summary panel
output$datasummary <- renderText({
req(baseData$df, input$timecol)
if(length(input$timecol) > 1){
if(isTRUE(any(is.na(unlist(selecData()$TBL))))){
natext <- "There are missing values in the dataset"
} else {
natext <- "There is no missing value in the dataset"
}
ndim <- dim(selecData()$TBL)
obstext <- paste(ndim[1], "observations", sep = " ")
vartext <- paste(ndim[2], "variables", sep = " ")
summarytext <- paste(obstext, "\n",
vartext, "\n",
natext,
sep = "")
return(summarytext)
} else {return()}
})
output$contents <- renderDataTable({
req(baseData$df, input$timecol)
return(selecData()$TBL)
})
# slide plot panel
output$slidetext <- renderText({
req(baseData$df, input$timecol)
slideplottext <- "The slide plot draws aggregated trajectories of individuals. \nThe thickness of a segment is proportional to the frequency of transition from one state to another."
})
output$slideplot <- renderPlot({
req(baseData$df, input$timecol)
print(SlidePlot(listFlows = createFlowsTab(), threshold = input$sliderthreshold, mask = input$mask, showfreq = input$showfreq, thickmin = input$thickmin))
})
# parallel coordinates plot panel
output$pctext <- renderText({
req(baseData$df, input$timecol)
parcoordtext <- "The parallel coordinates plot for sequence data shows partially aggregated trajectories of individuals."
})
output$pcplot <- renderPlot({
req(baseData$df, input$timecol)
seqpcplot(createSts(),
ltype = "non-embeddable",
cex = input$pccex,
lwd = input$pclwd,
grid.scale = input$pcgrid,
embedding = "most-frequent",
lorder = "foreground",
lcourse = "upwards",
order.align = "time")
})
# transition rate panel
output$transratetext <- renderText({
req(baseData$df, input$timecol)
tratetext <- "Transition rates are the frequency of transition from one state to another, as observed in the dataset. \nIt may be read as an origin-detination matrix, with absolute or relative frequency."
})
output$transrate <- renderTable(rownames = TRUE, expr = {
req(baseData$df, input$timecol)
wgtBool <- is.null(input$weightcol)
resTrans <- CreateTransRate(stsobj = createSts(), wgtbool = wgtBool, df = selecData()$TBL, wgtvec = selecData()$WGT)
if(input$transparameter == "absfreq"){
return(as.data.frame(resTrans$ABSFREQ))
}
if(input$transparameter == "rowpct"){
return(as.data.frame(round(resTrans$ROWPCT, digits = 2)))
}
if(input$transparameter == "colpct"){
return(as.data.frame(round(resTrans$COLPCT, digits = 2)))
}
})
# index plot panel
output$indextext <- renderText({
req(baseData$df, input$timecol)
indexplottext <- "The sequence index plot draws a set of individual sequences. \nEach distinct state is filled with a distinct colour."
})
output$seqindex <- renderPlot({
req(baseData$df, input$timecol)
seqiplot(createSts(), border = input$borderiplot, tlim = seq(input$sliderseqi[1], input$sliderseqi[2], 1), withlegend = "right")
})
# frequency plot panel
output$freqtext <- renderText({
req(baseData$df, input$timecol)
freqplottext <- "The sequence frequency plot draws the ten most frequent sequences. \nThe vertical axis indicates the cumulative percentage of these ten sequences."
})
output$seqfreq <- renderPlot({
req(baseData$df, input$timecol)
seqfplot(createSts(), border = input$borderfplot, withlegend = "right")
})
# distribution plot panel
output$distrtext <- renderText({
req(baseData$df, input$timecol)
distrplottext <- "The sequence distribution plot draws the whole dataset with transversal aggregation . \nIt shows the distribution of distinct states at each time step."
})
output$seqdistr <- renderPlot({
req(baseData$df, input$timecol)
seqdplot(createSts(), border = input$borderdplot, withlegend = "right")
})
# Downloads ----
output$downloadsp <- downloadHandler(
filename = "SlidePlot.svg",
content = function(file) {
svg(file, width = input$widthslide / 2.54, height = input$heightslide / 2.54, pointsize = 8)
req(baseData$df, input$timecol)
print(SlidePlot(listFlows = createFlowsTab(), threshold = input$sliderthreshold, mask = input$mask, showfreq = input$showfreq, thickmin = input$thickmin))
dev.off()
})
output$downloadpc <- downloadHandler(
filename = "ParallelCoordPlot.svg",
content = function(file) {
svg(file, width = input$widthseqpc / 2.54, height = input$heightseqpc / 2.54, pointsize = 8)
req(baseData$df, input$timecol)
seqpcplot(createSts(), ltype="non-embeddable", cex=input$pccex, lwd=input$pclwd, grid.scale=input$pcgrid, embedding="most-frequent", lorder="foreground", lcourse="upwards", order.align="time")
dev.off()
})
output$downloadip <- downloadHandler(
filename = "IndexPlot.svg",
content = function(file) {
svg(file, width = input$widthseqi / 2.54, height = input$heightseqi / 2.54, pointsize = 8)
req(baseData$df, input$timecol)
seqiplot(createSts(), border = input$borderiplot, tlim = seq(input$sliderseqi[1], input$sliderseqi[2], 1))
dev.off()
})
output$downloadfp <- downloadHandler(
filename = "FrequencyPlot.svg",
content = function(file) {
svg(file, width = input$widthseqf / 2.54, height = input$heightseqf / 2.54, pointsize = 8)
req(baseData$df, input$timecol)
seqfplot(createSts(), border = input$borderfplot)
dev.off()
})
output$downloaddp <- downloadHandler(
filename = "DistributionPlot.svg",
content = function(file) {
svg(file, width = input$widthseqd / 2.54, height = input$heightseqd / 2.54, pointsize = 8)
req(baseData$df, input$timecol)
seqdplot(createSts(), border = input$borderdplot)
dev.off()
})
})