-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
65 lines (53 loc) · 2.06 KB
/
Copy pathapp.R
File metadata and controls
65 lines (53 loc) · 2.06 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
# Package setup ---------------------------------------------------------------
# Install required packages:
# install.packages("pak")
# pak::pak(c(
# 'surveydown-dev/surveydown', # Development version from GitHub
# 'ggplot2',
# 'dplyr'
# ))
# Load packages
library(surveydown)
library(dplyr)
library(ggplot2)
# 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. Live polling reads
# the responses back, so for a real multi-respondent poll, 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) {
# Refresh data every 5 seconds
data <- sd_get_data(db, refresh_interval = 5)
# Render the plot
output$penguin_plot <- renderPlot({
data() |> # Note the () here, as this is a reactive expression
count(penguins) |>
mutate(
penguins = ifelse(penguins == '', 'No response', penguins)
) |>
ggplot() +
geom_col(aes(x = n, y = reorder(penguins, n)), width = 0.7) +
theme_minimal() +
labs(x = "Count", y = "Penguin Type", title = "Penguin Count")
})
# Run surveydown server and define database
sd_server(db = db)
}
# Launch the app
shiny::shinyApp(ui = ui, server = server)