|
rm(list=ls()) |
|
|
|
|
|
library(shiny) |
|
|
|
|
|
source("../script.R") |
|
|
|
|
|
orderInputPanel <- function() { |
|
conditionalPanel( |
|
condition = "output.showOrderInput", |
|
numericInput("target_order", "Target Order:", value = 1, min = 1), |
|
actionButton("confirm_order", "Confirm Order"), |
|
actionButton("cancel_order", "Cancel") |
|
) |
|
} |
|
|
|
textInputPanel <- function() { |
|
conditionalPanel( |
|
condition = "output.showTextInput", |
|
textInput("related_captions_input", "Enter related captions (comma separated):"), |
|
actionButton("confirm_text", "Confirm Order"), |
|
actionButton("cancel_text", "Cancel") |
|
) |
|
} |
|
|
|
|
|
ui <- fluidPage( |
|
|
|
titlePanel("SDtagEditoR"), |
|
|
|
uiOutput("notDRmode"), |
|
|
|
|
|
tags$style(type = "text/css", |
|
"#image_captions { white-space: pre-wrap; }", |
|
".shiny-action-button { margin-bottom: 50px; }" |
|
), |
|
|
|
|
|
tags$head(tags$script(HTML(" |
|
function copyToClipboard(text) { |
|
var dummy = document.createElement('textarea'); |
|
document.body.appendChild(dummy); |
|
dummy.value = text; |
|
dummy.select(); |
|
document.execCommand('copy'); |
|
document.body.removeChild(dummy); |
|
} |
|
"))), |
|
|
|
tags$script(HTML(" |
|
function handleCaptionClick(caption, source) { |
|
var operationModeElement = document.getElementById('notDRmode'); |
|
if (operationModeElement && operationModeElement.innerText === 'FALSE') { |
|
if (source === 'frequency') { |
|
Shiny.setInputValue('clicked_caption_frequency', caption, {priority: 'event'}); |
|
} else { |
|
Shiny.setInputValue('clicked_caption', caption, {priority: 'event'}); |
|
} |
|
} else { |
|
copyToClipboard(caption); |
|
} |
|
} |
|
")), |
|
|
|
|
|
|
|
sidebarLayout( |
|
sidebarPanel( |
|
|
|
textInput("directory_path", "Enter the directory path:", value = ""), |
|
|
|
|
|
actionButton("load_data", "Load Captions Data"), |
|
tags$br(), |
|
tags$br(), |
|
|
|
conditionalPanel( |
|
condition = "output.dataLoaded", |
|
|
|
|
|
selectInput("selected_image", "Choose an image:", choices = NULL), |
|
|
|
|
|
actionButton("prev_image", "Prev"), |
|
actionButton("next_image", "Next"), |
|
tags$br(), |
|
tags$br(), |
|
|
|
|
|
actionButton("direct_remover", "Direct Remover"), |
|
conditionalPanel( |
|
condition = "output.notDRmode === 'FALSE'", |
|
actionButton("exit_direct_remover", "Exit Direct Remover") |
|
), |
|
tags$br(), |
|
tags$br(), |
|
conditionalPanel( |
|
condition = "output.notDRmode === 'TRUE'", |
|
|
|
|
|
actionButton("shuffle_single", "Shuffle Order for Current Image"), |
|
actionButton("shuffle_all", "Shuffle Order for All Images"), |
|
tags$br(), |
|
tags$br(), |
|
numericInput("frequency_threshold", "Frequency Threshold:", value = 5, min = 1), |
|
actionButton("remove_low_freq", "Remove Low Frequency Captions"), |
|
tags$br(), |
|
tags$br(), |
|
textInput("edit_caption", "Caption to Edit:", ""), |
|
actionButton("remove_single", "Remove from Single"), |
|
actionButton("remove_from_all", "Remove from All"), |
|
|
|
tags$br(), |
|
actionButton("add_single", "Add to Single"), |
|
actionButton("add_to_all", "Add to All"), |
|
tags$br(), |
|
actionButton("move_caption", "Move Caption"), |
|
actionButton("move_caption_all", "Move Caption of All"), |
|
tags$br(), |
|
actionButton("remove_related_single", "Remove Related from Single"), |
|
actionButton("remove_related_all", "Remove Related from all"), |
|
orderInputPanel(), |
|
textInputPanel(), |
|
tags$br(), |
|
), |
|
tags$br(), |
|
tags$br(), |
|
actionButton("output_captions", "Output Edited Captions") |
|
) |
|
), |
|
|
|
mainPanel( |
|
fluidRow( |
|
column(6, |
|
imageOutput("image_display", width = "100%", height = "50%"), |
|
tags$br(), |
|
uiOutput("image_captions") |
|
), |
|
column(6, |
|
verbatimTextOutput("log_output"), |
|
fluidRow( |
|
column(6, tableOutput("selected_image_captions")), |
|
column(6, tableOutput("caption_frequency_table_with_links")) |
|
) |
|
) |
|
) |
|
) |
|
) |
|
) |
|
|
|
|
|
|
|
server <- function(input, output, session) { |
|
|
|
directory_path <- reactiveVal() |
|
captions_data <- reactiveVal() |
|
caption_frequency <- reactiveVal() |
|
operation_mode <- reactiveVal("normal") |
|
log_text <- reactiveVal("Log:\n") |
|
|
|
|
|
add_log <- function(message) { |
|
current_log <- log_text() |
|
new_log <- paste(current_log, message, "\n") |
|
log_text(new_log) |
|
} |
|
|
|
|
|
update_log <- function(message) { |
|
log_text(message) |
|
} |
|
|
|
|
|
output$dataLoaded <- reactive({ |
|
!is.null(captions_data()) |
|
}) |
|
outputOptions(output, "dataLoaded", suspendWhenHidden=FALSE) |
|
|
|
|
|
output$notDRmode <- renderText({ |
|
if (operation_mode() != "direct remove") { |
|
return("TRUE") |
|
} else { |
|
return("FALSE") |
|
} |
|
}) |
|
outputOptions(output, "notDRmode", suspendWhenHidden=FALSE) |
|
|
|
|
|
observeEvent(input$load_data, { |
|
update_log("Loading captions data...") |
|
|
|
|
|
temp_directory_path <- isolate(input$directory_path) |
|
temp_captions_data <- read_captions_from_directory(temp_directory_path) |
|
temp_caption_frequency <- get_caption_frequency(temp_captions_data) |
|
|
|
|
|
directory_path(temp_directory_path) |
|
captions_data(temp_captions_data) |
|
caption_frequency(temp_caption_frequency) |
|
|
|
|
|
unique_images <- unique(captions_data()$image_path) |
|
updateSelectInput(session, "selected_image", choices = unique_images, selected = unique_images[1]) |
|
|
|
update_log("Captions data loaded.") |
|
}) |
|
|
|
|
|
observeEvent(input$prev_image, { |
|
current_index <- which(unique(captions_data()$image_path) == input$selected_image) |
|
if (current_index > 1) { |
|
updateSelectInput(session, "selected_image", selected = unique(captions_data()$image_path)[current_index - 1]) |
|
} |
|
}) |
|
|
|
observeEvent(input$next_image, { |
|
current_index <- which(unique(captions_data()$image_path) == input$selected_image) |
|
if (current_index < length(unique(captions_data()$image_path))) { |
|
updateSelectInput(session, "selected_image", selected = unique(captions_data()$image_path)[current_index + 1]) |
|
} |
|
}) |
|
|
|
|
|
observeEvent(input$direct_remover, { |
|
operation_mode("direct remove") |
|
update_log("DIRECT REMOVER MODE.") |
|
}) |
|
|
|
observeEvent(input$exit_direct_remover, { |
|
operation_mode("normal") |
|
update_log("Exited Direct Remover mode.") |
|
}) |
|
|
|
|
|
observeEvent(input$shuffle_single, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
|
|
current_image <- input$selected_image |
|
updated_data <- captions_data() |
|
captions_for_current_image <- filter(updated_data, image_path == current_image) |
|
|
|
|
|
new_order <- sample(1:nrow(captions_for_current_image), nrow(captions_for_current_image)) |
|
captions_for_current_image$caption_order <- new_order |
|
|
|
|
|
updated_data[updated_data$image_path == current_image, ] <- captions_for_current_image |
|
captions_data(updated_data) |
|
|
|
update_log("captions of this image was shauffled.") |
|
}) |
|
|
|
|
|
observeEvent(input$shuffle_all, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
|
|
updated_data <- captions_data() |
|
unique_images <- unique(updated_data$image_path) |
|
|
|
for (image in unique_images) { |
|
captions_for_image <- filter(updated_data, image_path == image) |
|
new_order <- sample(1:nrow(captions_for_image), nrow(captions_for_image)) |
|
captions_for_image$caption_order <- new_order |
|
updated_data[updated_data$image_path == image, ] <- captions_for_image |
|
} |
|
|
|
captions_data(updated_data) |
|
update_log("captions of all images was shauffled.") |
|
}) |
|
|
|
|
|
observeEvent(input$remove_low_freq, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
|
|
if (!is.null(captions_data())) { |
|
threshold <- input$frequency_threshold |
|
updated_data <- remove_low_frequency_captions(captions_data(), threshold) |
|
captions_data(updated_data) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
} |
|
|
|
update_log("Low frequency captions removed.") |
|
}) |
|
|
|
|
|
observeEvent(input$remove_from_all, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
|
|
|
|
target_caption <- input$edit_caption |
|
updated_data <- captions_data() |
|
unique_images <- unique(updated_data$image_path[updated_data$caption == target_caption]) |
|
for (image in unique_images) { |
|
updated_data <- remove_caption_and_adjust_order(updated_data, image, target_caption) |
|
} |
|
captions_data(updated_data) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
|
|
update_log("Caption removed from all images.") |
|
}) |
|
|
|
|
|
showOrderInput <- reactiveVal(FALSE) |
|
showTextInput <- reactiveVal(FALSE) |
|
|
|
observeEvent(input$remove_single, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
target_caption <- input$edit_caption |
|
if (target_caption %in% captions_data()$caption[captions_data()$image_path == input$selected_image]) { |
|
updated_data <- remove_caption_and_adjust_order(captions_data(), input$selected_image, target_caption) |
|
captions_data(updated_data) |
|
update_log("Caption removed from this image.") |
|
} else { |
|
showNotification("Error: Caption not found in the selected image.", type = "error") |
|
} |
|
}) |
|
|
|
observeEvent(input$add_single, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
operation_mode("add_single") |
|
showOrderInput(TRUE) |
|
update_log("ADD SINGLE MODE.") |
|
}) |
|
|
|
observeEvent(input$add_to_all, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
operation_mode("add_all") |
|
showOrderInput(TRUE) |
|
update_log("ADD ALL MODE.") |
|
}) |
|
|
|
observeEvent(input$move_caption, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
target_caption <- input$edit_caption |
|
if (target_caption %in% captions_data()$caption[captions_data()$image_path == input$selected_image]) { |
|
operation_mode("move_caption") |
|
showOrderInput(TRUE) |
|
update_log("MOVE CAPTION SINGLE MODE.") |
|
} else { |
|
showNotification("Error: Caption not found in the selected image.", type = "error") |
|
} |
|
}) |
|
|
|
observeEvent(input$move_caption_all, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
operation_mode("move_caption_all") |
|
showOrderInput(TRUE) |
|
update_log("MOVE CAPTION ALL MODE.") |
|
}) |
|
|
|
observeEvent(input$remove_related_single, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
if (!is.null(captions_data())) { |
|
selected_image_path <- input$selected_image |
|
target_caption <- input$edit_caption |
|
|
|
|
|
if (any(captions_data()$image_path == selected_image_path & captions_data()$caption == target_caption)) { |
|
operation_mode("remove_related_single") |
|
showTextInput(TRUE) |
|
update_log("RELATIVE REMOVE SINGLE MODE.") |
|
} else { |
|
showNotification("Error: Caption not found in the selected image.", type = "error") |
|
} |
|
} |
|
}) |
|
|
|
observeEvent(input$remove_related_all, { |
|
if (operation_mode() != "normal") { |
|
showNotification("Another operation is in progress. Please finish or cancel it first.", type = "error") |
|
return() |
|
} |
|
operation_mode("remove_related_all") |
|
showTextInput(TRUE) |
|
update_log("RELATIVE REMOVE ALL MODE.") |
|
}) |
|
|
|
observeEvent(input$confirm_order, { |
|
switch(operation_mode(), |
|
"add_single" = { |
|
|
|
target_caption <- input$edit_caption |
|
target_order <- max(1, floor(input$target_order)) |
|
updated_data <- add_caption_at_order(captions_data(), input$selected_image, target_caption, target_order) |
|
captions_data(updated_data) |
|
update_log("Caption added to target order.") |
|
}, |
|
"add_all" = { |
|
target_caption <- input$edit_caption |
|
target_order <- target_order <- max(1, floor(input$target_order)) |
|
updated_data <- captions_data() |
|
unique_images <- unique(updated_data$image_path) |
|
for (image in unique_images) { |
|
updated_data <- add_caption_at_order(updated_data, image, target_caption, target_order) |
|
} |
|
captions_data(updated_data) |
|
update_log("Caption added to all images.") |
|
}, |
|
"move_caption" = { |
|
|
|
target_caption <- input$edit_caption |
|
target_order <- target_order <- max(1, floor(input$target_order)) |
|
updated_data <- move_caption_order(captions_data(), input$selected_image, target_caption, target_order) |
|
captions_data(updated_data) |
|
update_log("Caption moved to target order.") |
|
}, |
|
"move_caption_all" = { |
|
|
|
target_caption <- input$edit_caption |
|
target_order <- target_order <- max(1, floor(input$target_order)) |
|
updated_data <- captions_data() |
|
unique_images <- unique(updated_data$image_path[updated_data$caption == target_caption]) |
|
for (image in unique_images) { |
|
updated_data <- move_caption_order(updated_data, image, target_caption, target_order) |
|
} |
|
captions_data(updated_data) |
|
update_log("Caption moved to target order from all images.") |
|
} |
|
) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
operation_mode("normal") |
|
showOrderInput(FALSE) |
|
}) |
|
|
|
|
|
observeEvent(input$confirm_text, { |
|
switch(operation_mode(), |
|
"remove_related_single" = { |
|
|
|
related_captions <- unlist(strsplit(input$related_captions_input, ",")) |
|
related_captions <- trimws(related_captions) |
|
representative_caption <- input$edit_caption |
|
|
|
updated_data <- remove_related_captions_except_representative(captions_data(), related_captions, representative_caption, input$selected_image) |
|
captions_data(updated_data) |
|
update_log("Relative captions were removed from this image.") |
|
}, |
|
"remove_related_all" = { |
|
|
|
related_captions <- unlist(strsplit(input$related_captions_input, ",")) |
|
related_captions <- trimws(related_captions) |
|
representative_caption <- input$edit_caption |
|
|
|
updated_data <- captions_data() |
|
|
|
images_with_rep_caption <- unique(updated_data$image_path[updated_data$caption == representative_caption]) |
|
|
|
for (image_path in images_with_rep_caption) { |
|
updated_data <- remove_related_captions_except_representative(updated_data, related_captions, representative_caption, image_path) |
|
} |
|
|
|
captions_data(updated_data) |
|
update_log("Relative captions were removed from all images.") |
|
} |
|
) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
operation_mode("normal") |
|
showTextInput(FALSE) |
|
}) |
|
|
|
|
|
observeEvent(input$cancel_order, { |
|
operation_mode("normal") |
|
showOrderInput(FALSE) |
|
update_log("Operation was canceled.") |
|
}) |
|
|
|
observeEvent(input$cancel_text, { |
|
operation_mode("normal") |
|
showTextInput(FALSE) |
|
update_log("Operation was canceled.") |
|
}) |
|
|
|
output$showOrderInput <- reactive({ |
|
showOrderInput() |
|
}) |
|
|
|
outputOptions(output, "showOrderInput", suspendWhenHidden=FALSE) |
|
|
|
output$showTextInput <- reactive({ |
|
showTextInput() |
|
}) |
|
|
|
outputOptions(output, "showTextInput", suspendWhenHidden=FALSE) |
|
|
|
|
|
observeEvent(input$clicked_caption, { |
|
target_caption <- input$clicked_caption |
|
|
|
|
|
updated_data <- remove_caption_and_adjust_order(captions_data(), input$selected_image, target_caption) |
|
captions_data(updated_data) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
|
|
update_log("Caption removed from this image.") |
|
}) |
|
|
|
observeEvent(input$clicked_caption_frequency, { |
|
target_caption <- input$clicked_caption_frequency |
|
|
|
|
|
updated_data <- captions_data() |
|
unique_images <- unique(updated_data$image_path[updated_data$caption == target_caption]) |
|
for (image in unique_images) { |
|
updated_data <- remove_caption_and_adjust_order(updated_data, image, target_caption) |
|
} |
|
captions_data(updated_data) |
|
|
|
|
|
updated_caption_frequency <- get_caption_frequency(updated_data) |
|
caption_frequency(updated_caption_frequency) |
|
|
|
update_log("Caption removed from all images.") |
|
}) |
|
|
|
|
|
|
|
observeEvent(input$output_captions, { |
|
|
|
if (!dir.exists("./output")) { |
|
dir.create("./output") |
|
} |
|
|
|
|
|
unique_images <- unique(captions_data()$image_path) |
|
for (image_path in unique_images) { |
|
|
|
image_name <- basename(image_path) |
|
output_filename <- paste0("./output/", tools::file_path_sans_ext(image_name), ".txt") |
|
|
|
|
|
csv_captions <- capture.output(print_image_captions_as_csv(captions_data(), image_path)) |
|
|
|
|
|
writeLines(csv_captions, con = output_filename) |
|
} |
|
|
|
|
|
update_log("Captions data written to ./output/ directory.") |
|
}) |
|
|
|
|
|
output$image_display <- renderImage({ |
|
list(src = file.path(input$selected_image), alt = "Selected Image", width = "80%") |
|
}, deleteFile = FALSE) |
|
|
|
output$image_captions <- renderUI({ |
|
if (!is.null(captions_data())) { |
|
selected_image_path <- input$selected_image |
|
captions <- filter(captions_data(), image_path == selected_image_path) %>% |
|
arrange(caption_order) %>% |
|
pull(caption) |
|
|
|
|
|
linked_captions <- lapply(captions, function(caption) { |
|
sprintf("<a href='javascript:void(0);' onclick='handleCaptionClick(\"%s\", \"image\");'>%s</a>", caption, caption) |
|
}) |
|
|
|
HTML(paste(linked_captions, collapse = ", ")) |
|
} |
|
}) |
|
|
|
output$log_output <- renderText({ |
|
log_text() |
|
}) |
|
|
|
output$selected_image_captions <- renderTable({ |
|
if (!is.null(captions_data())) { |
|
selected_image_path <- input$selected_image |
|
selected_captions <- filter(captions_data(), image_path == selected_image_path) %>% |
|
select(caption, caption_order) %>% |
|
arrange(caption_order) |
|
|
|
|
|
selected_captions$caption <- sprintf("<a href='javascript:void(0);' onclick='handleCaptionClick(\"%s\", \"image\");'>%s</a>", selected_captions$caption, selected_captions$caption) |
|
|
|
return(selected_captions) |
|
} |
|
}, sanitize.text.function = function(x) x) |
|
|
|
output$caption_frequency_table_with_links <- renderTable({ |
|
if (!is.null(caption_frequency())) { |
|
freq_data <- caption_frequency() |
|
freq_data$caption <- sprintf("<a href='javascript:void(0);' onclick='handleCaptionClick(\"%s\", \"frequency\");'>%s</a>", freq_data$caption, freq_data$caption) |
|
freq_data |
|
} |
|
}, sanitize.text.function = function(x) x, rownames = TRUE) |
|
|
|
} |
|
|
|
|
|
shinyApp(ui = ui, server = server) |
|
|
|
|