cella110n commited on
Commit
c8ec4e7
·
1 Parent(s): 487b330

Delete app_0813.R

Browse files
Files changed (1) hide show
  1. app_0813.R +0 -604
app_0813.R DELETED
@@ -1,604 +0,0 @@
1
- rm(list=ls())
2
-
3
- # 必要なライブラリを読み込む
4
- library(shiny)
5
-
6
- # 事前に定義された関数を含むRスクリプトを読み込む
7
- source("./script.R")
8
-
9
- # パネル用関数
10
- orderInputPanel <- function() {
11
- conditionalPanel(
12
- condition = "output.showOrderInput",
13
- numericInput("target_order", "Target Order:", value = 1, min = 1),
14
- actionButton("confirm_order", "Confirm Order"),
15
- actionButton("cancel_order", "Cancel")
16
- )
17
- }
18
-
19
- textInputPanel <- function() {
20
- conditionalPanel(
21
- condition = "output.showTextInput",
22
- textInput("related_captions_input", "Enter related captions (comma separated):"),
23
- actionButton("confirm_text", "Confirm Order"),
24
- actionButton("cancel_text", "Cancel")
25
- )
26
- }
27
-
28
- # UIを定義
29
- ui <- fluidPage(
30
- # アプリケーションのタイトル
31
- titlePanel("SDtagEditoR"),
32
-
33
- # CSSを追加
34
- tags$style(type = "text/css",
35
- "#image_captions { white-space: pre-wrap; }",
36
- ".shiny-action-button { margin-bottom: 50px; }"
37
- ),
38
-
39
- # JavaScript関数を追加
40
- tags$head(tags$script(HTML("
41
- function copyToClipboard(text) {
42
- var dummy = document.createElement('textarea');
43
- document.body.appendChild(dummy);
44
- dummy.value = text;
45
- dummy.select();
46
- document.execCommand('copy');
47
- document.body.removeChild(dummy);
48
- }
49
- "))),
50
-
51
- # ユーザー入力を受け付ける部分
52
- sidebarLayout(
53
- sidebarPanel(
54
- # ディレクトリパスを入力するテキストボックス
55
- textInput("directory_path", "Enter the directory path:", value = ""),
56
-
57
- # キャプションデータを読み込むボタン
58
- actionButton("load_data", "Load Captions Data"),
59
- tags$br(),
60
- tags$br(),
61
-
62
- conditionalPanel(
63
- condition = "output.dataLoaded",
64
-
65
- # 画像の選択
66
- selectInput("selected_image", "Choose an image:", choices = NULL),
67
-
68
- # 前後の画像に移動するボタン
69
- actionButton("prev_image", "Prev"),
70
- actionButton("next_image", "Next"),
71
- tags$br(),
72
- tags$br(),
73
-
74
- # タブセット
75
- actionButton("shuffle_single", "Shuffle Order for Current Image"),
76
- actionButton("shuffle_all", "Shuffle Order for All Images"),
77
- tags$br(),
78
- tags$br(),
79
- numericInput("frequency_threshold", "Frequency Threshold:", value = 5, min = 1),
80
- actionButton("remove_low_freq", "Remove Low Frequency Captions"),
81
- tags$br(),
82
- tags$br(),
83
- textInput("edit_caption", "Caption to Edit:", ""),
84
- actionButton("remove_single", "Remove from Single"),
85
- actionButton("remove_from_all", "Remove from All"),
86
- # actionButton("edit_interactively", "Edit Interactively"),
87
- tags$br(),
88
- actionButton("add_single", "Add to Single"),
89
- actionButton("add_to_all", "Add to All"),
90
- tags$br(),
91
- actionButton("move_caption", "Move Caption"),
92
- actionButton("move_caption_all", "Move Caption of All"),
93
- tags$br(),
94
- actionButton("remove_related_single", "Remove Related from Single"),
95
- actionButton("remove_related_all", "Remove Related from all"),
96
- orderInputPanel(),
97
- textInputPanel(), # ここで関数を呼び出しています
98
- # conditionalPanel(
99
- # condition = "output.showCaptionConfirmation",
100
- # tags$div("Do you want to remove the caption '", tags$span(id="confirm_caption", ""), "' from the selected image?"),
101
- # actionButton("yes_button", "Yes"),
102
- # actionButton("no_button", "No"),
103
- # actionButton("end_button", "End")
104
- # )
105
- )
106
- ),
107
-
108
- mainPanel(
109
- fluidRow(
110
- column(6,
111
- imageOutput("image_display", width = "100%", height = "50%"),
112
- tags$br(),
113
- uiOutput("image_captions")
114
- ),
115
- column(6,
116
- verbatimTextOutput("log_output"), # ログの表示領域を追加
117
- fluidRow( # この行を追加
118
- column(6, tableOutput("selected_image_captions")),
119
- column(6, tableOutput("caption_frequency_table_with_links"))
120
- )
121
- )
122
- )
123
- )
124
-
125
- )
126
- )
127
-
128
-
129
- # サーバーロジックを定義
130
- server <- function(input, output, session) {
131
- # リアクティブな変数を定義
132
- directory_path <- reactiveVal()
133
- captions_data <- reactiveVal()
134
- caption_frequency <- reactiveVal()
135
- operation_mode <- reactiveVal(NULL)
136
- log_text <- reactiveVal("Log:\n")
137
-
138
- # ログを追加する関数
139
- add_log <- function(message) {
140
- current_log <- log_text()
141
- new_log <- paste(current_log, message, "\n")
142
- log_text(new_log)
143
- }
144
-
145
- # ログを更新する関数
146
- update_log <- function(message) {
147
- log_text(message)
148
- }
149
-
150
- # 読み込み確認
151
- output$dataLoaded <- reactive({
152
- !is.null(captions_data())
153
- })
154
- outputOptions(output, "dataLoaded", suspendWhenHidden=FALSE)
155
-
156
- ## Load Data
157
- observeEvent(input$load_data, {
158
- update_log("Loading captions data...")
159
-
160
- # ボタンが押されたときに実行されるコード
161
- temp_directory_path <- isolate(input$directory_path)
162
- temp_captions_data <- read_captions_from_directory(temp_directory_path)
163
- temp_caption_frequency <- get_caption_frequency(temp_captions_data)
164
-
165
- # リアクティブな変数を更新
166
- directory_path(temp_directory_path)
167
- captions_data(temp_captions_data)
168
- caption_frequency(temp_caption_frequency)
169
-
170
- # 画像の選択のための選択肢を更新
171
- unique_images <- unique(captions_data()$image_path)
172
- updateSelectInput(session, "selected_image", choices = unique_images, selected = unique_images[1])
173
-
174
- update_log("Captions data loaded.")
175
- })
176
-
177
- # 画像の移動
178
- observeEvent(input$prev_image, {
179
- current_index <- which(unique(captions_data()$image_path) == input$selected_image)
180
- if (current_index > 1) {
181
- updateSelectInput(session, "selected_image", selected = unique(captions_data()$image_path)[current_index - 1])
182
- }
183
- })
184
-
185
- observeEvent(input$next_image, {
186
- current_index <- which(unique(captions_data()$image_path) == input$selected_image)
187
- if (current_index < length(unique(captions_data()$image_path))) {
188
- updateSelectInput(session, "selected_image", selected = unique(captions_data()$image_path)[current_index + 1])
189
- }
190
- })
191
-
192
- # Shuffle Order for Current Image
193
- observeEvent(input$shuffle_single, {
194
- if (!is.null(operation_mode())) {
195
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
196
- return()
197
- }
198
-
199
- current_image <- input$selected_image
200
- updated_data <- captions_data()
201
- captions_for_current_image <- filter(updated_data, image_path == current_image)
202
-
203
- # Orderをランダムにする
204
- new_order <- sample(1:nrow(captions_for_current_image), nrow(captions_for_current_image))
205
- captions_for_current_image$caption_order <- new_order
206
-
207
- # データを更新
208
- updated_data[updated_data$image_path == current_image, ] <- captions_for_current_image
209
- captions_data(updated_data)
210
-
211
- update_log("captions of this image was shauffled.")
212
- })
213
-
214
- # Shuffle Order for All Images
215
- observeEvent(input$shuffle_all, {
216
- if (!is.null(operation_mode())) {
217
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
218
- return()
219
- }
220
-
221
- updated_data <- captions_data()
222
- unique_images <- unique(updated_data$image_path)
223
-
224
- for (image in unique_images) {
225
- captions_for_image <- filter(updated_data, image_path == image)
226
- new_order <- sample(1:nrow(captions_for_image), nrow(captions_for_image))
227
- captions_for_image$caption_order <- new_order
228
- updated_data[updated_data$image_path == image, ] <- captions_for_image
229
- }
230
-
231
- captions_data(updated_data)
232
- update_log("captions of all images was shauffled.")
233
- })
234
-
235
- # remove_low_freqボタンが押されたときの処理
236
- observeEvent(input$remove_low_freq, {
237
- if (!is.null(operation_mode())) {
238
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
239
- return()
240
- }
241
-
242
- if (!is.null(captions_data())) {
243
- threshold <- input$frequency_threshold
244
- updated_data <- remove_low_frequency_captions(captions_data(), threshold)
245
- captions_data(updated_data)
246
-
247
- # キャプションの頻度を更新
248
- updated_caption_frequency <- get_caption_frequency(updated_data)
249
- caption_frequency(updated_caption_frequency)
250
- }
251
-
252
- update_log("Low frequency captions removed.")
253
- })
254
-
255
- # Remove Captions from All Images
256
- observeEvent(input$remove_from_all, {
257
- if (!is.null(operation_mode())) {
258
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
259
- return()
260
- }
261
-
262
- # 指定されたキャプションをすべての画像から削除
263
- target_caption <- input$edit_caption
264
- updated_data <- captions_data()
265
- unique_images <- unique(updated_data$image_path[updated_data$caption == target_caption])
266
- for (image in unique_images) {
267
- updated_data <- remove_caption_and_adjust_order(updated_data, image, target_caption)
268
- }
269
- captions_data(updated_data)
270
-
271
- # キャプションの頻度を更新
272
- updated_caption_frequency <- get_caption_frequency(updated_data)
273
- caption_frequency(updated_caption_frequency)
274
-
275
- update_log("Caption removed from all images.")
276
- })
277
-
278
- # Edit Captions Interactivelyの処理
279
- # グローバル変数として画像のインデックスと画像リストを保持
280
- # image_index <- reactiveVal(1)
281
- # image_list <- reactiveVal()
282
- # show_confirmation <- reactiveVal(FALSE)
283
-
284
- # observeEvent(input$edit_interactively, {
285
- # update_log("Starting interactive caption editing...")
286
- #
287
- # # 該当するキャプションを持つ画像のリストを取得
288
- # target_caption <- input$edit_caption
289
- # images <- search_by_caption(captions_data(), target_caption)$image_path
290
- # image_list(images)
291
- # image_index(1)
292
- # show_confirmation(TRUE)
293
- # updateSelectInput(session, "selected_image", selected = images[1])
294
- # })
295
- #
296
- # observeEvent(input$yes_button, {
297
- # update_log(paste("Caption removed from image:", input$selected_image))
298
- #
299
- # # キャプションを削除
300
- # captions_data(remove_caption_and_adjust_order(captions_data(), input$selected_image, input$edit_caption))
301
- #
302
- # # 次の画像を表示
303
- # image_index(image_index() + 1)
304
- # if (image_index() <= length(image_list())) {
305
- # updateSelectInput(session, "selected_image", selected = image_list()[image_index()])
306
- # } else {
307
- # show_confirmation(FALSE)
308
- # update_log("Interactive caption editing ended.")
309
- # }
310
- # })
311
- #
312
- # observeEvent(input$no_button, {
313
- # update_log(paste("Caption retained for image:", input$selected_image))
314
- #
315
- # # 次の画像を表示
316
- # image_index(image_index() + 1)
317
- # if (image_index() <= length(image_list())) {
318
- # updateSelectInput(session, "selected_image", selected = image_list()[image_index()])
319
- # } else {
320
- # show_confirmation(FALSE)
321
- # update_log("Interactive caption editing ended.")
322
- # }
323
- # })
324
- #
325
- # observeEvent(input$end_button, {
326
- # # プロセスを終了
327
- # image_index(1)
328
- # show_confirmation(FALSE)
329
- # update_log("Interactive caption editing ended.")
330
- # })
331
- #
332
- # output$showCaptionConfirmation <- reactive({
333
- # show_confirmation()
334
- # })
335
- #
336
- # outputOptions(output, "showCaptionConfirmation", suspendWhenHidden=FALSE)
337
-
338
- # output$confirm_caption <- renderText({
339
- # input$edit_caption
340
- # })
341
-
342
- # Remode/Add Single/Add all/move captionの処理
343
- showOrderInput <- reactiveVal(FALSE)
344
- showTextInput <- reactiveVal(FALSE)
345
-
346
- observeEvent(input$remove_single, {
347
- if (!is.null(operation_mode())) {
348
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
349
- return()
350
- }
351
- target_caption <- input$edit_caption
352
- if (target_caption %in% captions_data()$caption[captions_data()$image_path == input$selected_image]) {
353
- updated_data <- remove_caption_and_adjust_order(captions_data(), input$selected_image, target_caption)
354
- captions_data(updated_data)
355
- update_log("Caption removed from this image.")
356
- } else {
357
- showNotification("Error: Caption not found in the selected image.", type = "error")
358
- }
359
- })
360
-
361
- observeEvent(input$add_single, {
362
- if (!is.null(operation_mode())) {
363
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
364
- return()
365
- }
366
- operation_mode("add_single")
367
- showOrderInput(TRUE)
368
- update_log("ADD SINGLE MODE.")
369
- })
370
-
371
- observeEvent(input$add_to_all, {
372
- if (!is.null(operation_mode())) {
373
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
374
- return()
375
- }
376
- operation_mode("add_all")
377
- showOrderInput(TRUE)
378
- update_log("ADD ALL MODE.")
379
- })
380
-
381
- observeEvent(input$move_caption, {
382
- if (!is.null(operation_mode())) {
383
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
384
- return()
385
- }
386
- target_caption <- input$edit_caption
387
- if (target_caption %in% captions_data()$caption[captions_data()$image_path == input$selected_image]) {
388
- operation_mode("move_caption")
389
- showOrderInput(TRUE)
390
- update_log("MOVE CAPTION SINGLE MODE.")
391
- } else {
392
- showNotification("Error: Caption not found in the selected image.", type = "error")
393
- }
394
- })
395
-
396
- observeEvent(input$move_caption_all, {
397
- if (!is.null(operation_mode())) {
398
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
399
- return()
400
- }
401
- operation_mode("move_caption_all")
402
- showOrderInput(TRUE)
403
- update_log("MOVE CAPTION ALL MODE.")
404
- })
405
-
406
- observeEvent(input$remove_related_single, {
407
- if (!is.null(operation_mode())) {
408
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
409
- return()
410
- }
411
- if (!is.null(captions_data())) {
412
- selected_image_path <- input$selected_image
413
- target_caption <- input$edit_caption
414
-
415
- # target_captionが選択された画像に紐づいているか確認
416
- if (any(captions_data()$image_path == selected_image_path & captions_data()$caption == target_caption)) {
417
- operation_mode("remove_related_single")
418
- showTextInput(TRUE)
419
- update_log("RELATIVE REMOVE SINGLE MODE.")
420
- } else {
421
- showNotification("Error: Caption not found in the selected image.", type = "error")
422
- }
423
- }
424
- })
425
-
426
- observeEvent(input$remove_related_all, {
427
- if (!is.null(operation_mode())) {
428
- showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error")
429
- return()
430
- }
431
- operation_mode("remove_related_all")
432
- showTextInput(TRUE)
433
- update_log("RELATIVE REMOVE ALL MODE.")
434
- })
435
-
436
- observeEvent(input$confirm_order, {
437
- switch(operation_mode(),
438
- "add_single" = {
439
- # Add to Singleの処理
440
- target_caption <- input$edit_caption
441
- target_order <- max(1, floor(input$target_order))
442
- updated_data <- add_caption_at_order(captions_data(), input$selected_image, target_caption, target_order)
443
- captions_data(updated_data)
444
- update_log("Caption added to target order.")
445
- },
446
- "add_all" = {
447
- target_caption <- input$edit_caption
448
- target_order <- target_order <- max(1, floor(input$target_order))
449
- updated_data <- captions_data()
450
- unique_images <- unique(updated_data$image_path)
451
- for (image in unique_images) {
452
- updated_data <- add_caption_at_order(updated_data, image, target_caption, target_order)
453
- }
454
- captions_data(updated_data)
455
- update_log("Caption added to all images.")
456
- },
457
- "move_caption" = {
458
- # Move Captionの処理
459
- target_caption <- input$edit_caption
460
- target_order <- target_order <- max(1, floor(input$target_order))
461
- updated_data <- move_caption_order(captions_data(), input$selected_image, target_caption, target_order)
462
- captions_data(updated_data)
463
- update_log("Caption moved to target order.")
464
- },
465
- "move_caption_all" = {
466
- # Move Caption Allの処理
467
- target_caption <- input$edit_caption
468
- target_order <- target_order <- max(1, floor(input$target_order))
469
- updated_data <- captions_data()
470
- unique_images <- unique(updated_data$image_path[updated_data$caption == target_caption])
471
- for (image in unique_images) {
472
- updated_data <- move_caption_order(updated_data, image, target_caption, target_order)
473
- }
474
- captions_data(updated_data)
475
- update_log("Caption moved to target order from all images.")
476
- }
477
- )
478
-
479
- # キャプションの頻度を更新
480
- updated_caption_frequency <- get_caption_frequency(updated_data)
481
- caption_frequency(updated_caption_frequency)
482
- operation_mode(NULL)
483
- showOrderInput(FALSE)
484
- })
485
-
486
-
487
- observeEvent(input$confirm_text, {
488
- switch(operation_mode(),
489
- "remove_related_single" = {
490
- # Remove Related captions of Singleの処理
491
- related_captions <- unlist(strsplit(input$related_captions_input, ","))
492
- related_captions <- trimws(related_captions) # スペースを削除
493
- representative_caption <- input$edit_caption
494
-
495
- updated_data <- remove_related_captions_except_representative(captions_data(), related_captions, representative_caption, input$selected_image)
496
- captions_data(updated_data)
497
- update_log("Relative captions were removed from this image.")
498
- },
499
- "remove_related_all" = {
500
- # Remove Related captions from All Imagesの処理
501
- related_captions <- unlist(strsplit(input$related_captions_input, ","))
502
- related_captions <- trimws(related_captions) # スペースを削除
503
- representative_caption <- input$edit_caption
504
-
505
- updated_data <- captions_data()
506
- # representative_captionを持つすべての画像を検索
507
- images_with_rep_caption <- unique(updated_data$image_path[updated_data$caption == representative_caption])
508
-
509
- for (image_path in images_with_rep_caption) {
510
- updated_data <- remove_related_captions_except_representative(updated_data, related_captions, representative_caption, image_path)
511
- }
512
-
513
- captions_data(updated_data)
514
- update_log("Relative captions were removed from all images.")
515
- }
516
- )
517
-
518
- # キャプションの頻度を更新
519
- updated_caption_frequency <- get_caption_frequency(updated_data)
520
- caption_frequency(updated_caption_frequency)
521
- operation_mode(NULL)
522
- showTextInput(FALSE)
523
- })
524
-
525
-
526
- observeEvent(input$cancel_order, {
527
- operation_mode(NULL)
528
- showOrderInput(FALSE)
529
- update_log("Operation was canceled.")
530
- })
531
-
532
- observeEvent(input$cancel_text, {
533
- operation_mode(NULL)
534
- showTextInput(FALSE)
535
- update_log("Operation was canceled.")
536
- })
537
-
538
- output$showOrderInput <- reactive({
539
- showOrderInput()
540
- })
541
-
542
- outputOptions(output, "showOrderInput", suspendWhenHidden=FALSE)
543
-
544
- output$showTextInput <- reactive({
545
- showTextInput()
546
- })
547
-
548
- outputOptions(output, "showTextInput", suspendWhenHidden=FALSE)
549
-
550
- # Remove Related
551
-
552
- # レンダリング
553
- output$image_display <- renderImage({
554
- list(src = file.path(input$selected_image), alt = "Selected Image", width = "80%")
555
- }, deleteFile = FALSE)
556
-
557
- output$image_captions <- renderUI({
558
- if (!is.null(captions_data())) {
559
- selected_image_path <- input$selected_image
560
- captions <- filter(captions_data(), image_path == selected_image_path) %>%
561
- arrange(caption_order) %>%
562
- pull(caption)
563
- # キャプションをリンク付きのHTMLに変換
564
- linked_captions <- lapply(captions, function(caption) {
565
- sprintf("<a href='javascript:void(0);' onclick='copyToClipboard(\"%s\");'>%s</a>", caption, caption)
566
- })
567
-
568
- HTML(paste(linked_captions, collapse = ", "))
569
- } else {
570
- return(NULL)
571
- }
572
- })
573
-
574
- output$log_output <- renderText({
575
- log_text()
576
- })
577
-
578
- output$selected_image_captions <- renderTable({
579
- if (!is.null(captions_data())) {
580
- selected_image_path <- input$selected_image
581
- selected_captions <- filter(captions_data(), image_path == selected_image_path) %>%
582
- select(caption, caption_order) %>%
583
- arrange(caption_order)
584
-
585
- # caption列の各エントリにリンクを追加
586
- selected_captions$caption <- sprintf("<a href='javascript:void(0);' onclick='copyToClipboard(\"%s\");'>%s</a>", selected_captions$caption, selected_captions$caption)
587
-
588
- return(selected_captions)
589
- }
590
- }, sanitize.text.function = function(x) x) # HTMLをエスケープしないようにする
591
-
592
- output$caption_frequency_table_with_links <- renderTable({
593
- if (!is.null(caption_frequency())) {
594
- freq_data <- caption_frequency()
595
- freq_data$caption <- sprintf("<a href='javascript:void(0);' onclick='copyToClipboard(\"%s\");'>%s</a>", freq_data$caption, freq_data$caption)
596
- freq_data
597
- }
598
- }, sanitize.text.function = function(x) x, rownames = TRUE)
599
-
600
- }
601
-
602
- # アプリを実行
603
- shinyApp(ui = ui, server = server)
604
-