-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
79 lines (64 loc) · 2.08 KB
/
app.R
File metadata and controls
79 lines (64 loc) · 2.08 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
75
76
77
78
79
# Package setup ---------------------------------------------------------------
# Install required packages:
# install.packages("pak")
# pak::pak(c(
# 'surveydown-dev/surveydown', # Development version from GitHub
# 'tidyverse'
# ))
# Load packages
library(surveydown)
library(tidyverse)
# 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.
# For this demo, we set ignore = TRUE in the following code, which will ignore
# the connection settings and won't attempt to connect to the database. This is
# helpful if you don't want to record testing data in the database table while
# doing local testing. Once you're ready to collect survey responses, set
# ignore = FALSE or just delete this argument.
db <- sd_db_connect(ignore = TRUE)
# Set up car options data frame
cars <- mpg |>
distinct(make = manufacturer, model) |>
mutate(
make = str_to_title(make),
model = str_to_title(model)
)
# UI setup --------------------------------------------------------------------
ui <- sd_ui()
# Server setup ----------------------------------------------------------------
server <- function(input, output, session) {
makes <- unique(cars$make)
names(makes) <- makes
sd_question(
type = "select",
id = "make",
label = "Make:",
option = makes
)
observe({
make_selected_df <- cars[which(sd_value("make") == cars$make), ]
models <- make_selected_df$model
names(models) <- models
sd_question(
type = "select",
id = "model",
label = "Model:",
option = models
)
})
# Run surveydown server and define database
sd_server(db = db)
}
# Launch the app
shiny::shinyApp(ui = ui, server = server)