-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
74 lines (59 loc) · 2.09 KB
/
Copy pathapp.R
File metadata and controls
74 lines (59 loc) · 2.09 KB
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
# Package setup ---------------------------------------------------------------
# Install required packages:
# install.packages("pak")
# pak::pak(c(
# 'surveydown-dev/surveydown', # Development version from GitHub
# 'readr',
# 'dplyr'
# ))
# Load packages
library(surveydown)
library(dplyr)
library(readr)
# Database setup --------------------------------------------------------------
#
# Details at: https://surveydown.org/docs/storing-data
#
# surveydown stores data on any PostgreSQL database. We recommend
# https://supabase.com/ for a free and easy to use service.
#
# Once you have your database ready, run the following function to store your
# database configuration parameters in a local .env file:
#
# sd_db_config()
#
# Once your parameters are stored, you are ready to connect to your database.
# This template runs in preview mode (set via `mode: preview` in survey.qmd),
# which saves responses locally instead of to a database. To collect real
# responses, run sd_db_config() to store your database credentials, then
# change `mode` to `database` in the survey.qmd YAML header.
db <- sd_db_connect()
# UI setup --------------------------------------------------------------------
ui <- sd_ui()
# Server setup ----------------------------------------------------------------
server <- function(input, output, session) {
# Read in the design file (generated in the 'make-design.R' file)
design <- read_csv("design.csv")
# Randomly choose a row id
q1_id <- sample(design$id, 1)
# Store the chosen row id in the survey data (here q1_id will be the column name)
sd_store_value(q1_id)
# Filter the design to get the chosen row
numbers <- design |>
filter(id == q1_id) |>
pull(numbers)
# Create the options
q1_options <- c('option 1', 'option 2', 'option 3')
names(q1_options) <- numbers
# Create the reactive question
sd_question(
id = "q1",
type = "mc",
label = "Which of these numbers is the largest?",
option = q1_options
)
# Run surveydown server and define database
sd_server(db = db)
}
# Launch the app
shiny::shinyApp(ui = ui, server = server)