Skip to contents

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 admin functionality.

Usage

sd_server(input, output, session, config, db = NULL)

Arguments

input

The Shiny input object.

output

The Shiny output object.

session

The Shiny session object.

config

A list containing configuration settings for the application.

db

A list containing database connection information created using sd_database function. Defaults to NULL.

Value

This function does not return a value; it sets up the server-side logic for the Shiny application.

Details

The config list should include the following elements:

  • page_structure: A list defining the structure of survey pages.

  • page_ids: A vector of page identifiers.

  • question_ids: A vector of question identifiers.

  • show_if: A data frame defining conditions for showing questions.

  • skip_if: A data frame defining conditions for skipping pages.

  • skip_if_custom: A list of custom skip conditions.

  • show_if_custom: A list of custom show conditions.

  • start_page: The identifier of the starting page.

  • question_required: A vector of required question identifiers.

  • all_questions_required: A logical indicating if all questions are required.

  • admin_page: A logical indicating if an admin page should be included.

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 operations or saves to a local CSV file in preview mode.

  • Sets up admin functionality if enabled in the configuration.

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 (pause mode), responses will be saved to a local CSV file.

Examples

if (FALSE) { # \dontrun{
  shinyApp(
    ui = sd_ui(),
    server = function(input, output, session) {
      sd_server(input, output, session, config = my_config, db = my_db)
    }
  )

  # With admin page enabled
  my_config <- sd_config(admin_page = TRUE)
  shinyApp(
    ui = sd_ui(),
    server = function(input, output, session) {
      sd_server(input, output, session, config = my_config, db = my_db)
    }
  )
} # }