gloignon commited on
Commit
b364de7
·
verified ·
1 Parent(s): ec0edff

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +45 -8
app.R CHANGED
@@ -2,6 +2,7 @@ library(shiny)
2
  library(udpipe)
3
  library(stringr)
4
  library(ggplot2)
 
5
 
6
  # Load the French bsd model (ensure it's downloaded and adjust path if necessary)
7
  model <- udpipe_load_model("french-gsd-ud-2.5-191206.udpipe")
@@ -17,7 +18,9 @@ ui <- fluidPage(
17
  textAreaInput("text", "Or enter French text directly:", value = "",
18
  placeholder = "Type or paste French text here",
19
  width = '100%', height = '200px', resize = "both"),
20
- actionButton("analyze", "Analyze")
 
 
21
  ),
22
 
23
  mainPanel(
@@ -96,11 +99,23 @@ server <- function(input, output, session) {
96
  unzip(input$corpus_zip$datapath, exdir = temp_dir)
97
  txt_files <- list.files(temp_dir, pattern = "\\.txt$", full.names = TRUE)
98
 
99
- # Calculate metrics for each text file and store in a list
100
- corpus_metrics <- lapply(txt_files, function(file) {
101
- text <- readLines(file, warn = FALSE)
102
- calculate_metrics(paste(text, collapse = " "))
103
- })
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  # Combine metrics into a data frame
106
  corpus_metrics_df <- do.call(rbind, corpus_metrics)
@@ -115,14 +130,15 @@ server <- function(input, output, session) {
115
  }
116
  })
117
 
118
- # Display box plots for corpus mode
119
  output$corpusPlots <- renderPlot({
120
  if (!is.null(results()) && results()$isCorpus) {
121
  corpus_metrics_df <- results()$data
122
- melted_df <- reshape2::melt(corpus_metrics_df)
123
 
124
  ggplot(melted_df, aes(x = variable, y = value)) +
125
  geom_boxplot() +
 
126
  labs(x = "Metric", y = "Value", title = "Corpus Analysis - Readability and Cohesion Metrics") +
127
  theme_minimal() +
128
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
@@ -134,7 +150,28 @@ server <- function(input, output, session) {
134
  !is.null(results()) && results()$isCorpus
135
  })
136
  outputOptions(output, "isCorpus", suspendWhenHidden = FALSE)
 
 
 
 
 
 
 
137
  }
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  # Run the application
140
  shinyApp(ui = ui, server = server)
 
2
  library(udpipe)
3
  library(stringr)
4
  library(ggplot2)
5
+ library(reshape2)
6
 
7
  # Load the French bsd model (ensure it's downloaded and adjust path if necessary)
8
  model <- udpipe_load_model("french-gsd-ud-2.5-191206.udpipe")
 
18
  textAreaInput("text", "Or enter French text directly:", value = "",
19
  placeholder = "Type or paste French text here",
20
  width = '100%', height = '200px', resize = "both"),
21
+ actionButton("analyze", "Analyze"),
22
+ tags$div(id = "progress", style = "display: none; margin-top: 10px;"),
23
+ tags$style("#progress {font-size: 14px; color: #337ab7;}")
24
  ),
25
 
26
  mainPanel(
 
99
  unzip(input$corpus_zip$datapath, exdir = temp_dir)
100
  txt_files <- list.files(temp_dir, pattern = "\\.txt$", full.names = TRUE)
101
 
102
+ corpus_metrics <- list()
103
+ n_files <- length(txt_files)
104
+
105
+ # Show progress to the user
106
+ updateProgress <- function(value) {
107
+ session$sendCustomMessage("updateProgress", list(value = value))
108
+ }
109
+ updateProgress(0)
110
+
111
+ # Calculate metrics for each text file
112
+ for (i in seq_along(txt_files)) {
113
+ text <- readLines(txt_files[i], warn = FALSE)
114
+ corpus_metrics[[i]] <- calculate_metrics(paste(text, collapse = " "))
115
+
116
+ # Update progress
117
+ updateProgress(i / n_files * 100)
118
+ }
119
 
120
  # Combine metrics into a data frame
121
  corpus_metrics_df <- do.call(rbind, corpus_metrics)
 
130
  }
131
  })
132
 
133
+ # Display box plots for corpus mode, using facets for individual scales
134
  output$corpusPlots <- renderPlot({
135
  if (!is.null(results()) && results()$isCorpus) {
136
  corpus_metrics_df <- results()$data
137
+ melted_df <- melt(corpus_metrics_df)
138
 
139
  ggplot(melted_df, aes(x = variable, y = value)) +
140
  geom_boxplot() +
141
+ facet_wrap(~ variable, scales = "free_y") +
142
  labs(x = "Metric", y = "Value", title = "Corpus Analysis - Readability and Cohesion Metrics") +
143
  theme_minimal() +
144
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
 
150
  !is.null(results()) && results()$isCorpus
151
  })
152
  outputOptions(output, "isCorpus", suspendWhenHidden = FALSE)
153
+
154
+ # JavaScript for updating progress bar
155
+ session$sendCustomMessage("updateProgress", list(value = 0))
156
+ observe({
157
+ session$sendCustomMessage("addProgress", "")
158
+ })
159
+
160
  }
161
 
162
+ # JavaScript for updating progress bar
163
+ js <- "
164
+ Shiny.addCustomMessageHandler('updateProgress', function(message) {
165
+ if (message.value === 0) {
166
+ $('#progress').show().html('Processing... 0%');
167
+ } else if (message.value === 100) {
168
+ $('#progress').html('Processing complete!').delay(1000).fadeOut();
169
+ } else {
170
+ $('#progress').html('Processing... ' + Math.round(message.value) + '%');
171
+ }
172
+ });
173
+ "
174
+ tags$head(tags$script(HTML(js)))
175
+
176
  # Run the application
177
  shinyApp(ui = ui, server = server)