-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
351 lines (320 loc) · 12.7 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
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
342
343
344
345
346
347
348
349
350
351
################
## LIBRARIES ##
################
#### LIBRARIES CHECK (IF ABSENT - INSTALL) ####
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("e1071")) install.packages("e1071")
if (!require("caret")) install.packages("caret")
if (!require("earth")) install.packages("earth")
if (!require("mda")) install.packages("mda")
if (!require("glmnet")) install.packages("glmnet")
if (!require("shiny")) install.packages("shiny")
if (!require("DT")) install.packages("DT")
if (!require("shinythemes")) install.packages("shinythemes")
#### LIBRARIES TO LOAD ####
library(ggplot2)
library(e1071)
library(caret)
library(earth)
library(mda)
library(glmnet)
library(shiny)
library(DT)
library(shinythemes)
###################
## APP STRUCTURE ##
###################
#### USER INTERFACE ####
ui <- fluidPage(
theme = shinytheme("yeti"), #A specific theme selected
h1("Automated Testing, Data Clasiffication", #App title, centrally aligned
align = "center"),
sidebarLayout(
sidebarPanel(
h3("Step 1: Data"),
fileInput( #Input should be a csv dataset
"Dataset",
"Input dataset",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"
)
),
textInput( #Column used as a classifying factor
"ColumnFactor",
"Factor column name",
value = NULL
),
textInput( #Columns used as data for classification
"ColumnsData",
"Data columns names (space separated)",
value = NULL
),
h3("Step 2: Method"),
selectInput( #Choice of a method
"Method", #(only 5 implemented so far)
"Method choice", #(only metric used is Accuracy/Kappa)
choices = list(
"knn" = "knn",
"svmRadial" = "svmRadial",
"lda" = "lda",
"fda" = "fda",
"glmnet" = "glmnet"
),
selected = NULL),
sliderInput( #Elements to be fed to TrainControl()
"NumberNo", #function
"Train Control: number",
min = 1,
max = 100,
value = 10
),
sliderInput(
"RepeatNo",
"Train Control: repeats",
min = 1,
max = 10,
value = 3
),
textInput( #Random sed
"SeedNo",
"RandomSeed",
value = NULL
),
checkboxInput( #Should function normalize data
"checkbox", #before testing (for algorithms
"Normalize data prior?", #without in-built function)
value = FALSE),
h3("Step 3: Results"),
downloadButton("downloadData", "Console printout"), #Downloading test printouts
downloadButton("downloadData2", "Box plots"), #Downloading boxplots
downloadButton("downloadData3", "Violin plots"), #Downloading violin plots
),
mainPanel(
tabsetPanel(
tabPanel(
"About the app",
p(" "),
strong("An example of a working Shiny app"),
p(" "),
p("This app was written as an
example how a specific element of R usage,
here testing the applicability of specific
classsifyig algorithms to numerical data,
can be wrapped in a GUI provided by the Shiny library.
Apart from testing proper, with visualisation of key metrics,
the app allows testing data inputted in a CSV file as well as
saving printouts from the training as txt and visualisations as
png (visualisatins available about ~15s after data genearation)."),
p(" "),
p("To run this app from GitHub
locally use the code below:"),
code('shiny::runGitHub("AutomatedClassificationApp","AndrzejRomaniuk",
ref = "main")'),
p(" "),
p("See the link below for the GitHub page"),
tags$a(href="https://github.com/AndrzejRomaniuk/AutomatedClassificationApp",
"github.com/AndrzejRomaniuk/AutomatedClassification"),
p(" "),
),
tabPanel(
"Dataset",
dataTableOutput( #Whole input dataset
'InputTable')
),
tabPanel(
"Dataset (selected)",
dataTableOutput( #Dataset, only selected columns
'InputTable2')
),
tabPanel( #Equation to be used
"Equation",
verbatimTextOutput(
"EquationFinal"
)
),
tabPanel( #How initial test looks like
"Model, details",
verbatimTextOutput(
"Model"
)
),
tabPanel(
"Box plots", #Range of Accuracy and Kappa obtained
plotOutput(
"ResultsVisualisation"
)
),
tabPanel(
"Violin plots", #Range of Accuracy and Kappa obtained 2
plotOutput(
"ResultsVisualisation2"
)
)
)
)
)
)
#### SERVER SIDE ####
server <- function(input, output, session) {
dataReactive <- reactive({ #Raw dataset
Input <- input$Dataset
if (is.null(Input)){
return(NULL)
} else{
DatasetUsed <- as.data.frame(read.csv(Input$datapath))
return(DatasetUsed)
}
})
dataReactive2 <- reactive({ #Dataset, only selected
Input <- input$Dataset
if (is.null(Input)){
return(NULL)
} else{
DatasetUsed <- as.data.frame(read.csv(Input$datapath))
if (input$ColumnsData == "" & input$ColumnFactor == "") {
return(NULL)
} else {
DatasetUsed <- DatasetUsed[stringReactive()]
DatasetUsed[,input$ColumnFactor] <- as.factor(
DatasetUsed[,input$ColumnFactor])
}
return(DatasetUsed)
}
})
stringReactive <- reactive({ #All columns considered
StringsUsed <- c(
input$ColumnFactor,strsplit(
input$ColumnsData, "\\s+"
)[[1]]
)
return(StringsUsed)
})
Equation <- reactive({ #Creating an equation for Train() function
if (input$ColumnsData == "" | input$ColumnFactor == "") {
return(NULL)
} else {
FactorSep <- paste(input$ColumnFactor,"~",sep = "")
ColumnsNames<- paste(c(strsplit(input$ColumnsData, "\\s+")[[1]]),
collapse= "+")
EquationUsed <- eval(parse(text=paste(FactorSep,ColumnsNames,sep = "")))
return(EquationUsed)
}
})
Control <- reactive({ #Compiling data for tainControl() function
FinalControl <- trainControl(
method="repeatedcv",
number=input$NumberNo,
repeats=input$RepeatNo
)
return(FinalControl)
})
Model <- reactive({ #Training Models
if(input$SeedNo == ""){
return(NULL)
} else {
set.seed(input$SeedNo)
if (input$checkbox == TRUE) {
fitting <- train(
Equation(),
data=dataReactive2(),
method=input$Method,
metric="Accuracy",
trControl=Control(),
preProc=c("center", "scale")
)
} else {
fitting <- train(
Equation(),
data=dataReactive2(),
method=input$Method,
metric="Accuracy",
trControl=Control()
)
}
return(fitting)
}
})
Summary <- reactive({ #Summary of Accuracy and Kappa
DataSummary1 <- data.frame(
matrix(
ncol = 2,
nrow = length(
Model()$resample$Accuracy
)
)
)
colnames(DataSummary1) <- c("Type","Value")
DataSummary1$Type <- "Accuracy"
DataSummary1$Value <- Model()$resample$Accuracy
DataSummary2 <- data.frame(
matrix(
ncol = 2,
nrow = length(
Model()$resample$Kappa
)
)
)
colnames(DataSummary2) <- c("Type","Value")
DataSummary2$Type <- "Kappa"
DataSummary2$Value <- Model()$resample$Kappa
DataSummary <- rbind(DataSummary1, DataSummary2)
return(DataSummary)
})
output$InputTable <- renderDataTable( #Rending a raw dataset
dataReactive(),
options = list(dom = 'ltp'),
rownames= FALSE
)
output$InputTable2 <- renderDataTable( #Rending data selected
dataReactive2(),
options = list(dom = 'ltp'),
rownames= FALSE
)
output$EquationFinal <- renderPrint( #Rendering how the final equation
{Equation()} #Looks like
)
output$Model <- renderPrint( #Rendering how the final traincontrol()
{Model()} #Looks like
)
vals <- reactiveValues() #External storage to ggplots (for later)
output$ResultsVisualisation <- renderPlot({ #Boxplots for accuracy and kappa
ggpl1 <- ggplot(Summary(), aes(x = Type, y = Value, fill = Type)) +
geom_boxplot() + theme_classic() + theme(legend.position = "none")
vals$ggpl1 <- ggpl1
print(ggpl1)
})
output$ResultsVisualisation2 <- renderPlot({ #Violin plots for accuracy and kappa
ggpl2 <- ggplot(Summary(), aes(x = Type, y = Value, fill = Type)) +
geom_violin() + theme_classic() + theme(legend.position = "none")
vals$ggpl2 <- ggpl2
print(ggpl2)
})
output$downloadData <- downloadHandler( #Creating a txt file to download
filename = function(){
paste("results.txt")
},
content = function(file) {
writeLines(paste(capture.output( {Model()} )), file)
}
)
output$downloadData2 <- downloadHandler( #Creating a png file (first) to download
filename = function() {"Boxplot.png"},
content = function(file) {
png(file)
print(vals$ggpl1)
dev.off()
}, contentType = 'image/png'
)
output$downloadData3 <- downloadHandler( #Creating a png file (second) to download
filename = function() {"Violinplot.png"},
content = function(file) {
png(file)
print(vals$ggpl2)
dev.off()
},contentType = 'image/png'
)
}
#### COMBINING UI AND SERVER ####
shinyApp(ui, server)