Skip to contents

Creates and deploys a Shiny application for a survey using SurveyJS (https://surveyjs.io) with PostgreSQL database integration. The application handles survey data collection, dynamic fields, and asynchronous logging through a future plan.

Usage

survey(
  json = NULL,
  list = NULL,
  show_response = FALSE,
  theme = "defaultV2",
  theme_color = "#003594",
  shiny_config = list(host = "0.0.0.0", port = 3838),
  db_config = list(host = Sys.getenv("HOST"), port = as.numeric(Sys.getenv("PORT")),
    db_name = Sys.getenv("DB_NAME"), user = Sys.getenv("USER"), password =
    Sys.getenv("PASSWORD"), write_table = Sys.getenv("WRITE_TABLE"), log_table =
    Sys.getenv("LOG_TABLE")),
  dynamic_config = NULL,
  cookie_expiration_days = 7,
  custom_css = NULL,
  suppress_logs = FALSE
)

Arguments

json

String. JSON survey definition or object.

list

List. Survey structure to convert to JSON.

show_response

Logical. Display responses in a data.table after submission. Default: FALSE.

theme

String. SurveyJS theme, either "defaultV2" or "modern". Default: "defaultV2".

theme_color

String. Hex color code for primary theme customization.

shiny_config

List. Optional Shiny configuration parameters.

db_config

List. Database connection parameters. If not specified, values are read from environment variables:

  • host: Database host (env: HOST)

  • port: Database port (env: PORT)

  • db_name: Database name (env: DB_NAME)

  • user: Database username (env: USER)

  • password: Database password (env: PASSWORD)

  • write_table: Survey data table name (env: WRITE_TABLE)

  • log_table: Log messages table name (env: LOG_TABLE)

dynamic_config

List. Configuration for dynamic fields. Supports three types:

Choice Configuration

Populates dropdown or radio button choices from database tables: * config_type: Set to "choice" * table_name: Database table to populate choices from * config_col: Column containing choice text * display_col: Optional column for display text

For dependent fields:
* `parent_table_name`: Parent table for dependency chain
* `parent_id_col`: Column linking to parent table

Parameter Configuration

Handles URL query parameters and hidden fields: * config_type: Set to "param" * table_name: Database table with valid parameters * config_col: Column matching URL parameter name * display_col: Optional column for display text

Unique Value Configuration

Validates unique entries against existing database records: * config_type: Set to "unique" * config_col: Column to check for uniqueness * result: Action on duplicate ("warn" or "stop") * result_field: Survey field for warning message (should be hidden)

Numeric. Number of days to retain survey cookies. Default: 7.

custom_css

String. Custom CSS rules to append to the theme.

suppress_logs

Logical. Suppress console log messages. Default: FALSE.

Value

A Shiny application object

Examples

if (FALSE) { # \dontrun{
# Choice configuration example
dynamic_config <- list(
  list(
    config_type = "choice",
    table_name = "packages",
    config_col = "name"
  )
)

# Parameter configuration example
dynamic_config <- list(
  list(
    config_type = "param",
    table_name = "sources",
    config_col = "source",
    display_col = "display_text"
  )
)

# Unique value configuration example
dynamic_config <- list(
  list(
    config_type = "unique",
    config_col = "title",
    result = "warn",
    result_field = "warning_message"
  )
)
} # }