forked from euraf/agroforestreeadvice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
124 lines (90 loc) · 4.31 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
server <- function(input, output, session) {
#http://127.0.0.1:3775/?in_language=cz&model=Czech this will just take the user to the desired tab, with the desired language
#127.0.0.1:3775/?model=Czech&soil_water=soil_water_waterlogged&habitus=bush this triggers a modal dialog to download a txt file with the species scores for this particular set of conditions
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['in_language']])) {
updateRadioButtons(session, "in_language", selected = query[['in_language']])
}
desiredmodel <- query$model
if(is.null(input$sidemenu) || !is.null(desiredmodel) && desiredmodel != input$sidemenu){
freezeReactiveValue(input, "sidemenu")
updateTabItems(session, "sidemenu", selected = "tool")
freezeReactiveValue(input, "toolsTabset")
print(desiredmodel)
updateTabsetPanel(session, "toolsTabset", selected = desiredmodel)
}
allotherparameters<-setdiff(names(query), c("in_language", "model"))
if(length(allotherparameters)>0){ #we passed parameters by URL = we want to get the results as csv
queryinputs<-unlist(query[allotherparameters]) #icicicic todo : check that all necessary inputs are provided before sending to the suitability function
print(paste("computing suitability of model", desiredmodel))
resultdf<-do.call(paste("compute_suitability_", desiredmodel, sep=""), list(
inputsdata=queryinputs,
database=get(paste("data", desiredmodel, sep="")),
interface=get(paste("interface", desiredmodel, sep="")))
)
#print(str(resultCSV))
# Define a function to generate and return the CSV content
generatetxtContent <- function(df) {
m <- as.matrix(df)
# add column headers
m <- rbind(dimnames(m)[[2]], m)
m<-apply(m, 1, paste, collapse="\t")
return(m)
}
preview_table <- head(resultdf, n = 5)
# Display data preview in the modal
output$dataPreview <- renderUI({
tableOutput("previewTable")
})
output$previewTable <- renderTable({
preview_table
})
# Handle the download button within the modal
output$modalDownload <- downloadHandler(
filename = function() {
"computed_data.csv"
},
content = function(file) {
txt_content <- generatetxtContent(resultdf)
writeLines(txt_content, file)
}
)
showModal(modalDialog(
title = "Download txt file",
htmlOutput("dataPreview"), # Display data preview
footer = tagList(
modalButton("Cancel"),
downloadButton("modalDownload", "Download")
),
size = "l",
easyClose = TRUE
))
}
}) #end managing URL queries
# Reactive expression for the selected language
language <- reactive({
input$in_language
})
# Czech tree advice ----
moduleTabInterface_Server(id = "Czech",
language= language,
data=dataCzech, interface=interfaceCzech, functionSuitability=compute_suitability_Czech, compactobjectives=FALSE)
# Flanders tree advice ----
moduleTabInterface_Server( # nom de la fonction server du module
id = "DENTRO", # Attention à bien donner le même id que dans ui !
language= language,
data = dataDENTRO, interface= interfaceDENTRO, functionSuitability=compute_suitability_DENTRO, compactobjectives=TRUE )
# Shade tree advice ----
moduleTabInterface_Server(id = "STA",
language= language,
data=dataSTA, interface=interfaceSTA, functionSuitability=compute_suitability_STA, compactobjectives=TRUE)
# Deciduous ----
moduleTabInterface_Server(id = "DECIDUOUS",
language= language,
data=dataDECIDUOUS, interface=interfaceDECIDUOUS, functionSuitability=compute_suitability_DECIDUOUS, compactobjectives=FALSE)
# Species Climate Suitability Model ----
moduleTabInterface_Server(id = "SCSM",
language= language,
data=dataSCSM, interface=interfaceSCSM, functionSuitability=compute_suitability_SCSM, compactobjectives=FALSE)
}