Learn R Programming

surveydown (version 0.5.0)

sd_show_if: Define show conditions for survey questions

Description

This function is used to define conditions under which certain questions in the survey should be shown. It takes one or more formulas where the left-hand side is the condition and the right-hand side is the target question ID. If called with no arguments, it will return NULL and set no conditions.

Usage

sd_show_if(...)

Value

A list of parsed conditions, where each element contains the condition and the target question ID. Returns NULL if no conditions are provided.

Arguments

...

One or more formulas defining show conditions. The left-hand side of each formula should be a condition based on input values, and the right-hand side should be the ID of the question to show if the condition is met.

See Also

sd_skip_if()

Examples

Run this code
if (interactive()) {
  library(surveydown)

  # Get path to example survey file
  survey_path <- system.file("examples", "sd_show_if.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_show_if(
      # If "Other" is chosen, show the conditional question
      input$fav_fruit == "other" ~ "fav_fruit_other"
    )

    sd_server()
  }

  # Run the app
  shiny::shinyApp(ui = sd_ui(), server = server)

  # Clean up
  setwd(orig_dir)
}

Run the code above in your browser using DataLab