-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
322 lines (247 loc) · 10.4 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
# Charger le package
library(FactoInvestigate)
library(shiny)
library(shinydashboard)
library(FactoMineR)
library(ggplot2)
library(readxl)
library(factoextra)
library(pander)
library(rsconnect)
# Définition de l'interface utilisateur pour la page des jeux de données
ui_dataset <- dashboardPage(
dashboardHeader(title = "Poverty_2014"),
dashboardSidebar(
sidebarMenu(
menuItem("About database", tabName = "display", icon = icon("database")),
menuItem("ACP", tabName = "acp", icon = icon("chart-bar")),
menuItem("Classification", tabName = "classification", icon = icon("chart-pie")),
menuItem("Télécharger les Résultats", tabName = "download_results", icon = icon("download")),
menuItem("Upload", tabName = "download", icon = icon("upload"))
),
tabItems(
tabItem(tabName = "download",
fluidRow(
column(width = 12,
fileInput("datafile", "Télécharger",
accept = c(".csv", ".xlsx")),
selectInput("filetype", "Sélectionner le type de fichier",
choices = c("csv", "xlsx")),
actionButton("loadData", "Télécharger")
)
)
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "display",
h2(" Afficher les Données"),
fluidRow(
column(width = 12,
tabsetPanel(
id = "download_display_tabs",
tabPanel("Afficher les Données", DT::dataTableOutput("data_table")),
tabPanel("Statistique descriptive", verbatimTextOutput("summary_output"))
)
)
)
),
tabItem(tabName = "acp",
h2("Analyse en Composantes Principales (ACP)"),
tabsetPanel(
id = "acp_tabs",
tabPanel("Correlation", DT::dataTableOutput("correlation_table")),
tabPanel("Inertie",
plotOutput("scree_plot"),
tableOutput("eigenvalues_table"),
tableOutput("variances_table")
),
tabPanel("Graphes",
plotOutput("var_graph"),
plotOutput("var_graph1"),
plotOutput("indiv_graph"),
plotOutput("indiv_graph1")
),
tabPanel("Résultats des variables",
DT::dataTableOutput("variable_results_table")
),
tabPanel("Résultats sur les individus",
DT::dataTableOutput("individual_results_table")
),
tabPanel("Résultats des variables qualitatives",
DT::dataTableOutput("categorical_variable_results_table")
)
)
),
tabItem(tabName = "classification",
h2("Classification hiérarchique CHA"),
fluidRow(
column(width = 6,
selectInput("num_clusters", "Nombre de clusters", choices = seq(10), selected = 3)
)
),
tabsetPanel(
id = "classification_tabs",
# Onglet "Graphes"
tabPanel("Graphes",
plotOutput("hcpc_tree_plot"),
plotOutput("hcpc_map_plot"),
plotOutput("hcpc_3d_map_plot")
)
)
),
tabItem(tabName = "download_results",
h2("Télécharger les Résultats"),
fluidRow(
column(width = 6,
actionButton("download_acp", "Télécharger les Résultats de l'ACP et de Classification")
)
),
fluidRow(
column(width = 12,
h4("Lien de téléchargement :"),
uiOutput("download_link_acp")
)
)
)
)
)
)
# Lancer l'application Shiny
shinyApp(
ui = ui_dataset,
server = function(input, output, session) {
# Charger les données sélectionnées
data <- reactive({
req(input$loadData)
if (is.null(input$datafile))
return(NULL)
file <- input$datafile$datapath
if (input$filetype == "csv") {
read.csv(file)
} else if (input$filetype == "xlsx") {
read_excel(file)
}
})
# Afficher les données sur le tableau de bord
output$data_table <- DT::renderDataTable({
data()
}, options = list(scrollX = TRUE, scrollY = "400px"))
output$summary_output <- renderPrint({
req(data()) # Assurez-vous d'avoir les données nécessaires pour les statistiques descriptives
# Génération du résumé des statistiques descriptives
summary(data())
})
# Matrice de corrélation
output$correlation_table <- DT::renderDataTable({
data <- data() # Supposons que vos données réactives sont stockées dans la variable 'data'
# Exclure la première colonne (catégorique) des données
data <- data[, -1]
matrice_corr <- cor(data)
# Arrondir chaque élément de la matrice de corrélation à deux décimales
matrice_corr_arrondie <- round(matrice_corr, 2)
}, options = list(scrollX = TRUE, scrollY = "300px"))
# Effectuer l'ACP et stocker les résultats dans res.PCA
res.PCA <- reactive({
req(data())
PCA(data(), quali.sup = c(1), graph = FALSE)
})
# Graphique des variables de l'ACP
output$var_graph <- renderPlot({
plot.PCA(res.PCA(), choix = "var", title = "Graphe des variables de l'ACP")
})
# Graphique des individus de l'ACP
output$indiv_graph <- renderPlot({
plot.PCA(res.PCA(), title = "Graphe des individus de l'ACP")
})
output$var_graph1 <- renderPlot({
req(res.PCA())
fviz_pca_var(res.PCA(), col.var = "contrib")
})
output$indiv_graph1 <- renderPlot({
req(res.PCA())
fviz_pca_ind(res.PCA(), col.ind = "cos2", repel = TRUE)
})
# Graphique de l'inertie
output$scree_plot <- renderPlot({
data <- data() # Assuming your data is reactive and stored in the variable 'data'
# Exclude the first column (categorical) from the data
data <- data[, -1]
res <- prcomp(data, center = TRUE, scale = TRUE)
fviz_screeplot(res, addlabels = TRUE)
})
# Tableau des valeurs propres
output$eigenvalues_table <- renderTable({
res.PCA()$eig
})
# Tableau des variances
output$variances_table <- renderTable({
res.PCA()$var
})
# Observer les changements de la valeur de input$download_acp
observeEvent(input$download_acp, {
# Générer le fichier HTML à partir du fichier Rmd
rmarkdown::render("resultat.Rmd", output_file = "resultat.pdf")
})
output$download_link_acp <- renderUI({
if (file.exists("resultat.pdf")) {
downloadLink(
"download_link_acp",
label = "Cliquez ici pour télécharger les résultats",
href = "resultat.pdf",
class = "btn btn-primary"
)
}
})
# Tableau des résultats des variables
output$variable_results_table <- DT::renderDataTable({
res.PCA()$var$coord
}, options = list(scrollX = TRUE, scrollY = "400px"))
# Tableau des résultats sur les individus
output$individual_results_table <- DT::renderDataTable({
res.PCA()$ind$coord
}, options = list(scrollX = TRUE, scrollY = "400px"))
# Tableau des résultats des variables qualitatives
output$categorical_variable_results_table <- DT::renderDataTable({
res.PCA()$quali$coord
}, options = list(scrollX = TRUE, scrollY = "400px"))
# Observer les changements de la valeur de nb_clusters
observe({
req(res.PCA(), input$num_clusters)
nb_clusters <- as.integer(input$num_clusters)
# Vérifier si nb_clusters est défini et est un nombre valide
if (!is.na(nb_clusters) && nb_clusters >= 2) {
res <- HCPC(res.PCA(), nb.clust = nb_clusters, consol = TRUE, graph = FALSE)
# Mettre à jour les sorties avec les nouveaux résultats
output$hcpc_tree_plot <- renderPlot({
plot.HCPC(res, choice = 'tree', title = 'Arbre hiérarchique')
})
output$hcpc_map_plot <- renderPlot({
plot.HCPC(res, choice = 'map', draw.tree = FALSE, title = 'Plan factoriel')
})
output$hcpc_3d_map_plot <- renderPlot({
plot.HCPC(res, choice = '3D.map', ind.names = FALSE, centers.plot = FALSE, angle = 60, title = 'Arbre hiérarchique sur le plan factoriel')
})
output$class_characterization_table <- renderTable({
res$desc.axes
})
}
})
# ... Ajoutez ici le code pour le téléchargement des résultats ..
observeEvent(input$download_acp, {
# Exécution de la PCA avec la variable qualitative
data <- data() # Assuming your data is reactive and stored in the variable 'data'
# Exclude the first column (categorical) from the data
data <- data[, -1]
res <- PCA(data, graph = FALSE)
# Génération du fichier HTML
html_output <- capture.output(Investigate(res)) # Correction : Utilisez 'Investigate' avec une majuscule
html_file <- "resultat.html" # Correction : Utilisez un nom de fichier approprié
writeLines(html_output, con = html_file)
# Définir le lien de téléchargement
output$download_link_acp <- renderUI({
tags$a(href = html_file, download = "resultat.html", "Télécharger le fichier HTML")
})
})
})