-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
231 lines (187 loc) · 7.42 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
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
library(shiny)
library(DT)
library(openxlsx)
library(tidyverse)
library(lubridate)
library(shiny.i18n)
i18n <- Translator$new(translation_csvs_path = "translation")
i18n$set_translation_language("en")
#validentries<-read.xlsx("dot100_variableLists.xlsx",1)
#write.csv(validentries,"validentries.csv",row.names = FALSE)
validentries<-read.csv("validentries.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel(i18n$t("Create DayCent Scheduler File From Input"), windowTitle = NULL),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
htmlOutput("downloader"),
uiOutput("selections"),
uiOutput("headerin"),
uiOutput("fn"),
uiOutput("dl")
),
# Show a plot of the generated distribution
mainPanel(
shiny.i18n::usei18n(i18n),
div(style = "float: right;",
selectInput('selected_language',
i18n$t("Language"),
choices = i18n$get_languages(),
selected = i18n$get_key_translation())
),
tabsetPanel(
tabPanel(i18n$t("View Schedule"),
htmlOutput("warning"),
htmlOutput("schedule")),
tabPanel(i18n$t("View Inputs"),
DTOutput("data1"))
)
)
)
)
#file1<-"C:/Users/sdumb/Documents/scheduleFile_template_SV2_test.xlsx"
# Define server logic required to draw a histogram
server <- function(input, output, session) {
output$selections <- renderUI({
fileInput("file1", i18n$t("Upload Completed Excel Schedule Template"),
multiple = FALSE,
accept = c(".xlsx"))
})
output$headerin <- renderUI({
checkboxInput("header",i18n$t("Include header/footer in schedule?"),value = TRUE)
})
observeEvent(input$selected_language, {
# Here is where we update language in session
shiny.i18n::update_lang(input$selected_language,session )
})
output$downloader<-renderText(
HTML(paste(i18n$t('Download Blank Excel Template File:'),
'<a href="https://github.com/stats4sd/DayCentSchedule/raw/main/scheduleFile_template.xlsx">',
i18n$t("Here"),'</a><br><br>'))
)
observeEvent(input$file1,{
nms<-getSheetNames(input$file1$datapath)
nms<-nms[nms!="lookups"]
schedule<-NULL
for(i in nms){
tmp<-read.xlsx(input$file1$datapath,i)
if(colnames(tmp)[1]=="fecha"){
schedule<-rbind(tmp,schedule)
}
}
lookups<-read.xlsx(input$file1$datapath,"lookups")
lookups$code<-toupper(lookups$code)
output$warning<-renderText({
fails<-anti_join(lookups,validentries)
if(nrow(fails)==0){
message<-""
}
else{
message<-paste("<p style='color:red;'>",
i18n$t("WARNING"),":",
nrow(fails),
i18n$t("code(s) specified in lookup list that were not found in the global dictionary"),".",
"<br>",
i18n$t("These are:"),"<br>",paste(fails$code,collapse="<br>"),"<br><br>",
i18n$t("Schedule file has been created with these codes still included. Please check codes, and correct them if needed."),
"<br></p>")
}
message
})
out1<-schedule %>%
arrange(fecha) %>%
mutate(siembra=ifelse(is.na(cultivo),
NA,"FRST"),
date=as.Date(fecha,origin="1899-12-30"),
doy=yday(date),
year=1
)
for(i in 2:nrow(out1)){
out1$year[i]<-ifelse(out1$doy[i]>out1$doy[i-1],out1$year[i-1],out1$year[i-1]+1)
}
out1<-out1 %>%
select(-fecha,-date) %>%
pivot_longer(cols=labranza:siembra,
names_to="parameter",values_to = "val",values_drop_na = TRUE) %>%
left_join(lookups,by=c("parameter"="variable","val"="text")) %>%
mutate(code=ifelse(parameter=="siembra",val,code),
daycent=paste(year,doy,code)) %>%
select(daycent,year,doy)
output$fn <- renderUI({
req(is.null(out1)==FALSE)
textInput("filename",i18n$t("Enter Output File Name"),value="myschedule")
})
output$dl <- renderUI({
req(is.null(out1)==FALSE)
downloadLink('downloadData',i18n$t('Download Daycent Schedule File'))
})
output$schedule<-renderUI({
dl_vers1<-select(out1,-doy,-year)
if(input$header==TRUE){
col1<-c("[XXX]","[XXX]","[XXX].100",0,-1,"-1.00",-1,-1,-1,0,0,-1,1,"[XXX]","","","Year Month Option",
"[XXX]","[XXX]","[XXX]","[XXX]","12","0.08333","F","[XXX].wth","")
col2<-c("Starting Year","Last Year","Site file name","Labeling type","Labeling Year","Microcosm",
"CO2 Systems","pH effect","Soil Warming","N input scalar option","OMAD scalar option",
"Climate scalar option","Initial system","Initial crop","Initial tree","","","Block",
"Last year","Repeats","Output starting year","Output month","Output interval","Weather choice","","")
dl_vers1<-data.frame(col1,col2) %>%
rbind(data.frame(col1=" ",col2=paste(" ",dl_vers1$daycent))) %>%
rbind(data.frame(col1="-999 -999 X",col2=""))
tx1<-paste(paste(dl_vers1$col1,dl_vers1$col2,sep="\t"),collapse="<br>")
}
if(input$header==FALSE){
tx1<-paste(paste0(" ",dl_vers1$daycent),collapse="<br>")
}
HTML(tx1)
})
output$data1<-DT::renderDT({
out1 %>%
group_by(year,doy) %>%
summarise(daycent=paste(daycent,collapse=" "))->out2
schedule<- schedule %>%
arrange(fecha) %>%
mutate(siembra=ifelse(is.na(cultivo),
NA,"FRST"),
date=as.Date(fecha,origin="1899-12-30"),
doy=yday(date),
year=1
)
for(i in 2:nrow(schedule)){
schedule$year[i]<-ifelse(schedule$doy[i]>schedule$doy[i-1],
schedule$year[i-1],schedule$year[i-1]+1)
}
schedule %>%
select(date,doy,year,labranza:siembra) %>%
full_join(out2,by=c("doy","year")) %>%
filter(is.na(daycent)==FALSE)
},options = list(pageLength = 100))
output$downloadData <- downloadHandler(
filename = function() {
paste(input$filename, '.txt', sep='')
},
content = function(con) {
dl_vers<-select(out1,-doy,-year)
if(input$header==TRUE){
col1<-c("[XXX]","[XXX]","[XXX].100",0,-1,"-1.00",-1,-1,-1,0,0,-1,1,"[XXX]","","","Year Month Option",
"[XXX]","[XXX]","[XXX]","[XXX]","12","0.08333","F","[XXX].wth","")
col2<-c("Starting Year","Last Year","Site file name","Labeling type","Labeling Year","Microcosm",
"CO2 Systems","pH effect","Soil Warming","N input scalar option","OMAD scalar option",
"Climate scalar option","Initial system","Initial crop","Initial tree","","","Block",
"Last year","Repeats","Output starting year","Output month","Output interval","Weather choice","","")
dl_vers<-data.frame(col1,col2) %>%
rbind(data.frame(col1=" ",col2=dl_vers$daycent)) %>%
rbind(data.frame(col1="-999 -999 X",col2=""))
}
else{
dl_vers$daycent<-paste0(" ",dl_vers$daycent)
}
write.table(dl_vers,con,row.names=FALSE,quote = FALSE,col.names = FALSE
)
}
)
})
}
# Run the application
shinyApp(ui = ui, server = server)