-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
154 lines (118 loc) · 4.23 KB
/
server.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
library(shiny)
library(shinyBS)
library(shinyjs)
library(shinydashboard)
library(xlsx)
#library(openxlsx)
shinyServer(function(input, output, session) {
options(shiny.maxRequestSize = 100*1024^2)
downloadInputs = c("downloadFile", "downloadCSV", "downloadImages")
for (name in downloadInputs) {
shinyjs::disable(name)
}
tempDirectory = paste0(tempdir(), "/", format(Sys.time(), "%Y%m%d%H%M%S"), sample(0:100, 1))
shinyjs::addClass("uploadBox", "opacity")
shinyjs::addClass("downloadBox", "opacity")
observe({
if (input$formType != "Please Select a Form Type") {
shinyjs::addClass("formTypeBox", "opacity")
shinyjs::removeClass("uploadBox", "opacity")
}
})
shinyjs::hide("processResult")
output$processResult = renderUI({
helpText(icon("spinner", "fa-spin"), "Scanning forms...")
})
formType = function() {
switch(input$formType,
"hybrid" = return("/home/shiny/formscanner/Hybrid_v3.2.xtmpl"),
"numeric" = return("/home/shiny/formscanner/Numeric_v3.2.xtmpl"),
"multi" = return("/home/shiny/formscanner/MC6_75_v3.2.xtmpl"))
}
excelType = function() {
switch(input$formType,
"hybrid" = return("ResultsHybridv3.2.xlsm"),
"numeric" = return("ResultsNumericv3.2.xlsm"),
"multi" = return("ResultsMCv3.2.xlsm"))
}
# TODO: Change indicator depending on step of process.
observeEvent(input$fileUpload, {
#print(formType())
shinyjs::show("processResult")
# rv$fileState = "uploaded"
if(dir.exists(tempDirectory)) {
shinyjs::disable("downloadFile")
unlink(tempDirectory, recursive = TRUE)
}
print(tempDirectory)
inputFile = input$fileUpload
dir.create(tempDirectory)
setwd(tempDirectory)
dir.create("images")
if (tools::file_ext(input$fileUpload$name) == "pdf") {
file.copy(inputFile$datapath, "forms.pdf")
system("nconvert -xall -dpi 300 -out jpeg -o 'images/%.jpeg' forms.pdf")
} else {
unzip(inputFile$datapath, exdir = "images")
}
system(paste("/bin/bash formscanner", formType(), "images"))
system("mv images/*.csv result.tmp")
system("java -jar /home/shiny/formscanner/ProcessCSV2.jar result.tmp results.csv")
system(paste0("cp /home/shiny/formscanner/", excelType(), " ."))
data = read.csv("results.csv")
excelForm = loadWorkbook(excelType())
sheets = getSheets(excelForm)
addDataFrame(data, sheets[[1]], col.names = FALSE, row.names = FALSE, startRow=2, colStyle = NULL)
saveWorkbook(excelForm, "results.xlsm")
# data = read.csv("results.csv")
# excelForm = loadWorkbook(excelType())
# writeData(excelForm, "Scan", data, colNames = FALSE, rowNames = FALSE, startRow = 2)
# saveWorkbook(excelForm, "results.xlsm")
# output$processResult = renderUI({
# helpText(icon("check"), "Scanning Done!")
# })
# rv$fileState = "processed"
output$processResult = renderUI({
helpText(icon("check"), "Scanning Done!")
})
shinyjs::show("processResult")
for (name in downloadInputs) {
shinyjs::enable(name)
}
shinyjs::addClass("uploadBox", "opacity")
shinyjs::removeClass("downloadBox", "opacity")
# shinyjs::show("downloadBox", anim = TRUE)
# shinyjs::hide("uploadBox", anim = TRUE)
})
observeEvent(input$reset, {
shinyjs::runjs("location.reload(true);")
})
output$downloadFile = downloadHandler(
filename = function() {
paste0(tools::file_path_sans_ext(input$fileUpload$name), ".xlsm")
},
content = function(file) {
file.copy("results.xlsm", file)
}
)
output$downloadCSV = downloadHandler(
filename = function() {
paste0(tools::file_path_sans_ext(input$fileUpload$name), ".csv")
},
content = function(file) {
file.copy("results.csv", file)
}
)
output$downloadImages = downloadHandler(
filename = function() {
paste0(tools::file_path_sans_ext(input$fileUpload$name), ".zip")
},
content = function(file) {
system("zip files.zip images/*.jpeg")
file.copy("files.zip", file)
}
)
session$onSessionEnded(function() {
unlink(tempDirectory, recursive = TRUE)
})
})