-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
138 lines (110 loc) · 3.67 KB
/
app.R
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
library(shiny)
library(shinyjs)
library(shinyWidgets)
# functions
source("lib.R")
# Define UI
ui <- fluidPage(
titlePanel("Image Processing App"),
sidebarLayout(
sidebarPanel(
textInput("remove_pattern", label = "What pattern should be removed?",
value = "(\\s\\(Klein\\))"),
textInput("name_pattern",
label = "What pattern is the same between all files?",
value = "P\\d{4}(.+)."),
textInput("file_extension", label = "What file type are you using?",
value = ".JPG"),
fileInput("zipFile", "Choose a zip file with images", accept = c(".zip")),
actionButton("processBtn", "Process Zip File"),
useShinyjs(),
div(id="dwnbutton",
downloadButton("downloadData", "Download",
onclick = "Shiny.setInputValue('dwnClicked',
true, {priority:'event'});")
),
),
mainPanel(
includeMarkdown("README.md"),
textOutput("result"),
)
)
)
# Define server
server <- function(input, output, session) {
file.remove("www/app.pdf")
unlink("workingdir/*")
unlink("workingdir/figures")
rv <- reactiveValues(messages = NULL,
output_file = NULL)
# Function to process the uploaded zip file
processZipFile <- function(zipFilePath) {
dir.create("www")
# Unzip the contents of the zip file into the temporary directory
files <- unzip(zipFilePath, exdir = "workingdir")
# Print the list of extracted files for debugging
print("files are")
print(files)
outfile <- make_pdf("workingdir", output_dir = "./www/",
extension = input$file_extension,
name_pattern = input$name_pattern,
pattern_remove = input$remove_pattern)
# Clean up: remove the temporary directory and its contents
unlink(zipFilePath, recursive = TRUE)
rv$output_file <- outfile
result <- outfile
# Print messages directly to verbatimTextOutput
message(paste("Process completed. Result:", rv$output_file, "\n"))
return(result)
}
# Event handler for the "Process Zip File" button
observeEvent(input$processBtn, {
req(input$zipFile)
# Process the zip file and store the result
result <- processZipFile(input$zipFile$datapath)
# Display the result (you can customize this based on your actual output)
output$result <- renderText({
paste("COMPLETED: Processed result:", result)
})
})
output$message <- renderPrint({
# Print messages from the reactiveValues
isolate(rv$messages)
})
# Event handler for the "Download PDF" button
observeEvent(input$downloadBtn, {
# Generate PDF and save it (replace this with your actual PDF generation code)
# Trigger download of the generated PDF
downloadHandler(
filename = function() {
"output.pdf"
},
content = function(file) {
message(paste("the output exist ",
file.exists("www/app.pdf")))
file.copy("www/app.pdf", file)
}
)
})
output$downloadData <- downloadHandler(
filename = "app.pdf",
content = function(file) {
file.copy("www/app.pdf", file)
}
)
observeEvent(input[["dwnClicked"]], {
if(file.exists("www/app.pdf")){
message("the file exist, allowing download!")
}else{
sendSweetAlert(
session = session,
title = "No data !",
text = "No data available \n Upload data first,
and press the process zip file button!",
type = "error"
)
}
})
}
# Run the Shiny app
shinyApp(ui, server)