-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
294 lines (218 loc) · 7.89 KB
/
app.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
#
# This is a Shiny web application. You can run tinstall.packages("plotly")he application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
# http://shiny.rstudio.com/
#
library(shiny)
library(shinydashboard)
library(plotly)
library(EBImage)
library(ijtiff)
library(dygraphs)
library(magrittr)
library(readr)
library(shinyWidgets)
library(bslib)
source("helpers.R")
# Define UI for application
ui <-fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")),
# add the name of the tab you want to use as title in data-value),
navbarPage(
title = "Time Series Dashboard",
theme = bs_theme(bootswatch = "cyborg"),
# title
dashboardSidebar(),
# Sidebar with a slider input for number of bins]
dashboardBody(
fluidRow(
column(width = 3,
fileInput("imgFile", label = " Choose Image (.tif)", multiple = TRUE), # upload image file
radioButtons("channel_sel", label = "Select Channel to Display",
choices = list("1st Channel" = 1, "2nd Channel" = 2, "3rd Channel" = 3),
selected = 1)
),
column(width = 4,
# selectInput("dataset",
# label = "Choose a dataset",
# choices = list("rst1","rst2"),
# selected = rst1
# ),
airDatepickerInput("time", "Starting Time:",
timepicker = TRUE,
todayButton = TRUE,
clearButton = TRUE,
timepickerOpts = timepickerOptions(timeFormat = 'hh:ii')),
sliderInput("minimum",
label = "Choose Minimum",
min = 5000,
max = 30000,
value = 10000,
step = 100
),
sliderInput("maximum",
label = "Choose Maximum",
min = 50000,
max = 400000,
value = 200000,
step = 100
)
)
),
# Show a plot/image
fluidRow(
box(width = 4, title = h6("Image Stack"), status = 'primary',
displayOutput("actualImage")
),
box(width = 8,
dygraphOutput("dygraph")
)
),
fluidRow(
box(width = 4, title = h6("Select ROI"), status = 'warning',
plotlyOutput("meanStack")
),
box(width = 4, title = h6("ROI Info"),
downloadButton("downloadData", "Download Data"),
verbatimTextOutput("info")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
options(shiny.maxRequestSize = 600*1024^2)
# show image file output
output$files <- renderTable(input$imgFile)
# reactive values
glob <- reactiveValues(img = NULL, imgpath = "", nFrame = 1, nChannel = 1,
time = NULL, mean1 = list(), mean2 = list(), df = NULL,
df1 = NULL, df2 = NULL, don = NULL, don1 = NULL, don2 = NULL, projImg = NULL)
# update minimum
observeEvent(input$minimum,{
if(input$minimum > input$maximum){
updateSliderInput(session, "minimum", value = input$maximum)
}
}
)
# update maximum
observeEvent(input$maximum, {
if(input$maximum < input$minimum){
updateSliderInput(session, "maximum", value = input$minimum)
}
})
# read image files
newImg <- eventReactive({
input$imgFile
input$minimum
input$maximum
input$channel_sel},{
glob$imgPath <- input$imgFile$datapath # set path
glob$nFrame <- dim(read_tif(glob$imgPath))[4] # number of frames
glob$nChannel <- dim(read_tif(glob$imgPath))[3] # number of channels
glob$img <- read_tif(glob$imgPath) # read tiff image
# 1 channel
if(glob$nChannel == 1){
glob$img <- scaling(glob$img, input$minimum, input$maximum) # scale the image
glob$projImg <- EBImage::rotate(apply(glob$img, c(2,1), mean), angle = 90)
return(glob$img)
}
# multi-channel
else{
glob$img <- glob$img[, , as.numeric(input$channel_sel), ,drop = F]
glob$projImg <- EBImage::rotate(apply(glob$img, c(2,1), mean), angle = 90)
return(glob$img)
}
})
observeEvent(input$imgFile,{
output$actualImage <-renderDisplay({
ijtiff::display(newImg(), method = "browser") #display image
})
output$meanStack <- renderPlotly({
p <- plot_ly(z= glob$projImg, type = "heatmap", source = "A", height = 490) %>%
config(modeBarButtonsToAdd = c("drawcircle", "drawrect", "eraseshape")) %>%
event_register("plotly_relayout")
})
})
# reactive value containing ROI coordinates
crop1 <- reactiveVal()
crop2 <- reactiveVal()
# get relayout info from the plot
observeEvent(event_data("plotly_relayout", source = "A"),{
d <- event_data("plotly_relayout", source = "A")
val <- NULL
if(!is.null(d$"shapes[0].x0")){
val <- list(
xmin = d$"shapes[0].x0",
xmax = d$"shapes[0].x1",
ymax = d$"shapes[0].y0",
ymin = d$"shapes[0].y1"
)
crop1(val)
}
if(!is.null(d$"shapes[1].x0")){
val <- list(
xmin = d$"shapes[1].x0",
xmax = d$"shapes[1].x1",
ymax = d$"shapes[1].y0",
ymin = d$"shapes[1].y1"
)
crop2(val)
}
})
# compute time stamps and mean value of ROI
observeEvent({
input$time
crop1()},{
glob$mean1 <- seg(glob$img, crop1()$xmin, crop1()$xmax, crop1()$ymin, crop1()$ymax, glob$nFrame)
glob$time <- format(seq(as.POSIXct(input$time, tz = 'GMT')
, length.out = dim(newImg())[4], by = '1 min'),'%Y-%m-%d %H:%M') # generate time stamps
glob$time <- as.POSIXct(glob$time)
glob$df1 <- data.frame(time = glob$time, mean = unlist(glob$mean1))
glob$don1 <- xts(x = glob$df1$mean, order.by = glob$df1$time)
})
observeEvent(crop2(),{
glob$mean2 <- seg(glob$img, crop2()$xmin, crop2()$xmax, crop2()$ymin, crop2()$ymax, glob$nFrame)
glob$df2 <- data.frame(time = glob$time, mean = unlist(glob$mean2))
glob$don2 <- xts(x = glob$df2$mean, order.by = glob$df2$time)
})
observeEvent({
crop1()
crop2()}, {
mydata <- cbind(glob$df1, glob$df2)
df <- mydata[-3]
colnames(df) <- c('time','ROI1 mean','ROI2 mean')
output$info <- renderPrint({
head(df, 25) # display dataframe
})
output$downloadData <- downloadHandler(
filename = function() {
paste("myData-", Sys.Date(), ".csv", sep = "")
},
content = function(file){
write.csv(df, file, row.names = FALSE) # write df to csv file
})
}
)
# time series plot with ROI
output$dygraph <- renderDygraph({
req(glob$don1)
dygraph(cbind(glob$don1,glob$don2),
main = "Mean Intensity of ROI Over Time",
ylab = "Mean",
xlab = "Time") %>%
dySeries("glob.don1", label = "ROI1") %>%
dySeries("glob.don2", label = "ROI2") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2")) %>%
dyRangeSelector() %>%
dyOptions(digitsAfterDecimal = 7)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)