File size: 2,784 Bytes
7a39695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f09951
 
 
 
 
 
 
 
 
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
# Fonctions

# Fonction pour filtrer les données
filter_data <- function(data, input, return_columns = NULL) {
  # If no column is selected, return an empty data table
  if (length(input$column) == 0) {
    return(data[0])
  }
  
  filtered_data <- data[
    Annee_unique >= input$date_range[1] &
      Annee_unique <= input$date_range[2]
  ]
  
  pattern <- input$motcle
  
  # Create a logical vector to store matches across columns
  matches <- rep(FALSE, nrow(filtered_data))
  
  # Check each selected column for matches and combine results with "OR" logic
  for (col in input$column) {
    if(input$regex){
      matches <- matches | grepl(pattern, filtered_data[[col]])
    } else {
      matches <- matches | grepl(pattern, filtered_data[[col]], fixed = TRUE)
    }
  }
  
  # Filter the data based on the combined matches
  filtered_data <- filtered_data[matches, ]
  
  # If return_columns is specified, select only those columns
  if (!is.null(return_columns)) {
    filtered_data <- filtered_data[, ..return_columns]
  }
  
  return(filtered_data)
}

plot_histogram <- function(data_frame) {
  if ("Annee_unique" %in% names(data_frame) && nrow(data_frame) > 0) {
    min_year <- min(na.omit(data_frame[, Annee_unique]))
    max_year <- max(na.omit(data_frame[, Annee_unique]))
    breaks_hist <- seq(min_year, max_year, length.out = input$num_breaks + 1)
    
    if (input$dist_type == "raw") {
      hist(data_frame()[, Annee_unique],
           main = "Distribution brute des notices",
           xlab = "Année",
           ylab = "Nombre",
           border = "blue",
           col = "lightblue",
           breaks = breaks_hist)
    } else {
      # Compute relative distribution
      filtered_counts <- hist(data_frame[, Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
      total_counts <- hist(data[data$Annee_unique %in% data_frame[, Annee_unique], Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
      relative_counts <- ifelse(total_counts == 0, 0, filtered_counts / total_counts)
      
      # Ensure names.arg matches the length of relative_counts
      names_for_bars <- round(seq(min_year, max_year, length.out = length(relative_counts)))
      
      barplot(relative_counts, 
              main = "Distribution relative des notices",
              xlab = "Année",
              ylab = "Fréquence relative",
              border = "blue",
              col = "lightblue",
              space = 0,
              names.arg = names_for_bars)
    }
  } else {
    plot.new()
    title(main = "No data available for the selected criteria")
  }
}

inst_ext_f <- function(extension) {
  if(!extension %in% rownames(installed.packages())) {
    install.packages(extension, dependencies = TRUE)
  }
  require(extension, character.only = TRUE)
}