-
Notifications
You must be signed in to change notification settings - Fork 0
/
g1.R
95 lines (68 loc) · 2.49 KB
/
g1.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
#install.packages("shiny")
library(shinyjs)
library(shiny)
labelMandatory <- function(label) {
tagList(
label,
span("* (Required)", class = "mandatory_star")
)
}
appCSS <- ".mandatory_star { color: red; }"
fieldsMandatory <- c("name", "qual", "lang")
#ValidationfieldsMandatory <- c("name", "qual", "lang")
fieldsAll <- c("name", "qual", "gender", "lang")
responsesDir <- file.path("...responses")
epochTime <- function() {
as.integer(Sys.time())
}
shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
shinyjs::inlineCSS(appCSS),
titlePanel("Shiny form"),
div(
id = "form",
conditionalPanel(
condition = "input[name] != ' ' && input[qual] != ' ' && input[lang] != ' '" ),
textInput("name", labelMandatory("Name"), ""),
textInput("qual", labelMandatory("Qualifications"), ""),
textInput("age", "Current age"),
radioButtons("gender", "Gender",c("Male", "Female", "Other")),
selectInput("lang", labelMandatory("Programming languages familiar with"),
c("", "C", "C++", "Python", "JAVA", "R")),
actionButton("submit", "Submit", class = "btn-primary")
)
),
server = function(input, output, session) {
# a<-is.alpha(input)
observe({
mandatoryFilled <-
vapply(fieldsMandatory,
function(x) {
!is.null(input[[x]]) && input[[x]] != ' ' &&input[[x]] == "a"
},
logical(1))
mandatoryFilled <- all(mandatoryFilled)
formData <- reactive({
data <- sapply(fieldsAll, function(x) input[[x]])
data <- c(data, timestamp = epochTime())
data <- t(data)
data
})
humanTime <- function() format(Sys.time(), "%Y%m%d-%H%M%OS")
saveData <- function(data) {
fileName <- sprintf("%s_%s.csv",
humanTime(),
digest::digest(data))
write.csv(x = data, file = file.path(responsesDir, fileName),
row.names = FALSE, quote = TRUE)
}
# action to take when submit button is pressed
observeEvent(input$submit, {
saveData(formData())
})
# enable/disable the submit button
shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
})
}
)