Dynamic Field Configuration
dynamic_field_config.Rmd
This package provides dynamic field capabilities with flexible types and dependency chain support.
The package supports three main types of dynamic configurations:
- Choice Configuration: Populate dropdown and radio button choices
- Parameter Configuration: Validate and use data in URL query parameters
- Unique Value Configuration: Validate field values against existing database records
All types include automatic saving to the survey database and error
handling. The dynamic configuration is specified through the
dynamic_config
parameter in survey()
. It
accepts a list of configuration objects.
Choice Configuration
Choice Configuration provides:
- Dependent fields with dynamic updates based on parent selections
- Flexible handling of dropdown and radio button options
- Support for both single and multi-level dependencies
Example
Imagine I want to develop an R package feedback survey that allows users to select one of my packages and a dependent version number populated from a database table:
dynamic_config = list(
list(
config_type = "choice", # Type of configuration
table_name = "config_packages", # Database table to populate choices from
config_col = "package" # Column with the choices
),
list(
config_type = "choice", # Type of configuration
table_name = "config_packages_versions", # Database table for dependent choices
parent_table_name = "config_packages", # Parent table for dependency
parent_id_col = "package_id", # Links to parent table
config_col = "version" # Column for dependent choices
)
)
This configuration creates two dependent fields where the second field’s choices update based on the selection in the first field.
Parameter Configuration
Parameter Configuration provides:
- URL parameter validation against database tables
- Optional display text mapping with hidden field integration
- Accept values from URL parameters
- Store values in hidden fields
- Look up display text from database tables
Example
Imagine I want to publish the link to my R package feedback form on my github profile, but I also want post it on bluesky and possibly present it at posit::conf(2025). I can use the URL query to track where I’m receiving feedback from.
# URL: http://127.0.0.1:3838/?source=github
dynamic_config = list(
list(
config_type = "param", # Type of configuration
table_name = "config_source", # Database table with valid parameters
config_col = "source", # Matches URL parameter name
display_col = "display_text" # Optional: Show display text
)
)
This feature is useful for tracking individuals, groups, or referral sources. It also allows you to pipe data into the survey UI using a hidden text field with the same name.
Unique Value Configuration
Unique Value Configuration provides:
- Text normalization for comparison
- Configurable validation responses (warn or stop)
- Custom warning or error messages
- Integration with form submission logic
Example
Imagine I want to create a different survey to submit issues relate to my package but to prevent users from submitting the same issue if the title already exists. (Yes, I know this would never happen because we have Github.)
The following configuration ensures that each package issue report has a unique title:
dynamic_config = list(
list(
config_type = "unique", # Type of configuration
config_col = "issue_title", # Column to check for uniqueness
result = "stop", # Action on duplicate ("warn" or "stop")
result_field = "title_error" # Field for custom error message
)
)
This configuration will:
- Check the entered issue title against existing records in the database
- If a duplicate is found, either:
- Display a warning message (result = “warn”)
- Prevent form submission and display an error message (result = “stop”)
The validation normalizes text by:
- Converting to lowercase
- Trimming whitespace
- Removing special characters