surveyServer | R Documentation |
Server function for the survey module
surveyServer(
id = "survey",
questionFile,
notListedLab = NULL,
outFile = NULL,
returnVals = NULL,
result = c("clear", "hide")
)
id |
The module id. Must be the same as in |
questionFile |
A valid file path to an existing CSV file, formatted according to the instructions in |
notListedLab |
For non-textInput questions, the name of the choice that will allow your participants to enter their own value for all questions that include this string in the 'options' column of |
outFile |
Name of rds or csv file to create with a participant's answers. Must be unique for each participant. |
returnVals |
Vector of inputs to return as reactive values. Must be a subset of the input IDs from the id column of |
result |
Action to perform when the participant clicks the submit button. Must be one of 'clear' (to clear the form) or 'hide' (to hide all form UI elements). |
A CSV or RDS file containing the values input by the participant as well as a character vector indicating the name of the file returned (same as supplied in outFile
, unless outFile
already exists; see note below), and/or a list of reactive values specified by returnVals
.
This function returns a file every time the submit button is clicked. To avoid over-writing files, the function will check if the file specified in outfile
exists. To use a custom naming scheme for individual files (e.g., to name all files from the same participant with the same prefix), we recommend pasting a unique ID number (from a reactive value) to a prefix to save a separate file for each participant. Though this function creates many small files, these files can easily be processed in bulk during analysis, which is less risky than opening and appending to existing files when multiple users visit the app simultaneously! Events can be attached to the submit button from the survey*() functions using the value of the id
argument in the following manner: input[["id-submit"]]
.
surveyUI
, surveyPrep
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("answer")),
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$answer <- renderText({paste0("Your answers were saved as ",
answers$filename,". 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.