-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
149 lines (125 loc) · 4.12 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
# remotes::install_github("surveydown-dev/surveydown", force = TRUE)
library(surveydown)
library(dplyr)
library(glue)
# Database setup - see the documentation for details:
# https://surveydown.org/store-data
db <- sd_database(
host = "",
dbname = "",
port = "",
user = "",
table = "",
ignore = TRUE
)
server <- function(input, output, session) {
# Make a 10-digit random number completion code
completion_code <- sd_completion_code(10)
# Store the completion code in the survey data
sd_store_value(completion_code)
# Read in the full survey design file
design <- readr::read_csv("choice_questions.csv")
# Sample a random respondentID and store it in your data
respondentID <- sample(design$respID, 1)
sd_store_value(respondentID, "respID")
# Filter for the rows for the chosen respondentID
df <- design %>%
filter(respID == respondentID) %>%
# Paste on the "images/" path (images are stored in the "images" folder)
mutate(image = paste0("images/", image))
# Function to create the question labels based on design values
make_cbc_options <- function(df) {
alt1 <- df |> filter(altID == 1)
alt2 <- df |> filter(altID == 2)
alt3 <- df |> filter(altID == 3)
options <- c("option_1", "option_2", "option_3")
names(options) <- c(
glue("
**Option 1**<br>
<img src='{alt1$image}' width=100><br>
**Type**: {alt1$type}<br>
**Price**: $ {alt1$price} / lb<br>
**Freshness**: {alt1$freshness}
"),
glue("
**Option 2**<br>
<img src='{alt2$image}' width=100><br>
**Type**: {alt2$type}<br>
**Price**: $ {alt2$price} / lb<br>
**Freshness**: {alt2$freshness}
"),
glue("
**Option 3**<br>
<img src='{alt3$image}' width=100><br>
**Type**: {alt3$type}<br>
**Price**: $ {alt3$price} / lb<br>
**Freshness**: {alt3$freshness}
")
)
return(options)
}
# Create the options for each choice question
cbc1_options <- make_cbc_options(df |> filter(qID == 1))
cbc2_options <- make_cbc_options(df |> filter(qID == 2))
cbc3_options <- make_cbc_options(df |> filter(qID == 3))
cbc4_options <- make_cbc_options(df |> filter(qID == 4))
cbc5_options <- make_cbc_options(df |> filter(qID == 5))
cbc6_options <- make_cbc_options(df |> filter(qID == 6))
# Create each choice question - display these in your survey using sd_output()
# Example: sd_output('cbc_q1', type = 'question')
sd_question(
type = 'mc_buttons',
id = 'cbc_q1',
label = "(1 of 6) If these were your only options, which would you choose?",
option = cbc1_options
)
sd_question(
type = 'mc_buttons',
id = 'cbc_q2',
label = "(2 of 6) If these were your only options, which would you choose?",
option = cbc2_options
)
sd_question(
type = 'mc_buttons',
id = 'cbc_q3',
label = "(3 of 6) If these were your only options, which would you choose?",
option = cbc3_options
)
sd_question(
type = 'mc_buttons',
id = 'cbc_q4',
label = "(4 of 6) If these were your only options, which would you choose?",
option = cbc4_options
)
sd_question(
type = 'mc_buttons',
id = 'cbc_q5',
label = "(5 of 6) If these were your only options, which would you choose?",
option = cbc5_options
)
sd_question(
type = 'mc_buttons',
id = 'cbc_q6',
label = "(6 of 6) If these were your only options, which would you choose?",
option = cbc6_options
)
# Define any conditional skip logic here (skip to page if a condition is true)
sd_skip_if(
input$screenout == "blue" ~ "end_screenout",
input$consent_age == "no" ~ "end_consent",
input$consent_understand == "no" ~ "end_consent"
)
# Define any conditional display logic here (show a question if a condition is true)
sd_show_if(
input$like_fruit %in% c("yes", "kind_of") ~ "fav_fruit"
)
# Database designation and other settings
sd_server(
db = db,
auto_scroll = TRUE,
rate_survey = TRUE,
all_questions_required = TRUE
)
}
# shinyApp() initiates your app - don't change it
shiny::shinyApp(ui = sd_ui(), server = server)