File size: 14,396 Bytes
288c719
 
b8b5684
288c719
 
 
 
b8b5684
288c719
 
 
 
 
 
 
 
 
b8b5684
 
288c719
b8b5684
288c719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab6660b
 
 
 
 
 
 
 
f27e965
e386d5d
f27e965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919803
f27e965
 
 
 
 
 
 
d919803
 
f27e965
d919803
f27e965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919803
f27e965
 
 
d919803
f27e965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919803
 
f27e965
 
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
# Fonction pour filtrer les données
filter_data <- function(data, input, return_columns = NULL) {
  # Vérifier si aucune colonne n'est sélectionnée
  if (length(input$column) == 0) {
    return(data[0])
  }
  
  # Filtrage par plage de dates
  filtered_data <- data[
    Annee_unique >= input$date_range[1] &
      Annee_unique <= input$date_range[2]
  ]
  
  pattern <- input$motcle
  matches <- rep(FALSE, nrow(filtered_data))
  
  for (col in input$column) {
    if (input$regex) {
      matches <- matches | grepl(pattern, filtered_data[[col]], ignore.case = TRUE)
    } else {
      matches <- matches | grepl(pattern, filtered_data[[col]], fixed = TRUE, ignore.case = TRUE)
    }
  }
  
  filtered_data <- filtered_data[matches, ]
  
  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)
}


extensions <- c("shiny",                            
                "data.table",
                "DT",
                "shinyWidgets",
                "stringr",
                "markdown")                             

sapply(extensions, inst_ext_f)

data <- readRDS("dolq_min.RDS") |> setDT()

readme_content <- readLines("README.md", warn = FALSE)

ui <- fluidPage(
  titlePanel("Dictionnaire des oeuvres littéraires du Québec, t. 1-6"),
  tabsetPanel(
    tabPanel("Recherche plein texte",
    sidebarLayout(
      sidebarPanel(
        noUiSliderInput(inputId = "date_range", 
                        label = "Sélectionner une période:",
                        min = 1830,
                        max = 1980,
                        value = c(1830, 1980),
                        format = wNumbFormat(decimals = 0),
                        tooltips = TRUE),
        br(),

        pickerInput(
          inputId = "column",
          label = h5("Choisissez les colonnes où effectuer la recherche:"),
          choices = c("Auteur_oeuvre", "Titre", "Auteur_notice", "Article",
                      "Depouillement", "Details_bibliographiques"),
          selected = "Article",
          options = list(`actions-box` = TRUE),
          multiple = TRUE
        ),
        pickerInput(
          inputId = "additional_columns",
          label = h5("Choisissez les colonnes à afficher:"),
          choices = c("Id", "Auteur_oeuvre", "Titre", "Annee_parution",  "Auteur_notice", "Article", "Volume", "Depouillement", "Details_bibliographiques"),
          selected = c("Auteur_oeuvre", "Titre", "Article"),
          options = list(`actions-box` = TRUE),
          multiple = TRUE
        ),
        textInput(
          inputId = "motcle",
          label = h5("Chaine de caractères à rechercher"),
          value = "Montréal"
        ),
        prettySwitch(
          inputId = "regex",
          label = "Utiliser une expression régulière?",
          fill = FALSE,
          status = "primary"
        ),
        downloadButton("downloadData", "Exporter le résultat de la table (csv)"),
        width = 4,
         hr(),
        
        tags$head(tags$link(rel = "stylesheet", href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css")),
        
        
        h5("Antisèche - expressions régulières"),
        tags$a(href = "regex.pdf", target = "_blank",
               tags$img(src = "regex.jpg", alt = "Thumbnail", width = "100%")),
        
        hr(),
        h5("Accéder aux volumes (pdf)"),
        # Add clickable images for each volume
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-1-des-origines-a-1900/", target = "_blank",
               tags$img(src = "DOLQ_01.jpg", alt = "Volume 1", width = "50%")),
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-2-1900-a-1939/", target = "_blank",
               tags$img(src = "DOLQ_02.jpg", alt = "Volume 2", width = "50%")),
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-3-1940-a-1959/", target = "_blank",
               tags$img(src = "DOLQ_03.jpg", alt = "Volume 3", width = "50%")),
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-4-1960-1969/", target = "_blank",
               tags$img(src = "DOLQ_04.jpg", alt = "Volume 4", width = "50%")),
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-5-1970-1975/", target = "_blank",
               tags$img(src = "DOLQ_05.jpg", alt = "Volume 5", width = "50%")),
        tags$a(href = "https://crilcq.org/dictionnaire-des-oeuvres-litteraires-du-quebec-tome-5-1970-1975/", target = "_blank",
               tags$img(src = "DOLQ_06.jpg", alt = "Volume 6", width = "50%")),
      ),
      
      mainPanel(h3("Distribution chronologique des notices"),
                
                radioButtons(inputId = "dist_type", 
                             label = "Type de distribution:",
                             choices = list("Distribution brute" = "raw", 
                                            "Distribution relative" = "relative"),
                             selected = "raw"),
                
                numericInput("num_breaks", label = "Nombre d'intervalles:", value = 65, min = 1),
              
                plotOutput("histogram"),
                
                downloadButton("download_graph", "Exporter le graphique"),
                downloadButton("download_table", "Exporter les données du graphique"),
                
                h3("Table"),
                textOutput("filtered_count"),
                DT::dataTableOutput('table'))
    )
    ),
    
    # Second tab: Documentation
    
    tabPanel("Documentation",
             uiOutput("readme") 
    )
  )
)

server <- function(input, output) {

  reactive_data <- reactive({
    filter_data(data, input, input$additional_columns)
  })
  
  reactive_data1 <- reactive({
    filter_data(data, input)
  })
  
  filtered_count <- reactive({
    nrow(reactive_data())
  })
  
  hist_data <- reactive({
    if ("Annee_unique" %in% names(reactive_data1()) && nrow(reactive_data1()) > 0) {
      # Ensure the data is numeric
      data_numeric <- as.numeric(reactive_data1()[, Annee_unique])
      
      # Remove NA values
      data_numeric <- na.omit(data_numeric)
      
      if (length(data_numeric) > 0) {
        min_year <- min(data_numeric)
        max_year <- max(data_numeric)
        breaks_hist <- seq(min_year, max_year, length.out = input$num_breaks + 1)
        
        # Raw counts
        raw_counts <- hist(data_numeric, plot=FALSE, breaks=breaks_hist)$counts
        
        # Relative counts
        total_counts <- hist(data[data$Annee_unique %in% reactive_data1()[, Annee_unique], Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
        
        relative_counts <- ifelse(total_counts == 0, 0, raw_counts / total_counts)
        
        breaks <- round(seq(min_year, max_year, length.out = length(raw_counts)))
        
        # Return data based on user's selection
        if (input$dist_type == "raw") {
          return(data.frame(Breaks = breaks, Counts = raw_counts))
        } else {
          return(data.frame(Breaks = breaks, RelativeCounts = relative_counts))
        }
      }
    }
    # Ensure a dataframe is always returned
    return(data.frame(Breaks = numeric(0), Counts = numeric(0)))
  })
  
  
  output$filtered_count <- renderText({
    paste("Nombre de notices filtrées:", filtered_count())
  })
  
  output$table <- DT::renderDataTable({
    datatable(reactive_data(), options = list(searching = FALSE), 
              callback = JS(paste0("
                table.on('draw.dt', function() {
                  var keyword = '", input$motcle, "';
                  $('td').each(function() {
                    var content = $(this).html();
                    var highlighted = content.replace(new RegExp(keyword, 'gi'), function(match) {
                      return '<span style=\"background-color: yellow;\">' + match + '</span>';
                    });
                    $(this).html(highlighted);
                  });
                });
              ")))
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("filtered_data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      fwrite(reactive_data(), file)
    }
  )
  
  output$histogram <- renderPlot({
    # Check if 'Annee_unique' column exists and has data
    if ("Annee_unique" %in% names(reactive_data1()) && nrow(reactive_data1()) > 0) {
      min_year <- min(na.omit(reactive_data1()[, Annee_unique]))
      max_year <- max(na.omit(reactive_data1()[, Annee_unique]))
      breaks_hist <- seq(min_year, max_year, length.out = input$num_breaks + 1)
      
      if (input$dist_type == "raw") {
        hist(reactive_data1()[, 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(reactive_data1()[, Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
        total_counts <- hist(data[data$Annee_unique %in% reactive_data1()[, 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")
    }
  })
  
  output$download_graph <- downloadHandler(
    filename = function() {
      paste("histogram_graph", Sys.Date(), ".png", sep = "_")
    },
    content = function(file) {
      png(file)
        # Check if 'Annee_unique' column exists and has data
        if ("Annee_unique" %in% names(reactive_data1()) && nrow(reactive_data1()) > 0) {
          min_year <- min(na.omit(reactive_data1()[, Annee_unique]))
          max_year <- max(na.omit(reactive_data1()[, Annee_unique]))
          breaks_hist <- seq(min_year, max_year, length.out = input$num_breaks + 1)
          
          if (input$dist_type == "raw") {
            hist(reactive_data1()[, 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(reactive_data1()[, Annee_unique], plot=FALSE, breaks=breaks_hist)$counts
            total_counts <- hist(data[data$Annee_unique %in% reactive_data1()[, 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")
        }
      dev.off()
    }
  )
  
  output$download_table <- downloadHandler(
    filename = function() {
      paste("histogram_data", Sys.Date(), ".csv", sep = "_")
    },
    content = function(file) {
      write.csv(hist_data(), file, row.names = FALSE)
    }
  )
  output$readme <- renderUI({
    # Read the content of the README.md file
    readme_content <- readLines("README.md", warn = FALSE)
    
    # Convert the markdown content to HTML for rendering in the Shiny app
    HTML(markdown::markdownToHTML(text = readme_content, fragment.only = TRUE))
  })
  
}


shinyApp(ui = ui, server = server)