-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
125 lines (88 loc) · 4.28 KB
/
server.R
File metadata and controls
125 lines (88 loc) · 4.28 KB
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
shinyServer(function(input, output) {
# Load and read csv file
my_data <- reactive({read_csv(input$file$datapath)})
# When loaded, ask to choose the column with filepath
observeEvent(input$file,{
output$var_fp <- renderUI({
tagList(
radioButtons("var_fp", "Variable containing filepath:",
choiceNames = names(my_data()),
choiceValues = 1:length(names(my_data())),
selected=character(0))
)
})
})
# After choosing filepath column, ask if we start a new classification or
# resume a new one (if so, select the column)
observeEvent(input$var_fp ,{
output$var_cla <- renderUI({
tagList(
radioButtons("var_cla", "Variable containing previous classification:",
choiceNames = c("Start new classification (create new column)", names(my_data())),
choiceValues = c(0, 1:length(names(my_data()))),
selected=character(0))
)
})
})
#Reactive indexes (depends if we resume or start a classification)
ind <- eventReactive(input$var_cla, {
if(input$var_cla == 0){
seq.int(from = 1, to = nrow(my_data()), by = 1)
}else{
which(is.na(my_data()[, as.numeric(input$var_cla)]))
}
})
#When choice about classification is made, plot the image and start classifying
# Each time a button is pressed, save the result in reactive vector and display
# following image
observeEvent(input$var_cla, {
#Reactive counter that changes each time a button is pressed
counter <- reactiveValues(countervalue = 1)
#Reactive classification result
res <- reactiveValues(data = NULL)
#Plot the image reactively
output$imgPlot <- renderPlot({
plot(readImage(unlist(my_data()[ind()[counter$countervalue], as.numeric(input$var_fp)])))
})
# Observe Event for the 5 classes : when a button is pressed save the results in
# reactive vector and add one to the reactive counter to display following image
# You can change the class name depending on your needs
observeEvent(input$Class_1, {
res$data <- c(res$data, "Class_1")
counter$countervalue <- counter$countervalue + 1
})
observeEvent(input$Class_2, {
res$data <- c(res$data, "Class_2")
counter$countervalue <- counter$countervalue + 1
})
observeEvent(input$Class_3, {
res$data <- c(res$data, "Class_3")
counter$countervalue <- counter$countervalue + 1
})
observeEvent(input$Class_4, {
res$data <- c(res$data, "Class_4")
counter$countervalue <- counter$countervalue + 1
})
observeEvent(input$Class_5, {
res$data <- c(res$data, "Class_5")
counter$countervalue <- counter$countervalue + 1
})
#Button save : depends if we start or resume classification,
# In both cases, overwrite the original file
observeEvent(input$Save, {
if(as.numeric(input$var_cla) == 0){
save_data <- data.frame(my_data(),
"class" = c(res$data, rep(NA, nrow(my_data())))[1:nrow(my_data())])
write_csv(x = save_data, path = input$file$name)
}else{
save_data <- data.frame(my_data())
save_data[ind(), as.numeric(input$var_cla)] <- c(res$data, rep(NA, nrow(my_data())))[1:nrow(my_data()[ind(),])]
write_csv(x = save_data, path = input$file$name)
}
})
# Percentage counter, usefull when classifying large dataset.
output$test <- renderText({
paste0(round((ind()[1]+counter$countervalue)/nrow(my_data())*100, digits = 3), "%")
})
})
})