Upload Fonctions.R
Browse files- Fonctions.R +76 -0
Fonctions.R
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Fonctions
|
2 |
+
|
3 |
+
# Fonction pour filtrer les données
|
4 |
+
filter_data <- function(data, input, return_columns = NULL) {
|
5 |
+
# If no column is selected, return an empty data table
|
6 |
+
if (length(input$column) == 0) {
|
7 |
+
return(data[0])
|
8 |
+
}
|
9 |
+
|
10 |
+
filtered_data <- data[
|
11 |
+
Annee_unique >= input$date_range[1] &
|
12 |
+
Annee_unique <= input$date_range[2]
|
13 |
+
]
|
14 |
+
|
15 |
+
pattern <- input$motcle
|
16 |
+
|
17 |
+
# Create a logical vector to store matches across columns
|
18 |
+
matches <- rep(FALSE, nrow(filtered_data))
|
19 |
+
|
20 |
+
# Check each selected column for matches and combine results with "OR" logic
|
21 |
+
for (col in input$column) {
|
22 |
+
if(input$regex){
|
23 |
+
matches <- matches | grepl(pattern, filtered_data[[col]])
|
24 |
+
} else {
|
25 |
+
matches <- matches | grepl(pattern, filtered_data[[col]], fixed = TRUE)
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
# Filter the data based on the combined matches
|
30 |
+
filtered_data <- filtered_data[matches, ]
|
31 |
+
|
32 |
+
# If return_columns is specified, select only those columns
|
33 |
+
if (!is.null(return_columns)) {
|
34 |
+
filtered_data <- filtered_data[, ..return_columns]
|
35 |
+
}
|
36 |
+
|
37 |
+
return(filtered_data)
|
38 |
+
}
|
39 |
+
|
40 |
+
plot_histogram <- function(data_frame) {
|
41 |
+
if ("Annee_unique" %in% names(data_frame) && nrow(data_frame) > 0) {
|
42 |
+
min_year <- min(na.omit(data_frame[, Annee_unique]))
|
43 |
+
max_year <- max(na.omit(data_frame[, Annee_unique]))
|
44 |
+
breaks_hist <- seq(min_year, max_year, length.out = input$num_breaks + 1)
|
45 |
+
|
46 |
+
if (input$dist_type == "raw") {
|
47 |
+
hist(data_frame()[, Annee_unique],
|
48 |
+
main = "Distribution brute des notices",
|
49 |
+
xlab = "Année",
|
50 |
+
ylab = "Nombre",
|
51 |
+
border = "blue",
|
52 |
+
col = "lightblue",
|
53 |
+
breaks = breaks_hist)
|
54 |
+
} else {
|
55 |
+
# Compute relative distribution
|
56 |
+
filtered_counts <- hist(data_frame[, Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
|
57 |
+
total_counts <- hist(data[data$Annee_unique %in% data_frame[, Annee_unique], Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
|
58 |
+
relative_counts <- ifelse(total_counts == 0, 0, filtered_counts / total_counts)
|
59 |
+
|
60 |
+
# Ensure names.arg matches the length of relative_counts
|
61 |
+
names_for_bars <- round(seq(min_year, max_year, length.out = length(relative_counts)))
|
62 |
+
|
63 |
+
barplot(relative_counts,
|
64 |
+
main = "Distribution relative des notices",
|
65 |
+
xlab = "Année",
|
66 |
+
ylab = "Fréquence relative",
|
67 |
+
border = "blue",
|
68 |
+
col = "lightblue",
|
69 |
+
space = 0,
|
70 |
+
names.arg = names_for_bars)
|
71 |
+
}
|
72 |
+
} else {
|
73 |
+
plot.new()
|
74 |
+
title(main = "No data available for the selected criteria")
|
75 |
+
}
|
76 |
+
}
|