-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
221 lines (165 loc) · 5.61 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
# closeAllConnections()
# rm(list=ls())
library(shiny)
# setwd("C:/Users/Garrett/Desktop/Shiny/Date_Utility")
source("jd2mjd.R")
source("mjd2jd.R")
source("jd2yr2.R")
source("yr2jd.R")
source("jd2doy.R")
source("doy2jd.R")
source("jd2dow.R")
source("gps2jd.R")
source("jd2cal.R")
source("jd2gps.R")
source("getJulienDate.R")
source("convertAllSystems.R")
source("formatStringBasedOnTimeSystem.R")
validateDate <- function(timeSystem,outputString) {
if (outputString == "") {
errorMessage="Please input date"
return(errorMessage)
}
if (timeSystem==1){
x=strsplit(outputString, split = " ")
gpsWeek = as.numeric(unlist(x)[1])
DOW = as.numeric(unlist(x)[2])
if(is.na(DOW)){
errorMessage="Invalid DOW required"
return(errorMessage)
}
if(gpsWeek<=0){
errorMessage="GPS week"
return(errorMessage)
}
else {
NULL
}
}
if (timeSystem==2){
x=strsplit(outputString, split = " ")
year = as.numeric(unlist(x)[1])
DOY = as.numeric(unlist(x)[2])
if(is.na(DOY)){
errorMessage="Invalid DOY required"
return(errorMessage)
}
else {
NULL
}
}
if ((timeSystem==3)&&((outputString != ""))){
mjd = outputString
jd = mjd2jd(as.numeric(mjd))
if(is.na(mjd)){
errorMessage="Invalid decimal year format N/A"
return(errorMessage)
}
else {
NULL
}
}
if ((timeSystem==4)&&((outputString != ""))){
decimalYear = (as.numeric(outputString))
if((nchar(as.character(outputString))<4)){
errorMessage="Invalid decimal year format"
return(errorMessage)
}
if(is.na(decimalYear)){
errorMessage="Invalid decimal year format N/A"
return(errorMessage)
}
if (decimalYear < 0){
errorMessage="Invalid year"
return(errorMessage)
}
else {
NULL
}
}
if ((timeSystem==5)&&((outputString != ""))){
x=strsplit(outputString, split = "/")
year = as.numeric(unlist(x)[1])
month = as.numeric(unlist(x)[2])
day = as.numeric(unlist(x)[3])
# print(as.character(outputString))
# print(nchar(as.character(outputString)))
# print(day)
if((nchar(as.character(outputString))<8)){
errorMessage="Invalid date format"
return(errorMessage)
}
if(is.na(day)){
errorMessage="Invalid date format"
return(errorMessage)
}
if (((day) < 1)||(month == 2 && day > 29)||(month == 3 && day > 30)||(month == 5 && day > 30)||(month == 9 && day > 30)||(month == 11 && day > 30) ){
errorMessage="Invalid day of the month"
return(errorMessage)
}
if((month<1)||(month>12)){
errorMessage="Invalid month"
return(errorMessage)
}
if((year<1800)){
errorMessage="Invalid year"
return(errorMessage)
}
else {
NULL
}
}
}
shinyServer(function(input, output, clientData, session) {
# Load system time as default data
yearSystemTime = format(Sys.Date(), format="%Y")
monthSystemTime = format(Sys.Date(), format="%m")
daySystemTime = format(Sys.Date(), format="%d")
# Convert to julien date
jd = cal2jd(as.numeric(yearSystemTime),as.numeric(monthSystemTime),as.numeric(daySystemTime));
# Convert julien date to all other systems
allSystemTimes = convertAllSystems(jd)
#Observes changes to timeSystem and updates input box value
observe({
timeSystem<-input$timeSystem
outputString=formatStringBasedOnTimeSystem(timeSystem,allSystemTimes)
# Change values for input$inSelect
updateSelectInput(session, "intputTextBased",
selected = paste0(outputString)
)
}) #observe end
# Reactive function ???
dataFormReactiveFunction<-reactive({
#input date
outputString=toString(input$intputTextBased)
timeSystem<-input$timeSystem
#validation of input data
validate(
validateDate(timeSystem,input$intputTextBased)
)
jd=getJulienDate(timeSystem,outputString)
allSystemTimes = convertAllSystems(jd)
#Calculate date parameters
calculatedGPSweek = as.numeric(allSystemTimes[[1]])
calculatedDOW = as.numeric(allSystemTimes[[2]])
calculatedYear = as.numeric(allSystemTimes[[3]])
calculatedDOY = as.numeric(allSystemTimes[[4]])
calculatedMJD = as.numeric(allSystemTimes[[5]])
calculatedDecimalYear = as.numeric(allSystemTimes[[6]])
calculatedYYYYMMDD = as.numeric(allSystemTimes[[7]])
# Create data frame
labelGPSweekDOW = paste(calculatedGPSweek, " ", calculatedDOW)
labelYearDOY = paste(calculatedYear, " ", calculatedDOY)
labelMJD = paste(calculatedMJD)
labelDecimalYear= paste(round(calculatedDecimalYear*10000)/10000)
labelYYYYMMDD = paste(calculatedYYYYMMDD[1], "/", calculatedYYYYMMDD[2], "/", calculatedYYYYMMDD[3])
dataFrameString = c("GPS week, day of week", "Year, Day of Year", "Modified Julian Day","Decimal Year","YYYY-MM-DD")
dataFrameNumbers= c(labelGPSweekDOW, labelYearDOY, labelMJD, labelDecimalYear, labelYYYYMMDD)
dataFrameTime = data.frame(dataFrameString, dataFrameNumbers)
colnames(dataFrameTime) <- c("Time System", "Output")
# Output data frame
return(dataFrameTime)
})
output$table <- renderDataTable(dataFormReactiveFunction(),options = list(pageLength = 5,paging = FALSE,searching = FALSE,info=0))
# https://datatables.net/upgrade/1.10-convert
})