surveyUI: Create a User Interface for the survey module

surveyUIR Documentation

Create a User Interface for the survey module

Description

Create a User Interface for the survey module

Usage

surveyUI(
  id = "survey",
  questionFile,
  title = "Survey",
  subtitle = "Please answer the questions below. Questions marked with * are required.",
  requiredLab = "*",
  submitLab = "SUBMIT"
)

Arguments

id

The module id. Must be the same as surveyServer.

questionFile

A valid file path to an existing CSV file, formatted according to the instructions in surveyPrep. Must be the same as the one used in surveyServer.

title

The title of the survey to display to participants.

subtitle

The subtitle of the survey to display to participants.

requiredLab

The value that will be appended to required questions' labels. Defaults to asterisk (*).

submitLab

The label that should be displayed on the button participants will click to submit the form.

Details

In a Shiny App, this should always be used with surveyServer. Before creating the app, be sure to format your questionFile according to the instructions in surveyPrep.

Value

A Shiny user interface with the questions specified in the questionFile CSV file.

Examples

library(shiny)
library(shinyjs)
data("demographics")
write.csv(demographics, "demographics.csv", row.names = FALSE)
# If you use your own survey file, run surveyPrep() first!

# You can use IDs specified in `returnVals` to trigger events after the survey is submitted.
if (interactive()) {
  ui <- fluidPage(
    useShinyjs(),
    sidebarLayout(
      sidebarPanel(width = 4,
                   textOutput("age")),
      mainPanel(width = 8,
                actionButton("begin", "Begin"),
                surveyUI(id = "survey",
                         title = "Background Information Survey",
                         questionFile = "demographics.csv",
                         notListedLab = "Not listed:")
      )
    )
  )

  server <- function(input, output, session) {
    observeEvent(input$begin, {
      hide("begin")
      # Show the survey when "begin" is clicked.
      answers <- surveyServer(id = "survey",
                              questionFile = "demographics.csv",
                              notListedLab = "Not listed:",
                              outFile = "sample.rds",
                              returnVals = c("age", "sex"),
                              result = "clear")

      # Once an answer for "age" is submitted, show the answer in the sidebar panel.
      observeEvent(answers$age, {
        output$age <- renderText({paste0("You are ", answers$age, " years old.")})
      })
    })
  }
  shinyApp(ui = ui, server = server)
}

# Or, you can trigger events on click of the survey's submit button
#  using the module id in the following string: `input[["id-submit"]]`
if (interactive()) {
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(width = 4,
                   textOutput("age")),
      mainPanel(width = 8,
                actionButton("begin", "Begin"),
                surveyUI(id = "survey",
                         title = "Background Information Survey",
                         questionFile = "demographics.csv",
                         notListedLab = "Not listed:")
      )
    )
  )

  server <- function(input, output, session) {
    observeEvent(input$begin, {
      answers <- surveyServer(id = "survey",
                              questionFile = "demographics.csv",
                              notListedLab = "Not listed:",
                              outFile = "sample.rds",
                              returnVals = c("age", "sex"),
                              result = "hide")

      observeEvent(input[["survey-submit"]], {
        output$age <- renderText({paste0("You are ", answers$age, " years old.")})
      })
    })
  }
  shinyApp(ui = ui, server = server)
}

abbey-thomas/speechcollectr documentation built on Nov. 19, 2024, 7:09 p.m.