```{r}
# Conditional question
sd_question(
type = "mc",
id = "penguins",
label = "Which is your favorite type of penguin?",
option = c(
"Adélie" = "adelie",
"Chinstrap" = "chinstrap",
"Gentoo" = "gentoo",
"Other" = "other"
)
)
# Target question
sd_question(
type = "text",
id = "penguins_other",
label = "Please specify the other penguin type:"
)
```
Conditional Question Display
It is often useful to have a question display based on some condition, such as the respondent choosing a particular value in a multiple choice question.
For example, let’s say we have a choice question about people’s favorite penguin type, and the last option is “other”. If the respondent chose it, you may want a second question to display that allows them to specify the “other” penguin type, like this:

To implement this, you first need to define both the conditional question and the target question in the survey.qmd
file, like this:
Then in the server function in the app.R
file, you can use the sd_show_if()
function to define that the "penguins_other"
question would only be shown if the respondent chose the "other"
option in the "penguins"
question, like this:
server <- function(input, output, session) {
sd_show_if(
input$penguins == "other" ~ "penguins_other"
)
sd_server(db = db)
}
The structure of the condition in the sd_show_if()
function is always:
<condition> ~ "target_question_id"
You can provide multiple conditions to the sd_show_if()
function, each separated by a comma.
In the example above, input$penguins == "other"
is the condition, and "penguins_other"
is the target question that will be shown if the condition is met. The ~
symbol is used to separate the condition from the target question.
Take a look at the Common Conditions page for examples of other types of supported conditions you can use to conditionally display questions.
The input
object is a Shiny object that stores each question id
defined by sd_question()
in your survey.qmd
file, so whenever referring to a question in a condition, you must use the format input$question_id
.