-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
120 lines (99 loc) · 3.1 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
library(shiny)
# which fields get saved
fieldsAll <- c("date", "samplingTrip", "recorder", "location",
'commonName', 'sciName', 'length_cm', 'count', 'sex', 'notes', 'photoID',
'start_time', 'stop_time', 'ownership', 'nearestLandmark', 'gear',
'bait', 'samplersNumber', 'depth'
)
# which fields are mandatory
fieldsMandatory <- c("date", "samplingTrip", "recorder", "location")
# functions
outputDir <- "responses"
saveData <- function(data) {
data <- t(data)
# Create a unique file name
fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
# Write the file to the local system
write.csv(
x = data,
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
loadData <- function() {
# Read all the files into a list
files <- list.files(outputDir, full.names = TRUE)
data <- lapply(files, read.csv, stringsAsFactors = FALSE)
# Concatenate all data together into one data.frame
data <- do.call(rbind, data)
data
}
server = function(input, output, session) {
updateApp <- reactive({
# read in fish data
Micro_fish <- read.csv("data/Micro_fish.csv", stringsAsFactors = FALSE)
data <- Micro_fish
data <- data[data$ComName %in% input$p1,]
updateSelectizeInput(session, 'p2', choices = data$Species, selected = data$ComName, server = TRUE)
data
})
output$table <- DT::renderDataTable(
DT::datatable(updateApp())
)
observe({
mandatoryFilled <-
vapply(fieldsMandatory,
function(x) {
!is.null(input[[x]]) && input[[x]] != ""
},
logical(1))
mandatoryFilled <- all(mandatoryFilled)
shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
})
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fieldsAll, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# clear last responce
observeEvent(input$resetLast,{
#Define the file name that will be deleted
df <- file.info(list.files("./responses/", full.names = T))
fn = rownames(df)[which.max(df$mtime)]
#Check its existence
if (file.exists(fn))
#Delete file if it exists
file.remove(fn)
})
observeEvent(input$reset,{
file.remove(dir(
'responses/',
pattern = "*",
full.names = TRUE
))
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
input$resetLast
input$reset
loadData()
})
# download table
# Reactive expression with the data, in this case iris
react_data <- reactive(loadData())
output$dto <- renderDataTable({react_data()})
output$download <- downloadHandler(
filename = function() {
paste("Sampling-data-", Sys.Date(), ".csv", sep="")
},
content = function(filename){
write.csv(react_data(), filename, row.names=FALSE)
}
)
}