-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_GPRdelineation.R
183 lines (155 loc) · 4.93 KB
/
app_GPRdelineation.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
if(!require("devtools")) install.packages("devtools")
devtools::install_github("emanuelhuber/RGPR")
library(RGPR)
if(!require("shiny")) install.packages("shiny")
library(shiny)
ui <- basicPage(
#actionButton("edit","edit"),
fileInput("file1", "Choose GPR File",
multiple = TRUE
),
# Input: Select separator ----
radioButtons("edit", "Edit",
choices = c(Draw = "draw",
Delete = "delete",
Select = "select"),
selected = "draw"),
actionButton("delete_selected", "Delete selected"),
plotOutput("plot1",
click = "plot_click",
brush = brushOpts(id = "plot_brush")
),
downloadButton("downloadData", "Download points "),
verbatimTextOutput("info"),
# verbatimTextOutput("click_info"),
tableOutput('table')
)
server <- function(input, output) {
output$value <- renderPrint({ input$edit })
click_saved <- reactiveValues(singleclick = NULL)
observeEvent(eventExpr = input$plot_click,
handlerExpr = {
click_saved$singleclick <- input$plot_click
if (input$edit == "draw"){
print(click_saved$singleclick$x)
xy <- data.frame(click_saved$singleclick$x,
click_saved$singleclick$y)
names(xy) <- c("x", "y")
rv$m <- rbind(rv$m, xy)
}else if(input$edit == "delete"){
if(nrow(rv$m) > 0){
i <- nearPoints(rv$m, click_saved$singleclick,
xvar = "x", yvar = "y", addDist = TRUE,
allRows = TRUE)
rv$m <- rv$m[!i$selected_,]
}
}
rv$rmrows <- rep(FALSE, nrow(rv$m))
}
)
brush_saved <- reactiveValues(singlebrush = NULL)
observeEvent(eventExpr = input$plot_brush,
handlerExpr = {
brush_saved$singlebrush <- input$plot_brush
if(input$edit == "select"){
j <- brushedPoints(rv$m, brush_saved$singlebrush,
xvar = "x", yvar = "y",
allRows = TRUE)
# print(j[["selected_"]])
rv$rmrows <- j[["selected_"]]
}
}
)
observeEvent(input$delete_selected, {
if(input$edit == "select"){
if(!is.null(rv$rmrows)){
# print("***", rv$rmrows, "***")
rv$m <- rv$m[!rv$rmrows, ]
rv$rmrows <- rep(FALSE, nrow(rv$m))
}
}
})
rv <- reactiveValues(m = data.frame(x = numeric(), y = numeric()),
rmrows =c())
xplt <- reactiveValues(v = NULL)
observeEvent(input$file1, {
req(input$file1)
# print(input$file1$datapath)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
tryCatch(
{
xplt$v <- RGPR::readGPR(dsn = input$file1$datapath)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
})
output$plot1 <- renderPlot({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
if(!is.null(xplt$v)){
plot(xplt$v)
}else{
plot(0,0, type = "n")
}
if(length(rv$m$x[-1] > 2)){
lines(rv$m$x, rv$m$y)
}
# if(length(rv$rmrows) == nrow(rv$m)){
if(!is.null(rv$rmrows)){
points(rv$m$x[!rv$rmrows], rv$m$y[!rv$rmrows])
points(rv$m$x[rv$rmrows], rv$m$y[rv$rmrows], pch = 20, col = "red")
}else{
points(rv$m$x, rv$m$y)
}
title(length(rv$m$x))
})
output$info <- renderText({
paste0(unlist(click_saved$singleclick))
})
.whichMin <- function(x,y){
which.min(abs(x-y))
}
output$downloadData <- downloadHandler(
filename = function() {
paste0("delineation_", Sys.Date(), ".csv")
},
content = function(file) {
if(nrow(rv$m) > 0 ){
if(length(xplt$v@coord) == 0){
xplt$v@coord <- matrix(0, nrow = ncol(xplt$v), ncol = 3)
xplt$v@coord[,1] <- xplt$v@pos
}
# xvalues <- posLine(xplt$v@coord)
xval <- relTrPos(xplt$v)
i <- sapply(rv$m$x, .whichMin, xval)
j <- sapply(rv$m$y, .whichMin, xplt$v@depth)
test <- i >= 0 & i <= length(xplt$v) &
j >= 0 & j <= nrow(xplt$v)
if(any(!test)) warning("there is a problem")
print(cbind(i, j))
}
write.csv(cbind(i, j), file, row.names = FALSE)
# write.csv(rv$m, file, row.names = FALSE)
}
)
# output$click_info <- renderText({
# paste0(unlist(click_saved$singleclick))
# })
observeEvent(eventExpr = input$edit, handlerExpr = {
# if (input$edit > 0) {
# rv$m <- rbind(rv$m,unlist(click_saved$singleclick))
# }
})
output$table <- renderTable({
if (is.null(rv$m)) {return()}
# print("***",rv$rmrows, "***")
}, 'include.rownames' = FALSE
, 'include.colnames' = TRUE
)
}
shinyApp(ui, server)