-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
137 lines (118 loc) · 4.16 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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
# Read the survey questions
Qlist <- read.csv("Qlist.csv")
# Qlist <- Qlist[1,]
shinyServer(function(input, output) {
# Create an empty vector to hold survey results
results <<- rep("", nrow(Qlist))
# Name each element of the vector based on the
# second column of the Qlist
names(results) <<- Qlist[,2]
# Hit counter
output$counter <-
renderText({
if (!file.exists("counter.Rdata")) counter <- 0
if (file.exists("counter.Rdata")) load(file="counter.Rdata")
counter <- counter <<- counter + 1
save(counter, file="counter.Rdata")
paste0("Hits: ", counter)
})
# This renderUI function holds the primary actions of the
# survey area.
output$MainAction <- renderUI( {
dynamicUi()
})
# Dynamic UI is the interface which changes as the survey
# progresses.
dynamicUi <- reactive({
# Initially it shows a welcome message.
if (input$Click.Counter==0)
return(
list(
h5("Welcome to Shiny Survey Tool!"),
h6("by VicinoTech")
)
)
# Once the next button has been clicked once we see each question
# of the survey.
if (input$Click.Counter>0 & input$Click.Counter<=nrow(Qlist))
return(
list(
h5(textOutput("question")),
radioButtons("survey", "Please Select:",
c("Prefer not to answer", option.list()))
)
)
# Finally we see results of the survey as well as a
# download button.
if (input$Click.Counter>nrow(Qlist))
return(
list(
h4("View aggregate results"),
tableOutput("surveyresults"),
h4("Thanks for taking the survey!"),
downloadButton('downloadData', 'Download Individual Results'),
br(),
h6("Haven't figured out how to get rid of 'next' button yet")
)
)
})
# This reactive function is concerned primarily with
# saving the results of the survey for this individual.
output$save.results <- renderText({
# After each click, save the results of the radio buttons.
if ((input$Click.Counter>0)&(input$Click.Counter>!nrow(Qlist)))
try(results[input$Click.Counter] <<- input$survey)
# try is used because there is a brief moment in which
# the if condition is true but input$survey = NULL
# If the user has clicked through all of the survey questions
# then R saves the results to the survey file.
if (input$Click.Counter==nrow(Qlist)+1) {
if (file.exists("survey.results.Rdata"))
load(file="survey.results.Rdata")
if (!file.exists("survey.results.Rdata"))
presults<-NULL
presults <- presults <<- rbind(presults, results)
rownames(presults) <- rownames(presults) <<-
paste("User", 1:nrow(presults))
save(presults, file="survey.results.Rdata")
}
# Because there has to be a UI object to call this
# function I set up render text that distplays the content
# of this funciton.
""
})
# This function renders the table of results from the
# survey.
output$surveyresults <- renderTable({
t(summary(presults))
})
# This renders the data downloader
output$downloadData <- downloadHandler(
filename = "IndividualData.csv",
content = function(file) {
write.csv(presults, file)
}
)
# The option list is a reative list of elements that
# updates itself when the click counter is advanced.
option.list <- reactive({
qlist <- Qlist[input$Click.Counter,3:ncol(Qlist)]
# Remove items from the qlist if the option is empty.
# Also, convert the option list to matrix.
as.matrix(qlist[qlist!=""])
})
# This function show the question number (Q:)
# Followed by the question text.
output$question <- renderText({
paste0(
"Q", input$Click.Counter,":",
Qlist[input$Click.Counter,2]
)
})
})