-
Notifications
You must be signed in to change notification settings - Fork 0
/
withggplot_v3.11dev.R
441 lines (387 loc) · 17.1 KB
/
withggplot_v3.11dev.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
##############################Packages_Needed###################################
library(shiny)
library(readxl)
library(agricolae)
library(markdown)
library(bslib)
library(pastecs)
library(corrplot)
library(GGally)
library(ggplot2)
library(correlation)
library(flextable)
#####################################UI#########################################
# theme = shinytheme("cerulean")
ui <- fluidPage(theme = bs_theme(version = 4, bootswatch = "minty"),
navbarPage(title = div(img(src = "favicon.png", height = 30), "AgriAnalytics@R Ver. 3.10"),
header = list(
tags$div(
class = "navbar-header",
HTML(paste0("<a class='navbar-brand' href='#'>",
"<img src='ICAR.png' height='30'></a>")),
style = "float: right;"
)
),
tabPanel("Upload",
fluidRow(
column(width = 4,
fileInput("file", "Choose Excel File",
accept = c(".xlsx", ".csv")),
tableOutput("data_preview")
),
column(width = 8)
)
),
tabPanel("Descriptive Statistics",
fluidRow(
column(width = 3,
selectInput("desc_xvar", "Select a Variable",
choices = NULL),
selectInput("desc_grvar", "Select a Group Variable",
choices = NULL)
),
column(width = 9,
plotOutput("boxplot"),
uiOutput("summary")
)
)
),
tabPanel("Anova CRD",
fluidRow(
column(width = 4,
selectInput("xvar_crd", "Select X Variable",
choices = NULL),
selectInput("yvar_crd", "Select Y Variable",
choices = NULL),
actionButton("run_crd", "Run ANOVA")
),
column(width = 8,
verbatimTextOutput("anova_crd"),
verbatimTextOutput("anova_crd_2"),
downloadButton("download_anova_crd", "Download ANOVA Results as RTF"),
plotOutput("plot_crd"),
downloadButton("download_plot_crd", "Download ANOVA PLOT as PNG")
)
)
),
tabPanel("Anova RBD",
fluidRow(
column(width = 4,
selectInput("xvar_rbd", "Select Treatment Variable",
choices = NULL),
selectizeInput("yvar_rbd", "Select Response Variable",
choices = NULL, multiple = TRUE),
selectInput("rep_var_rbd", "Select Replication Variable",
choices = NULL),
actionButton("run_rbd", "Run ANOVA")
),
column(width = 8,
div(
verbatimTextOutput("anova_rbd"),
style = "max-height: 300px; overflow-y: scroll;"),
downloadButton("download_anova_rbd", "Download ANOVA Results as RTF"),
plotOutput("plot_rbd"),
downloadButton("download_plot_rbd", "Download ANOVA PLOT as PNG")
)
)
),
tabPanel("Correlation",
fluidRow(
column(width = 4,
selectizeInput("xvar_cor", "Select Variables",
choices = NULL, multiple = TRUE
),
uiOutput("cor_output_selector"),
actionButton("run_cor", "Run Correlation")
),
column(width = 8,
conditionalPanel(condition = "input.cor_output=='cor_out'",
verbatimTextOutput("cor_out"),
downloadButton("download_cor", "Download Correlation Results as RTF")
),
conditionalPanel(condition = "input.cor_output=='cor_out_2'",
verbatimTextOutput("cor_out_2"),
downloadButton("download_cor_2", "Download Individual Correlation Results as RTF")
),
conditionalPanel(condition = "input.cor_output=='plot_cor'",
plotOutput("plot_cor"),
downloadButton("download_plot_cor", "Download Correlogram as PNG")
),
conditionalPanel(condition = "input.cor_output=='plot_cor_2'",
plotOutput("plot_cor_2"),
downloadButton("download_plot_cor_2", "Download Scatterplot Matrix as PNG")
)
)
)
),
tabPanel("About",
uiOutput("about_ui")
)
)
)
################################################################################
####################################Server######################################
server <- function(input, output, session) {
df <- reactive({
req(input$file)
file <- input$file
ext <- tools::file_ext(file$datapath)
if (ext %in% c("xlsx", "xls")) {
read_excel(file$datapath)
} else if (ext == "csv") {
read.csv(file$datapath)
} else {
stop("Invalid file type")
}
})
###################################UPLOAD#######################################
# Data preview
output$data_preview <- renderTable({
df()
})
############################DESCRIPTIVE STATISTICS##############################
# Descriptive statistics output
observeEvent(df(), {
updateSelectInput(session, "desc_xvar", "Select Response Variable", choices = names(df()))
updateSelectInput(session, "desc_grvar", "Select Group Variable", choices = c("None", names(df())))
})
output$summary <- renderUI({
dat <- data.frame(
Descriptors = c("nbr.val", "nbr.null", "nbr.na", "min", "max", "range", "sum", "median", "mean", "SE.mean", "CI.mean.0.95", "var", "std.dev", "coef.var"),
stat.desc(df()) %>% round(2))
tbl <- flextable(dat)
htmltools_value(tbl)
})
output$boxplot <- renderPlot({
y <- input$desc_xvar
grv <- input$desc_grvar
if (grv == "None") {
boxplot(df()[[y]], main = paste("Boxplot of", y), xlab = "",
ylab = paste(y))
} else {
boxplot(df()[[y]]~df()[[grv]], main = paste("Boxplot of", y), xlab = paste(grv),
ylab = paste(y))
}
})
###################################ANOVA CRD####################################
# ANOVA_crd results output
observeEvent(df(), {
numeric_cols <- sapply(df(), is.numeric)
updateSelectInput(session, "xvar_crd", "Choose the Treatment variable", choices = names(df())[numeric_cols])
updateSelectInput(session, "yvar_crd", "Choose the Response variable", choices = names(df())[numeric_cols])
})
observeEvent(input$run_crd, {
if (!is.null(df())) {
x <- input$xvar_crd
y <- input$yvar_crd
model_crd <- lm(paste(y, "~", x), df())
model_lsd_crd <- LSD.test(model_crd, x, p.adj = "none")
#Tab ANOVA First row
output$anova_crd <- renderPrint({
anova(model_crd)
})
#Tab ANOVA Second row
output$anova_crd_2 <- renderPrint({
model_lsd_crd$groups
})
# Download ANOVA results as RTF
filename <- paste0("anova_crd_results_", Sys.Date(), ".rtf")
sink(filename)
print(anova(model_crd))
print(model_lsd_crd$groups)
sink()
output$download_anova_crd <- downloadHandler(
filename = function() {
filename
},
content = function(file) {
file.copy(filename, file)
}
)
#Tab ANOVA Third row
output$plot_crd <- renderPlot({
plot(model_lsd_crd)
})
#Download ANOVA plot as PNG
output$download_plot_crd <- downloadHandler(
filename = function() {
paste("anova_plot", ".png", sep = "")
},
content = function(file) {
png(file, width = 800, height = 800, res = 120)
par(cex.lab = 1.5, cex.main = 1.5, mar = c(2,2,2,2))
plot(model_lsd_crd)
dev.off()
}
)
}
})
###################################ANOVA RBD####################################
# ANOVA_rbd results output
observeEvent(df(), {
updateSelectInput(session, "xvar_rbd", "Choose the Treatment variable", choices = names(df()))
updateSelectInput(session, "yvar_rbd", "Choose the Response variable", choices = names(df()))
updateSelectInput(session, "rep_var_rbd", "Choose the Replication factor", choices = names(df()))
})
observeEvent(input$run_rbd, {
if (!is.null(df())) {
x <- input$xvar_rbd
y <- input$yvar_rbd
r <- input$rep_var_rbd
if (is.null(y)) {
return()
}
if (length(y) > 1) {
# create a list to store the results for each response variable
anova_list <- list()
for (i in 1:length(y)) {
# fit the ANOVA model for each response variable
model_rbd <- lm(paste(y[i], "~", r, "+", x), data = df())
model_lsd_rbd <- LSD.test(model_rbd, x, p.adj = "none")
anova_list[[y[i]]] <- list(anova = anova(model_rbd), lsd_groups = model_lsd_rbd$groups)
}
} else {
# fit the ANOVA model for a single response variable
model_rbd <- lm(paste(y, "~", r, "+", x), data = df())
model_lsd_rbd <- LSD.test(model_rbd, x, p.adj = "none")
# store the results in a list
anova_list <- list(anova = anova(model_rbd), lsd_groups = model_lsd_rbd$groups)
}
#Tab ANOVA First row
output$anova_rbd <- renderPrint({
anova_list
})
#Download ANOVA results as RTF
filename <- paste0("anova_results_", Sys.Date(), ".rtf")
sink(filename)
print(anova_list)
sink()
output$download_anova_rbd <- downloadHandler(
filename = function() {
filename
},
content = function(file) {
file.copy(filename, file)
}
)
#Tab ANOVA Third row
output$plot_rbd <- renderPlot({
plot(model_lsd_rbd)
})
#Download ANOVA plot as PNG
output$download_plot_rbd <- downloadHandler(
filename = function() {
paste("anova_plot", ".png", sep = "")
},
content = function(file) {
png(file, width = 800, height = 800, res = 120)
par(cex.lab = 1.5, cex.main = 1.5, mar = c(2,2,2,2))
plot(model_lsd_rbd)
dev.off()
}
)
}
})
#################################CORRELATION####################################
# Correlation results output
observeEvent(df(), {
numeric_cols <- sapply(df(), is.numeric)
updateSelectizeInput(session, "xvar_cor", "Choose Variables",
choices = names(df())[numeric_cols])
})
observeEvent(input$run_cor, {
if (!is.null(df())) {
x <- input$xvar_cor
y <- input$yvar_cor
model_cor <- round(cor(df()[, c(x, y = NULL)]), 2)
model_cor_2 <- correlation::correlation((df()[, c(x, y = NULL)]),include_factors = TRUE, method = "auto")
#Tab Correlation First row
output$cor_out <- renderPrint({
model_cor
})
#Download Correlation as RTF
filename <- paste0("Correlation_results_", Sys.Date(), ".rtf")
sink(filename)
print(model_cor)
sink()
output$download_cor <- downloadHandler(
filename = function() {
filename
},
content = function(file) {
file.copy(filename, file)
}
)
#Tab Correlation Second row
output$cor_out_2 <- renderPrint({
model_cor_2
})
#Download Correlation as RTF
filename <- paste0("Correlation_results_withp_", Sys.Date(), ".rtf")
sink(filename)
print(model_cor_2)
sink()
output$download_cor_2 <- downloadHandler(
filename = function() {
filename
},
content = function(file) {
file.copy(filename, file)
}
)
#Tab Correlation Third row
output$plot_cor <- renderPlot({
corrplot(model_cor)
})
#Download corrplot as PNG
output$download_plot_cor <- downloadHandler(
filename = function() {
paste("cor_plot", ".png", sep = "")
},
content = function(file) {
png(file, width = 800, height = 800, res = 120)
par(cex.lab = 1.5, cex.main = 1.5, mar = c(2,2,2,2))
corrplot(model_cor)
dev.off()
}
)
#Tab Correlation Third row
output$plot_cor_2 <- renderPlot({
ggpairs(df()[, x])
})
# Download corrplot as PNG_2
output$download_plot_cor_2 <- downloadHandler(
filename = function() {
paste("scatter_plot", ".png", sep = "")
},
content = function(file) {
ggsave(file, plot = ggpairs(df()[, x]))
}
)
}
})
# Correlation output selector
output$cor_output_selector <- renderUI({
selectInput(
"cor_output",
"Select correlation output",
choices = c("Correlation Matrix" = "cor_out",
"Correlation Matrix with P value" = "cor_out_2",
"Correlogram" = "plot_cor",
"Scatterplot Matrix" = "plot_cor_2")
)
})
# Display selected correlation output
output$selected_cor_output <- renderPrint({
eval(parse(text = input$cor_output))
})
###################################ABOUT########################################
#Read the contents of the README.md file
about_text <- readLines("README.md")
#Render the contents of the README.md file as Markdown
output$about_ui <- renderText({
HTML(renderMarkdown(about_text))
})
}
#################################App_Exec#######################################
shinyApp(ui, server)