forked from aya-mourad/Udemy-Courses-Shiny-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIVProject.R
438 lines (319 loc) · 13.6 KB
/
IVProject.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
#install.packages("readxl")
#install.packages("ggplot2")
#install.packages(shiny)
#install.packages("shinyjs")
#install.packages("plotly")
library(plotly)
remotes::install_github("deepanshu88/shinyDarkmode")
#install.packages(shinyDarkmode)
library(shiny)
library(shinythemes)
library(shinydashboard)
library(shinyjs)
library(shinyDarkmode)
library(dplyr)
#library("readxl")
library("ggplot2")
#to read data set
library(readr)
courseUdemy <-read_csv("udemy_courses.csv")
#check data type
str(courseUdemy)
# To Convert date to Known shape ex: 2022/2/22
courseUdemy$published_timestamp <-as.Date(courseUdemy$published_timestamp)
#check change is done
#check null values
colSums(is.na(courseUdemy))
sapply(courseUdemy, function(x) sum(is.na(x)))
#check duplicated values
any(duplicated(courseUdemy))
sum(duplicated(courseUdemy))
#check data validity for numerical columns
summaryStats <- function(column) {
c(Min = min(column, na.rm = TRUE),
Mean = mean(column, na.rm = TRUE),
Median = median(column, na.rm = TRUE),
Max = max(column, na.rm = TRUE),
'1st Qu' = quantile(column, 0.25, na.rm = TRUE),
'3rd Qu' = quantile(column, 0.75, na.rm = TRUE))
}
# Apply the function to a column (replace 'numericColumn' with your column name)
numericStats <- summaryStats(courseUdemy$price)
numericStats1 <- summaryStats(courseUdemy$num_subscribers)
numericStats2 <- summaryStats(courseUdemy$num_reviews)
numericStats3 <- summaryStats(courseUdemy$num_lectures)
# Round the results
round(numericStats, 2)
round(numericStats1, 2)
round(numericStats2, 2)
round(numericStats3, 2)
# Calculate daily and cumulative courses
daily_courses <- courseUdemy %>%
group_by(published_timestamp) %>%
summarise(DailyCount = n())
cumulative_courses <- daily_courses %>%
mutate(CumulativeCount = cumsum(DailyCount))
vars <- c("num_subscribers","price","num_reviews","num_lectures")
str(courseUdemy)
summary(courseUdemy)
ui <- dashboardPage(
#Toggle switch for light/dark mode
dashboardHeader(
# dark/light mode toggle
),
dashboardSidebar(
sidebarMenu(
menuItem("Dataset", tabName = "dataset", icon = icon("th")),
menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
menuItem("Instructions", tabName = "Instructions", icon = icon("book"))
)
),
dashboardBody(
use_darkmode(),
useShinyjs(),
tabItems(
# First tab content
tabItem(tabName = "dataset",
h1("Dataset is Udemy Courses"),
DT::dataTableOutput("DisplayData")
),
# New tab: instructions
tabItem(
tabName = "Instructions",
h1("Hope these instructions help you !"),
verbatimTextOutput("instructionOutput")
),
# Second tab content
tabItem(tabName = "visualization",
fluidPage(
#theme = shinytheme("superhero"),
titlePanel("Udemy Dataset"),
tabsetPanel(
tabPanel(title = "Histogram Plot",h1("Histogram Plot"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "bins",
label = "Number of bins:",
min = 0,max = 50,value = 30),
radioButtons(inputId ="chose_freq_dinsty", label="Choose Y Type:", choices = c("Absolute", "Density"))
),
mainPanel(
plotlyOutput(outputId = "histplot"),h2("Observation of the plot is : Most frequent course when the price = 20$ ")
)
)
),
tabPanel(title = "Bar charts",h1("Bar charts"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("categoryInput", "Select Categories:",
choices = unique(courseUdemy$subject),
selected = unique(courseUdemy$subject)[1])
),
mainPanel(
plotlyOutput(outputId = "barchart"),h2("Observation of the plot is : Both Business Finance and Web Development are equal frequency and minimum frequency is graphic design.")
)
)
),
# Single tab containing both plots
tabPanel(title = "Combined Plots",
# Checkbox to show/hide Pie Chart
checkboxInput("showPieChart", "Show Pie Chart", value = TRUE),
# Conditional UI for Pie Chart
uiOutput("pieChartUI"),
# Checkbox to show/hide Freqpoly Plot
checkboxInput("showFreqpolyPlot", "Show Freqpoly Plot", value = FALSE),
# Conditional UI for Freqpoly Plot
uiOutput("freqpolyPlotUI")
),
# ... Other tabs if any ...
tabPanel(title = "Box Plot",h1("Box Plot"),plotlyOutput(outputId = "boxPlot"),h2("Observation of the plot is : Web development courses vary the most in content duration and Musical instruments courses are right skewed.")),
tabPanel(title = "scatter Plot",h1("scatter Plot"),
sidebarLayout(
sidebarPanel(
selectInput('xcol', 'X Variable', vars,selected = vars[[3]]),
selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
),mainPanel(plotOutput(outputId = "scatterPlot"),h2("Observation of the plot is : Majority of subscribers don't leave reviews.")
)
)),
tabPanel(title = "line Chart Plot",h1("line Chart Plot"),
sidebarLayout(
sidebarPanel(
dateRangeInput("dateRange",
label = "Select Date Range",
start = as.Date("2013-01-01"),
end = as.Date("2017-01-01")
)
),
mainPanel(
plotlyOutput(outputId = "lineChart"),h2("Observation of the plot is :Year 2013 had the highest level of subscriber engagement or enrollment.")
)
)
),
tabPanel(title = "line chart plot 2",h1("line plot"),
sidebarLayout(
sidebarPanel(
radioButtons("plotType", "Choose plot type:",
choices = c("Daily" = "daily", "Cumulative" = "cumulative"))
),
mainPanel(
plotlyOutput(outputId = "line"),h2("Observation of the plot is :the number of courses published on Udemy shows a directly proportional relationship, this means that as time progresses, the number of published courses increases at a steady, consistent rate")
)
)
),
)
)
)
)
)
)
server <- function(input, output, session) {
darkmode(
bottom = "32px",
right = "32px",
left = "unset",
time = "0.5s",
mixColor = "#fff",
backgroundColor = "#fff",
buttonColorDark = "#100f2c",
buttonColorLight = "#fff",
saveInCookies = FALSE,
label = "???" ,
autoMatchOsTheme = TRUE
)
#Tables of Dataset
output$DisplayData <- DT::renderDataTable({
courseUdemy
#DT::datatable(Data_to_display(courseUdemy))
})
# instructions
output$instructionOutput <- renderText({
sentences <- c(
"Instructions : ",
"Use the dropdown menus to select parameters for visualizations.",
"Hover over data points for more information.",
"Toggle between dark and light mode for comfortable viewing.",
"Click on the 'Visualization' tab to explore charts and graphs.",
"Use the checkbox to select parameters for visualizations." ,
"Use checkboxes to dynamically show/hide different visualizations in combined plots tab",
"Use tooltips or hover effects for detailed information in the visualizations."
)
paste(sentences, collapse = "\n")
})
# Histogram plot
output$histplot <- renderPlotly({
x <- courseUdemy$price
if (input$chose_freq_dinsty == "Absolute")
{p <- ggplot(data.frame(x), aes(x = x)) +
geom_histogram(bins = as.numeric(input$bins), fill = "#007bc2", color = "white") +
labs(x = "Price",
y = input$chose_freq_dinsty,
title = "Histogram of Courses Price")
ggplotly(p, tooltip = c("x", "y"))
} else
{
p <- ggplot(data.frame(x), aes(x = x)) +
geom_histogram(aes(y = ..density..), bins = as.numeric(input$bins), fill = "#007bc2", color = "white") +
labs(x = "Price",
y = input$chose_freq_dinsty, # Dynamic y-axis label
title = "Histogram of Courses Price")
ggplotly(p, tooltip = c("x", "y"))
}
})
# Bar chart
filteredData <- reactive({
courseUdemy[courseUdemy$subject %in% input$categoryInput, ]
})
output$barchart <- renderPlotly({
dataToPlot <- filteredData()
p<-ggplot(dataToPlot, aes(subject)) + geom_bar(fill = "#FF6666")
ggplotly(p, tooltip = c("x", "y"))
})
# Box Plot
output$boxPlot <- renderPlotly({
sample_data <- courseUdemy[sample(nrow(courseUdemy), size = 300), ]
p<-ggplot(sample_data, aes(x = subject, y = content_duration)) + geom_boxplot()
ggplotly(p, tooltip = c("x", "y"))
})
# Freqpoly Plot
output$FreqpolyPlot <- renderPlot({
ggplot(courseUdemy, aes(num_lectures)) + geom_freqpoly()
})
# Pie Chart
courseUdemy$is_paid <- as.factor(courseUdemy$is_paid)
output$pieChart <- renderPlot({
pie(table(courseUdemy$is_paid),
labels = levels(courseUdemy$is_paid),
main = "Distribution of Courses ( Paid / Free )")
})
selectedData <- reactive({
courseUdemy[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selectedData(), input$clusters)
})
# Scatter Plot
output$scatterPlot <- renderPlot({
palette(c("#FF6666", "#EEE7DA", "#C1F2B0", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 1)
points(clusters()$centers, pch = 4, cex = 1, lwd = 1)
})
# Line Chart
filteredData1 <- reactive({
startDate <- input$dateRange[1]
endDate <- input$dateRange[2]
subset(courseUdemy, courseUdemy$published_timestamp >= startDate & courseUdemy$published_timestamp <= endDate)
})
output$lineChart <- renderPlotly({
dataToPlot1 <- filteredData1()
p<-ggplot(dataToPlot1, aes(x = published_timestamp, y = num_subscribers, group = 1)) +
geom_line(color = "blue") +
labs(title = "Number of Subscribers Over Time",
x = "Time Variable",
y = "Number of Subscribers") +
theme_minimal()
ggplotly(p, tooltip = c("x", "y"))
})
#show or hide pichart
output$pieChartUI <- renderUI({
if(input$showPieChart) {
tagList(
h1("Pie Chart Plot"),
plotOutput("pieChart"),
h2("Observation of the pie chart is: Most courses are paid."),
hr() # Optional separator
)
}
})
#show or hide freqpolyplot
output$freqpolyPlotUI <- renderUI({
if(input$showFreqpolyPlot) {
tagList(
h1("Freqpoly Plot"),
plotOutput("FreqpolyPlot"),
h2("Observation of the freqpoly plot is: Most courses have less than 100 lectures.")
)
}
})
output$line <- renderPlotly({
if(input$plotType == "daily") {
p<-ggplot(daily_courses, aes(x = published_timestamp, y = DailyCount)) +
geom_line() +
labs(title = "Daily Number of Courses Published on Udemy",
x = "Date", y = "Number of Courses")
} else {
p<-ggplot(cumulative_courses, aes(x = published_timestamp, y = CumulativeCount)) +
geom_line() +
labs(title = "Cumulative Number of Courses Published on Udemy",
x = "Date", y = "Cumulative Number of Courses")
}
ggplotly(p, tooltip = c("x", "y"))
})
####################################End of server
}
shinyApp(ui = ui, server = server)