-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft1.R
290 lines (230 loc) · 8.56 KB
/
draft1.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
library(shiny)
library(shinythemes)
library(data.table)
library(ggplot2)
not_sel <- "Not Selected"
about_page <- tabPanel(
title = "About",
titlePanel("About"),
"Statistical Consultancy",
br(),
"2022 March",
br(),
"Ramindu de Silva - AS2017342",
br(),
"References - https://towardsdatascience.com/how-to-build-a-data-analysis-app-in-r-shiny-143bee9338f7"
)
main_page <- tabPanel(
title = "Analysis",
titlePanel("Inputs"),
sidebarLayout(
sidebarPanel(
title = "Inputs",
fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
selectInput("num_var_1", "Numerical Variable 1", choices = c(not_sel)),
selectInput("num_var_2", "Numerical Variable 2", choices = c(not_sel)),
selectInput("fact_var", "Factor Variable", choices = c(not_sel)),
br(),
actionButton("run_button", "Run Analysis", icon = icon("play"))
),
mainPanel(
tabsetPanel(
tabPanel(
title = "One numerical variable",
plotOutput("plot_1")
),
tabPanel(
title = "Association of two numerical variables",
plotOutput("plot_2")
),
tabPanel(
title = "Association of a numerical and factor variable",
plotOutput("plot_3")
),
tabPanel(
title = "One factor variable",
plotOutput("plot_4")
),
tabPanel(
title = "Summary statistics",
fluidRow(
column(width = 4, strong(textOutput("num_var_1_title"))),
column(width = 4, strong(textOutput("num_var_2_title"))),
column(width = 4, strong(textOutput("fact_var_title")))
),
fluidRow(
column(width = 4, tableOutput("num_var_1_summary_table")),
column(width = 4, tableOutput("num_var_2_summary_table")),
column(width = 4, tableOutput("fact_var_summary_table"))
),
fluidRow(
column(width = 12, strong("Combined Statistics"))
),
fluidRow(
column(width = 12, tableOutput("combined_summary_table"))
)
)
)
)
)
)
#One numerical variable
draw_plot_1 <- function(data_input, num_var_1, num_var_2, fact_var){
if(fact_var!=not_sel){
data_input[,(fact_var):= as.factor(data_input[,get(fact_var)])]
}
if(num_var_1 != not_sel & num_var_2 == not_sel & fact_var == not_sel){
ggplot(data = data_input,
aes_string(x = num_var_1)) +
geom_histogram()
}
else if(num_var_1 == not_sel & num_var_2 != not_sel & fact_var == not_sel){
ggplot(data = data_input,
aes_string(x = num_var_2)) +
geom_histogram()
}
}
#Association of two numerical variables
draw_plot_2 <- function(data_input, num_var_1, num_var_2, fact_var){
if(fact_var!=not_sel){
data_input[,(fact_var):= as.factor(data_input[,get(fact_var)])]
}
if(num_var_1 != not_sel & num_var_2 != not_sel & fact_var != not_sel){
ggplot(data = data_input,
aes_string(x = num_var_1, y = num_var_2, color = fact_var)) +
geom_point()
}
else if(num_var_1 != not_sel & num_var_2 != not_sel & fact_var == not_sel){
ggplot(data = data_input,
aes_string(x = num_var_1, y = num_var_2)) +
geom_point()
}
}
#Association of a numerical and factor variable
draw_plot_3 <- function(data_input, num_var_1, num_var_2, fact_var){
if(fact_var!=not_sel){
data_input[,(fact_var):= as.factor(data_input[,get(fact_var)])]
}
if(num_var_1 != not_sel & num_var_2 == not_sel & fact_var != not_sel){
ggplot(data = data_input,
aes_string(x = fact_var, y = num_var_1)) +
geom_boxplot()
}
else if(num_var_1 == not_sel & num_var_2 != not_sel & fact_var != not_sel){
ggplot(data = data_input,
aes_string(x = fact_var, y = num_var_2)) +
geom_boxplot()
}
}
#One factor variable
draw_plot_4 <- function(data_input, num_var_1, num_var_2, fact_var){
if(fact_var!=not_sel){
data_input[,(fact_var):= as.factor(data_input[,get(fact_var)])]
}
if(num_var_1 == not_sel & num_var_2 == not_sel & fact_var != not_sel){
ggplot(data = data_input,
aes_string(x = fact_var)) +
geom_bar()
}
}
create_num_var_table <- function(data_input, num_var){
if(num_var != not_sel){
col <- data_input[,get(num_var)]
if (length(col)>5000) col_norm <- sample(col,5000) else col_norm <- col
norm_test <- shapiro.test(col_norm)
statistic <- c("mean", "median", "Standard deviation","Q1", "Q3", "IQR",
"Shapiro p-value")
value <- c(round(mean(col),2), round(median(col),2), round(sd(col),2),
round(quantile(col, 0.25),2), round(quantile(col, 0.75),2),
round(IQR(col),2), norm_test$p.value)
data.table(statistic, value)
}
}
create_fact_var_table <- function(data_input, fact_var){
if(fact_var != not_sel){
freq_tbl <- data_input[,.N, by = get(fact_var)]
freq_tbl <- setnames(freq_tbl,c("factor_value", "count"))
freq_tbl
}
}
create_combined_table <- function(data_input, num_var_1, num_var_2, fact_var){
if(fact_var != not_sel){
if(num_var_1 != not_sel & num_var_2 != not_sel){
res_tbl <- data_input[,.(correlation = cor(get(num_var_1), get(num_var_2))), by = fact_var]
}
else if(num_var_1 != not_sel & num_var_2 == not_sel){
res_tbl <- data_input[,.(mean = mean(get(num_var_1))), by = fact_var]
}
else if(num_var_1 == not_sel & num_var_2 != not_sel){
res_tbl <- data_input[,.(mean = mean(get(num_var_2))), by = fact_var]
}
}
else if(num_var_1 != not_sel & num_var_2 != not_sel){
res_tbl <- data.table(
statistic = c("correlation"),
value = c(cor(
data_input[,get(num_var_1)],
data_input[,get(num_var_2)])))
}
return(res_tbl)
}
ui <- navbarPage(
title = "Exploratory Data Analysis",
main_page,
about_page
)
server <- function(input, output){
options(shiny.maxRequestSize=10*1024^2)
data_input <- reactive({
req(input$csv_input)
fread(input$csv_input$datapath)
})
observeEvent(data_input(),{
choices <- c(not_sel,names(data_input()))
updateSelectInput(inputId = "num_var_1", choices = choices)
updateSelectInput(inputId = "num_var_2", choices = choices)
updateSelectInput(inputId = "fact_var", choices = choices)
})
num_var_1 <- eventReactive(input$run_button,input$num_var_1)
num_var_2 <- eventReactive(input$run_button,input$num_var_2)
fact_var <- eventReactive(input$run_button,input$fact_var)
# plot
plot_1 <- eventReactive(input$run_button,{
draw_plot_1(data_input(), num_var_1(), num_var_2(), fact_var())
})
output$plot_1 <- renderPlot(plot_1())
plot_2 <- eventReactive(input$run_button,{
draw_plot_2(data_input(), num_var_1(), num_var_2(), fact_var())
})
output$plot_2 <- renderPlot(plot_2())
plot_3 <- eventReactive(input$run_button,{
draw_plot_3(data_input(), num_var_1(), num_var_2(), fact_var())
})
output$plot_3 <- renderPlot(plot_3())
plot_4 <- eventReactive(input$run_button,{
draw_plot_4(data_input(), num_var_1(), num_var_2(), fact_var())
})
output$plot_4 <- renderPlot(plot_4())
# 1-d summary tables
output$num_var_1_title <- renderText(paste("Num Var 1:",num_var_1()))
num_var_1_summary_table <- eventReactive(input$run_button,{
create_num_var_table(data_input(), num_var_1())
})
output$num_var_1_summary_table <- renderTable(num_var_1_summary_table(),colnames = FALSE)
output$num_var_2_title <- renderText(paste("Num Var 2:",num_var_2()))
num_var_2_summary_table <- eventReactive(input$run_button,{
create_num_var_table(data_input(), num_var_2())
})
output$num_var_2_summary_table <- renderTable(num_var_2_summary_table(),colnames = FALSE)
output$fact_var_title <- renderText(paste("Factor Var:",fact_var()))
fact_var_summary_table <- eventReactive(input$run_button,{
create_fact_var_table(data_input(), fact_var())
})
output$fact_var_summary_table <- renderTable(fact_var_summary_table(),colnames = FALSE)
# multi-d summary table
combined_summary_table <- eventReactive(input$run_button,{
create_combined_table(data_input(), num_var_1(), num_var_2(), fact_var())
})
output$combined_summary_table <- renderTable(combined_summary_table())
}
shinyApp(ui = ui, server = server)