-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
323 lines (277 loc) · 12.3 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
library(shiny)
# Define server logic required to draw a histogram
function(input, output, session) {
exampleData1 <- read.csv("www/example/survival_example1.csv")
exampleData2 <- read.csv("www/example/survival_example2.csv")
my_modal <- function (timeColumn, statusColumn, dependentVariableColumn) {modalDialog(
title = "File attributes",
HTML("<strong>", timeColumn, "</strong>", ": Represents the time column <br>",
"<strong>", statusColumn, "</strong>", ": Represents the status column <br>",
"<strong>", dependentVariableColumn, "</strong>", ": Represnts the dependent variable column"),
easyClose = TRUE,
footer = NULL
)
}
output$downloadExampleFile1 <- downloadHandler(
filename = function() {
"example_file_1.txt" # Name of the file when downloaded
},
content = function(file) {
showModal(my_modal("time", "status", "sex"))
# Specify the path to the file in the www folder
file.copy("www/example/survival_example1.csv", file)
}
)
output$downloadExampleFile2 <- downloadHandler(
filename = function() {
"example_file_2.txt" # Name of the file when downloaded
},
content = function(file) {
showModal(my_modal("overall_survival", "vital_status", "Tumor_stage"))
# Specify the path to the file in the www folder
file.copy("www/example/survival_example2.csv", file)
}
)
# observe({
# if (input$tabset == "Help") {
# hide("sidebar") # Hide the sidebar when "Help" is selected
# runjs('$(".col-sm-8").removeClass("col-sm-8").addClass("col-sm-12");') # Expand the main panel
# } else {
# show("sidebar") # Show the sidebar for other tabs
# runjs('$(".col-sm-12").removeClass("col-sm-12").addClass("col-sm-8");') # Reset the main panel width
# }
# })
autoChoices <- function(data){
timeChoices <- colnames(data)[sapply(data, function(col) is.numeric(col) )]
statusChoices <- colnames(data)[sapply(data, function(col) length(unique(col)) == 2)]
dependentVariableChoices <- colnames(data)[sapply(data, function(col) length(unique(col)) == 2)]
updateSelectInput(session, "selectedTimeColumn", choices = timeChoices)
updateSelectInput(session, "selectedStatusColumn", choices = statusChoices)
updateSelectInput(session, "selectedDependentVariableColumn", choices = dependentVariableChoices)
updateRadioButtons(session, "selectedDependentVariableType", selected = "bool")
if(input$selectedDataMode == "ex1"){
updateSelectInput(session, "selectedTimeColumn", selected = "time")
updateSelectInput(session, "selectedStatusColumn", selected = "status")
updateSelectInput(session, "selectedDependentVariableColumn", selected = "sex")
}
else if(input$selectedDataMode == "ex2"){
updateSelectInput(session, "selectedTimeColumn", selected = "overall_survival")
updateSelectInput(session, "selectedStatusColumn", selected = "vital_status")
}
}
# Reactive to determine the selected dataset
dataset <- reactive({
if (input$selectedDataMode == "ex1") {
autoChoices(exampleData1)
return(exampleData1)
} else if (input$selectedDataMode == "ex2") {
autoChoices(exampleData2)
return(exampleData2)
} else if (input$selectedDataMode == "ex3") {
req(input_file())
data <- input_file()
autoChoices(data)
return(data)
}
})
# Rendering the dataTable
output$dataTable <- DT::renderDataTable({
DT::datatable(
data = dataset(),
options = list(pageLength = 20, autoWidth = FALSE)
)
})
input_file <- reactive({
if (is.null(input$selectedFile)) {
return(NULL)
}
# actually read the file
data <- read.csv(file = input$selectedFile$datapath, sep = input$selectedSeparator)
colnames(data) <- gsub("\\.", "_", colnames(data))
data
})
# Toggle
observe({
toggleState(id = "crGridSize", condition = input$crGridBoolean == "TRUE")
toggleState(id = "crGridColor", condition = input$crGridBoolean == "TRUE")
toggleState(id = "crPvaluePosition", condition = input$crPValueBoolean == "TRUE")
toggleState(id = "crPvalueSize", condition = input$crPValueBoolean == "TRUE")
toggleState(id = "crPvalueColor", condition = input$crPValueBoolean == "TRUE")
toggleState(id = "crTruncateXCoordinate", condition = input$crTruncateXBoolean == "TRUE")
})
# Observe Time Column
observeEvent(input$selectedTimeColumn,{
req(max(dataset()[[input$selectedTimeColumn]], na.rm = T))
TimeMax <- max(dataset()[[input$selectedTimeColumn]], na.rm = T)
TimeMin <- min(dataset()[[input$selectedTimeColumn]], na.rm = T)
XAxisStepSize <- 10
if(TimeMax >= 1000){
XAxisStepSize = ceiling(TimeMax/(5*100))*100
}
else if(TimeMax >= 100){
XAxisStepSize = ceiling(TimeMax/(5*10))*10
}
else if(TimeMax >= 1){
XAxisStepSize = ceiling(TimeMax/5)
}
else{
XAxisStepSize = TimeMax/5
}
updateNumericInput(session, "crXAxisStepSize", min = TimeMin, max = TimeMax, value = XAxisStepSize)
TruncateXCoordinate <- 10
if(TimeMax >= 1000){
TruncateXCoordinate = ceiling(TimeMax/(2*100))*100
}
else if(TimeMax >= 100){
TruncateXCoordinate = ceiling(TimeMax/(2*10))*10
}
else if(TimeMax >= 1){
TruncateXCoordinate = ceiling(TimeMax/2)
}
else{
TruncateXCoordinate = TimeMax/2
}
updateNumericInput(session, "crTruncateXCoordinate", min = TimeMin, max = TimeMax, value = TruncateXCoordinate)
PValXCoordinateMax <<- TimeMax
})
# Observe Status Column
observeEvent(input$selectedStatusColumn,{
unique_values <- dataset()[[input$selectedStatusColumn]]
updateSelectInput(session, "selectCensoredLabel", choices = unique_values)
})
# Observe Dependent Variable Type
observeEvent(input$selectedDependentVariableType,{
if(input$selectedDependentVariableType == "bool"){
updateSelectInput(session, "selectedDependentVariableColumn",
choices = colnames(dataset())[sapply(dataset(), function(col) length(unique(col)) == 2)])
if (input$selectedDataMode == "ex1") {
updateSelectInput(session, "selectedDependentVariableColumn", selected = "sex")
}
if (input$selectedDataMode == "ex2") {
updateSelectInput(session, "selectedDependentVariableColumn", selected = "Tumor_stage")
}
}
else{
updateSelectInput(session, "selectedDependentVariableColumn",
choices = colnames(dataset())[sapply(dataset(), function(col) {is.numeric(col) & sum(is.na(col)) == 0 } )])
}
})
pValueCoordinates <- reactive({
if(input$crPvaluePosition== 'bottom-center'){
PValYCoordinate <- 0.1
PValXCoordinate <- ceiling(PValXCoordinateMax/(2))
}
else if(input$crPvaluePosition == 'bottom-right'){
PValYCoordinate = 0.1
PValXCoordinate = ceiling(PValXCoordinateMax/(1.1))
}
else if(input$crPvaluePosition == 'center-left'){
PValYCoordinate = 0.5
PValXCoordinate = ceiling(PValXCoordinateMax/(99))
}
else if(input$crPvaluePosition == 'center-right'){
PValYCoordinate = 0.5
PValXCoordinate = ceiling(PValXCoordinateMax/(1.1))
}
else if(input$crPvaluePosition == 'upper-left'){
PValYCoordinate = 0.9
PValXCoordinate = ceiling(PValXCoordinateMax/(99))
}
else if(input$crPvaluePosition == 'upper-center'){
PValYCoordinate = 0.9
PValXCoordinate = ceiling(PValXCoordinateMax/(2))
}
else if(input$crPvaluePosition == 'upper-right'){
PValYCoordinate = 0.9
PValXCoordinate = ceiling(PValXCoordinateMax/(1.1))
} else {
# Default values if no position is selected
PValXCoordinate <- NULL
PValYCoordinate <- NULL
}
return(list(x=PValXCoordinate, y=PValYCoordinate))
})
# survfit
fit <- reactive({
duration <<- dataset()[[input$selectedTimeColumn]]
status <<- dataset()[[input$selectedStatusColumn]]
dependentVariable <<- dataset()[[input$selectedDependentVariableColumn]]
status <<- ifelse(status == input$selectCensoredLabel, 0, 1)
if(input$selectedDependentVariableType == "number"){
if(input$selectedDependentVariableSplit == "median"){
dependentVariable <- ifelse(dependentVariable > median(dependentVariable), 'high', 'low')
}
else if(input$selectedDependentVariableSplit == "upper"){
dependentVariable <- cut(
dependentVariable,
breaks = quantile(dependentVariable, probs = c(0, 0.25, 1), na.rm = TRUE),
labels = c("low", "high"),
include.lowest = TRUE
)
}
else if(input$selectedDependentVariableSplit == "lower"){
dependentVariable <- cut(
dependentVariable,
breaks = quantile(dependentVariable, probs = c(0, 0.75, 1), na.rm = TRUE),
labels = c("low", "high"),
include.lowest = TRUE
)
}
}
survfit(Surv(duration, status) ~ dependentVariable, data = dataset())
})
# Render the survival plot
plot_survival <- reactive({
legendList <- unique(dataset()[[input$selectedDependentVariableColumn]])
legendLabelOne <- paste(input$selectedDependentVariableColumn, "=", as.character(legendList[1]), sep = " ")
legendLabelTwo <- paste(input$selectedDependentVariableColumn, "=", as.character(legendList[2]), sep = " ")
if(input$selectedDependentVariableType == "number"){
legendLabelOne <- 'high'
legendLabelTwo <- 'low'
}
ggplot_surv <- ggsurvplot(fit(), data = dataset(),
size = input$crLineWidth,
linetype = input$crLineType,
censor.shape = input$crCensorShape,
censor.size = input$crCensorWidth,
surv.median.line = input$crMedianLineType,
break.time.by = input$crXAxisStepSize,
break.y.by = input$crYAxisStepSize,
pval = if (input$crPValueBoolean == "TRUE") TRUE else FALSE,
pval.size = input$crPvalueSize,
pval.coord = c(pValueCoordinates()$x, pValueCoordinates()$y),
palette = c(input$crClass1Color, input$crClass2Color),
xlab = if (nchar(input$crXTitle) != 0) input$crXTitle else "Time",
ylab = if (nchar(input$crYTitle) != 0) input$crYTitle else "Survival Probability",
legend.title= if (nchar(input$crLegendTitle) != 0) input$crLegendTitle,
legend.labs = c(
if(nchar(input$crLegendTitle1) != 0) input$crLegendTitle1 else legendLabelOne,
if(nchar(input$crLegendTitle2) != 0) input$crLegendTitle2 else legendLabelTwo
),
ggtheme = theme(panel.background = element_rect(fill = input$crBgColor),
panel.grid = if (input$crGridBoolean == "TRUE") element_line(colour = input$crGridColor, linewidth = input$crGridSize)
else element_blank(),
),
xlim = if (input$crTruncateXBoolean == "TRUE") c(0, input$crTruncateXCoordinate),
)
if(input$crPValueBoolean == "TRUE") ggplot_surv[["plot"]][["layers"]][[4]][["aes_params"]][["colour"]] = input$crPvalueColor
ggplot_surv$plot
})
# Render the plot using Plotly
output$survivalPlot <- renderPlotly({
plotHeight <- input$crPlotHeight * 37.795275591 # Convert cm to pixels
plotWidth <- input$crPlotWidth * 37.795275591 # Convert cm to pixels
ggplotly(plot_survival(), height = plotHeight, width = plotWidth)
})
output$plotDownload <- downloadHandler(
filename = function() {
paste("survival_plot.",input$plotFormat, sep = "") # Name of the file when downloaded
},
content = function(file) {
plotHeight <- (input$crPlotHeight/2.54)
plotWidth <- (input$crPlotWidth/2.54)
plotDPI <- as.numeric(input$plotDPI)
ggsave(file, plot = plot_survival() ,device = input$plotFormat, width=plotWidth, height=plotHeight, dpi = plotDPI)
}
)
}