Page Navigation

This page discusses how to create pages and navigate between them in a surveydown survey.

Defining pages

In surveydown, pages can be defined using two different syntax options:

  • Shorthand syntax (recommended)
  • Fence syntax

Shorthand syntax

The shorthand syntax uses three dashes --- followed by the page ID to mark the start of a page. Each new page delimiter automatically closes the previous page, so you don’t need explicit closing tags:

--- page1

Page 1 content here

--- page2

Page 2 content here

This is the recommended approach as it’s more concise and easier to read.

Fence syntax

Alternatively, you can define pages using fences (:::), which explicitly mark both the start and end of each page:

::: {.sd_page id=page1}

Page 1 content here

:::

::: {.sd_page id=page2}

Page 2 content here

:::

With fence syntax, we use three colon symbols :::, called a “fence”, to mark the start and end of pages. This notation is commonly used in Quarto for a variety of use cases, like defining subfigures in images.

In the starting fence, you need to define the class as .sd_page and provide a page id (e.g. page1 and page2 in the example above). Then anything you put between the page fences will appear on that page.

Both syntaxes work identically - choose whichever you find more readable.

If you are using RStudio, you can also make use of the page gadget to create pages under the “Addins” dropdown menu. You can easily link the gadget with a keyboard shortcut from the “Tools” menu. Check out this blog post for a detailed walkthrough.

Here is what the Survey Page Gadget looks like in RStudio:



Adding navigation buttons

To navigate to the next page, you need to insert a sd_next() function call inside a code chunk, like this:

```{r}
sd_next()
```

The above code chunk will create a “Next” button that goes on to the immediate next page. The button will look like this:

You can also provide an alternative label on the next page button using the label argument, like this:

```{r}
sd_next(label = "Go to the next page")
```

Skipping Forward

Direct Forward Skipping

You can send the survey respondent to other forward pages by changing the value assigned to the next_page argument in the sd_next() function. For example, to send the user to a page with the id page3, you can use:

```{r}
sd_next(next_page = 'page3')
```

Conditional Forward Skipping

While basic page navigation is handled using sd_next(), you can override this static navigation in your server function with the sd_skip_if() function to send the respondent to a forward page based on some condition.

A common example is the need to screen out people based on their response(s) to a question. Let’s say you need to screen out people who do not own a vehicle. To do this, you would first define a question in your survey.qmd file about their vehicle ownership, e.g.:

```{r}
sd_question(
  type  = 'mc',
  id    = 'vehicle_ownership',
  label = "Do you own your vehicle?",
  option = c(
    'Yes' = 'yes',
    'No'  = 'no'
  )
)
```

You would also need to define a screenout page to send respondents to, like this:

--- screenout

Sorry, but you are not qualified to take our survey.

Then in the server function in the app.R file, you can use the sd_skip_if() function to define the condition under which the respondent will be sent to the target screenout page, like this:

Note

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.

server <- function(input, output, session) {

  sd_skip_if(
    input$vehicle_ownership == "no" ~ "screenout"
  )

  # ...other server code...

}

You can provide multiple conditions to the sd_skip_if() function, each separated by a comma. The structure for each condition is always:

<condition> ~ "target_page_id"

In the example above, input$vehicle_ownership == "no" is the condition, and "screenout" is the target page that the respondent will be sent to if the condition is met.

Take a look at the Common Conditions section for examples of other types of supported conditions you can use to conditionally control the survey flow.

Back to top