sd_server {surveydown} | R Documentation |
Server logic for a surveydown survey
Description
This function defines the server-side logic for a 'shiny' application used in surveydown. It handles various operations such as conditional display, progress tracking, page navigation, database updates for survey responses, and exit survey functionality.
Usage
sd_server(
db = NULL,
required_questions = NULL,
all_questions_required = FALSE,
start_page = NULL,
auto_scroll = FALSE,
rate_survey = FALSE,
language = "en",
use_cookies = TRUE,
highlight_unanswered = TRUE,
highlight_color = "gray"
)
Arguments
db |
A list containing database connection information created using
|
required_questions |
Vector of character strings. The IDs of questions
that must be answered. Defaults to |
all_questions_required |
Logical. If |
start_page |
Character string. The ID of the page to start on.
Defaults to |
auto_scroll |
Logical. Whether to enable auto-scrolling to the next
question after answering. Defaults to |
rate_survey |
Logical. If |
language |
Set the language for the survey system messages. Include
your own in a |
use_cookies |
Logical. If |
highlight_unanswered |
Logical. If |
highlight_color |
Character string. Color for highlighting unanswered questions. Options are "blue", "orange", "green", "purple", "gray", or "grey". Defaults to "gray". |
Details
The function performs the following tasks:
Initializes variables and reactive values.
Implements conditional display logic for questions.
Tracks answered questions and updates the progress bar.
Handles page navigation and skip logic.
Manages required questions.
Performs database operation.
Controls auto-scrolling behavior based on the
auto_scroll
argument.Uses sweetalert for warning messages when required questions are not answered.
Handles the exit survey process based on the
rate_survey
argument.
Value
This function does not return a value; it sets up the server-side logic for the 'shiny' application.
Progress Bar
The progress bar is updated based on the last answered question. It will jump to the percentage corresponding to the last answered question and will never decrease, even if earlier questions are answered later. The progress is calculated as the ratio of the last answered question's index to the total number of questions.
Database Operations
If db
is provided, the function will update the database with survey
responses. If db
is NULL
(ignore mode), responses will be saved to a local
CSV file.
Auto-Scrolling
When auto_scroll
is TRUE
, the survey will automatically scroll to the
next question after the current question is answered. This behavior can be
disabled by setting auto_scroll = FALSE
.
Exit Survey
When rate_survey = TRUE
, the function will show a rating question when
the user attempts to exit the survey. When FALSE
, it will show a simple
confirmation dialog. The rating, if provided, is saved with the survey data.
See Also
sd_database()
, sd_ui()
Examples
if (interactive()) {
library(surveydown)
# Get path to example survey file
survey_path <- system.file("examples", "basic_survey.qmd",
package = "surveydown")
# Copy to a temporary directory
temp_dir <- tempdir()
file.copy(survey_path, file.path(temp_dir, "survey.qmd"))
orig_dir <- getwd()
setwd(temp_dir)
# Define a minimal server
server <- function(input, output, session) {
# sd_server() accepts these following parameters
sd_server(
db = NULL,
required_questions = NULL,
all_questions_required = FALSE,
start_page = NULL,
auto_scroll = FALSE,
rate_survey = FALSE,
language = "en",
use_cookies = TRUE,
highlight_unanswered = TRUE,
highlight_color = "gray"
)
}
# Run the app
shiny::shinyApp(ui = sd_ui(), server = server)
# Clean up
setwd(orig_dir)
}