surveyUI | R Documentation |
Create a User Interface for the survey module
surveyUI(
id = "survey",
questionFile,
title = "Survey",
subtitle = "Please answer the questions below. Questions marked with * are required.",
requiredLab = "*",
submitLab = "SUBMIT"
)
id |
The module id. Must be the same as |
questionFile |
A valid file path to an existing CSV file, formatted according to the instructions in |
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. |
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
.
A Shiny user interface with the questions specified in the questionFile
CSV file.
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.