-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
307 lines (276 loc) · 10.7 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
library(shiny)
library(Benchmarking)
library(DT)
library(ggplot2)
function(input, output,session){
options(scipen=999)
Sys.setlocale(locale = "zh_TW.UTF8")
checkclass<-function(x){
cl<-NULL
for(i in 1:ncol(x)){
cl<-c(cl,class(x[,i]))
}
cl
}
# 資料讀取reactive----
deadata <- reactive({
inputhisfile <- input$file1
if(is.null(inputhisfile)){
return(NULL)
}else{
read.csv(inputhisfile$datapath, fileEncoding = "BIG5", header = T, stringsAsFactors = F)
}
})
# 資料前處理
XYpre <- reactive({
if(is.null(deadata())){
return(NULL)
}else{
ARData <- deadata()
ARDataColName <- colnames(ARData)[-1] # 第一行為prjcode
Xcol <- na.omit(as.numeric(strsplit(paste(input$X)," ")[[1]]))
Ycol <- na.omit(as.numeric(strsplit(paste(input$Y)," ")[[1]]))
lenX <- length(Xcol)
lenY <- length(Ycol)
LC <- c("lenX" = lenX, "lenY" = lenY)
NM <- ARDataColName
return(list(Xcol,Ycol,LC,NM))
}
})
# 資料輸入、依照變數數量展開UI
observeEvent(input$vGo,{
inFile <- input$file1
if (is.null(inFile)){
showModal(modalDialog(
"請先匯入資料",
easyClose = T
,footer = modalButton("OK")
))
}else if(any(duplicated(as.numeric(na.omit(as.numeric(strsplit(paste(input$X,input$Y)," ")[[1]])))))){
showModal(modalDialog(
"欄位重複",
easyClose = T
,footer = modalButton("OK")
))
}else if(input$X==""|input$Y==""){
showModal(modalDialog(
"變數欄位不能空白",
easyClose = T
,footer = modalButton("OK")
))
}else{
colchoosed<-setdiff(as.numeric(strsplit(paste(input$X,input$Y)," ")[[1]]),NA)
if(any(colchoosed %in% which(checkclass(deadata())=="character"))){
showModal(modalDialog(
"只能選擇數字型態欄位",
easyClose = T
,footer = modalButton("OK")
))
}else if(any(!colchoosed %in% 1:ncol(deadata()))){
showModal(modalDialog(
"輸入的欄位超出資料欄位數",
easyClose = T
,footer = modalButton("OK")
))
}else{
if(input$mode==1){
return(NULL)
}else{
# Input lower
output$Iratiolower <- renderUI({
XYpre <- XYpre()
ARDataColName <- XYpre[[4]]
LC <- XYpre[[3]]
if(LC["lenX"]==1){
return(NULL)
}else if(LC["lenX"]>=2){
lapply(1:(LC["lenX"]-1), function(i){
div(style="display:inline-block",numericInput(inputId = paste0("ILower",i),
label = paste(combn(ARDataColName,2)[1,1:(LC["lenX"]-1)],
combn(ARDataColName,2)[2,1:(LC["lenX"]-1)],sep="/")[i],value=0.5))
})
}
})
# Input upper
output$Iratioupper <- renderUI({
XYpre <- XYpre()
LC <- XYpre[[3]]
ARDataColName <- XYpre[[4]][(LC["lenX"]+1):length(XYpre[[4]])] # for output
if(LC["lenX"]==1){
return(NULL)
}else if(LC["lenX"]>=2){
lapply(1:(LC["lenX"]-1), function(i){
div(style="display:inline-block",numericInput(inputId = paste0("IUpper",i),
label = paste(combn(ARDataColName,2)[1,1:(LC["lenX"]-1)],
combn(ARDataColName,2)[2,1:(LC["lenX"]-1)],sep="/")[i],value=2))
})
}
})
# Output lower
output$Oratiolower <- renderUI({
XYpre <- XYpre()
LC <- XYpre[[3]]
ARDataColName <- XYpre[[4]][(LC["lenX"]+1):length(XYpre[[4]])] # for output
if(LC["lenY"]==1){
return(NULL)
}else if(LC["lenY"]>=2){
lapply(1:(LC["lenY"]-1), function(i){
div(style="display:inline-block",numericInput(inputId = paste0("OLower",i),
label = paste(combn(ARDataColName,2)[1,1:(LC["lenY"]-1)],
combn(ARDataColName,2)[2,1:(LC["lenY"]-1)],sep="/")[i],value=0.5))
})
}
})
combn(c(3,4),2)
# Output upper
output$Oratioupper <- renderUI({
XYpre <- XYpre()
ARDataColName <- XYpre[[4]]
LC <- XYpre[[3]]
if(LC["lenY"]==1){
return(NULL)
}else if(LC["lenY"]>=2){
lapply(1:(LC["lenY"]-1), function(i){
div(style="display:inline-block",numericInput(inputId = paste0("OUpper",i),
label = paste(combn(ARDataColName,2)[1,1:(LC["lenY"]-1)],
combn(ARDataColName,2)[2,1:(LC["lenY"]-1)],sep="/")[i],value=2))
})
}
})
}
}
}
})
# 資料檢視----
output$viewdata<-DT::renderDataTable({
if (is.null(deadata())){
return(NULL)
}else{
datatable(deadata(),
options = list(searching = F, filter = "top",
lengthMenu = list(c(-1, 20), c('20','All')),
pageLength = 20))
}
})
# 計算完畢視窗+跳轉Panel
observeEvent(input$run,{
if(!is.null(deadata()) & !is.null(XYpre)){
showModal(modalDialog(
div("專案相對效率計算完畢!", style = "font-size:200%"),
easyClose = T, footer = modalButton("OK")))
#跳轉panel ----
updateTabsetPanel(session, "tabset",selected = "result")
}else{
showModal(modalDialog(
div("請檢查資料輸入是否正確完整!", style = "font-size:200%"),
easyClose = T
,footer = modalButton("OK")
))
}
})
# DEA計算模組----
calculation <- eventReactive(input$run,{
# Reactive Data
ARData <- deadata()
# Data Preprocess
XYpre <- XYpre()
ARDataColName <- XYpre[[4]] # 第一行為prjcode(已在XYpre去除)
LC <- XYpre[[3]]
Xcol <- XYpre[[1]]
Ycol <- XYpre[[2]]
# 建立DEA AR Model
if(input$mode == 1){
CCR <- dea.dual(X=ARData[,Xcol],Y=ARData[,Ycol],RTS="crs")
CCR.Result <- cbind(ARData[,1],"相對效率" = round(CCR$eff,10), round(CCR$u,10), round(CCR$v,10))
colnames(CCR.Result) <- c("Project Code", "相對效率", ARDataColName[Xcol-1], ARDataColName[Ycol-1] )
return(CCR.Result)
}else{
# 跑CCR-AR-I,且每格都要填
# 取出input、output權重
Inum <- 1:(LC["lenX"]-1)
Onum <- 1:(LC["lenY"]-1)
# 先將權重抓出變成vector
lowerI <- 0; upperI <- 0
for(t in 1:Inum){
lowerI <- c(lowerI,input[[paste0("ILower",t)]])
upperI <- c(upperI,input[[paste0("IUpper",t)]])
}
lowerO <- 0; upperO <- 0
for(tt in 1:Onum){
lowerO <- c(lowerO,input[[paste0("ILower",tt)]])
upperO <- c(upperO,input[[paste0("IUpper",tt)]])
}
# 變成矩陣;row1是lower, row2是upper
InputWeight <- rbind(lowerI[-1], upperI[-1])
OutputWeight <- rbind(lowerO[-1], upperO[-1])
Ivector <- c()
for(h in 1:(LC["lenX"]-1)){
Ivector <- c(Ivector,InputWeight[,h])
}
Ovector <- c()
for(k in 1:(LC["lenX"]-1)){
Ovector <- c(Ovector,OutputWeight[,k])
}
# 這理的權重必須由RenderUI取得
dual.AR <- matrix(c(Ivector,Ovector), nrow=LC["lenX"]+LC["lenY"]-2, ncol=2, byrow=TRUE)
E.AR <- dea.dual(X=ARData[,Xcol],Y=ARData[,Ycol],RTS="crs", DUAL=dual.AR)
AR.Result <- cbind(ARData[,1],"相對效率" = round(E.AR$eff,10), round(E.AR$u,10), round(E.AR$v,10))
colnames(AR.Result) <- c("Project Code", "相對效率", ARDataColName[Xcol-1], ARDataColName[Ycol-1] )
return(AR.Result)
}
})
# DEA結果output----
output$Efficiency <- DT::renderDataTable({
if (is.null(calculation())){
return(NULL)
}else{
datatable(calculation(),
options = list(searching = F, filter = "top",
lengthMenu = list(c(20, -1), c('20','All')),
pageLength = 20))
}
})
# 儲存結果----
output$SaveNum <- downloadHandler(
filename = function() { paste0("DEA_Result - " ,Sys.Date(), ".csv") },
content = function(file) {
if(is.null(calculation())){
return(NULL)
}else{
write.csv(calculation(), file)
}
}
)
# 圖表呈現----
output$viewplot <- renderPlot({
if(is.null(XYpre())){
return(NULL)
}else{
# Reactive Data
ARData <- deadata()
# Data Preprocess
XYpre <- XYpre()
ARDataColName <- XYpre[[4]] # 第一行為prjcode(已在XYpre去除)
LC <- XYpre[[3]]
Xcol <- XYpre[[1]]
Ycol <- XYpre[[2]]
weight <- apply(as.matrix(calculation()),2,as.numeric)
# 矩陣相乘並抓對角線為分數
Xm <- apply(as.matrix(ARData[,Xcol]),2,as.numeric)
Ym <- apply(as.matrix(ARData[,Ycol]),2,as.numeric)
XMatrix <- Xm %*% t(weight[,(1:LC["lenX"])+2])
YMatrix <- Ym %*% t(weight[,((3+LC["lenX"]):ncol(weight))])
for(u in 1:nrow(ARData)){
ARData$XAxis[u] <- XMatrix[u,u]
ARData$YAxis[u] <- YMatrix[u,u]
}
fmt_dcimals <- function(decimals=0){
# return a function responpsible for formatting the
# axis labels with a given number of decimals
function(x) as.character(round(x,decimals))
}
ggplot(ARData, aes(x = XAxis, y = YAxis)) + geom_point() + geom_text(aes(label=ARData[,1]),hjust=-0.5, vjust=-0.5) +
scale_y_continuous(labels=fmt_dcimals(10))
}
})
}