Surveys in Shiny: an interactive guide
Build a web survey in R that stores structured responses, step by step
Companion to the workshop Survey and Questionnaire Construction: the Shiny Package in R, Edinburgh Futures Institute, November 2024.
Shiny turns R code into a web application. This workshop uses it to build a survey that does more than off-the-shelf tools: it asks for consent first, refuses to submit until required questions are answered, records how long each person took, and writes each response as a new row in a Google Sheet. Every widget below runs in your browser and shows the R code that would produce it.
1Anatomy of an app
Every Shiny app lives in one app.R file, in its own folder, and has three parts:
- ui: what the app looks like (its “face”);
- server: the logic that reacts to what the user does;
- shinyApp(ui = ui, server = server): the call that joins them and runs the app.
The key idea is reactivity: when an input changes, every output that depends on it re-runs on its own. Move the slider in this copy of the workshop’s first app.
2Widget workshop
Asking every question as free text gives you messy data. Widgets fix the form of the answer. Every widget takes the same first two arguments, an id (used in the server as input$id) and a label (shown to the user), then a default. Choose a widget, edit its label, and see both the widget and its code.
A design point from the workshop: for radio buttons use selected = 0, so no answer is pre-selected and nobody is nudged towards one.
3Storing responses
Saving with write.csv() only works on your own computer. Once the app is hosted on shinyapps.io it runs on a remote server, so responses need to go somewhere remote. The workshop uses Google Sheets through the googlesheets4 package: free, structured, and easy to export as .csv.
Four pieces of server code do the work:
| Piece | What it does |
|---|---|
fields <- c("name", "used_shiny", "r_num_years") | Names the inputs to save, which become the sheet’s columns |
formData <- reactive(...) | Collects the current value of each field into one record |
save_data_gsheets() with sheet_append() | Adds that record as a new row, without touching earlier rows |
observeEvent(input$submit, ...) | Runs the save only when Submit is clicked |
4Authentication and secrets
Every visitor to your app needs to write to the sheet, but none of them should be able to log in as you. The answer is a service account: a robot Google account that the app authenticates as.
- In Google Cloud Console, go to IAM and Admin, then Service Accounts, and create one.
- Create a key for it in JSON format and download it.
- Copy the service account’s email from the JSON file and share your Google Sheet with that email as an editor. Enable the Google Sheets API for the project.
- In your app’s folder create a
.secretsfolder, put the key there asservice-account.json, and authenticate withgs4_auth(path = ".secrets/service-account.json").
SHEET_ID). Anyone with either can read or change your respondents’ data. Never put them in a public repository, a slide or a handout. Sharing the sheet with the service account is enough; you do not need to make it editable by “anyone with the link”.5Making it look like a survey
An unstyled app puts everything on the left and looks unfinished. The workshop adds a small style sheet with tags$style(HTML("...")) and wraps each question in div(class = ...). Toggle the styling to see the difference.
6The finished survey
The final app adds three pieces of logic:
- Consent first.
conditionalPanel()shows a consent page. With consent the survey appears; without it, a thank-you page. - Required questions.
shinyjs::disable("submit")greys out the button until a reactive check finds every starred question answered. - Timing. A small JavaScript timer starts when the app connects and sends the seconds taken to
input$time_spenton submit, which helps spot rushed answers or bots.
The “how did you travel” checkboxes are also split into one 0/1 column per option, which is much easier to analyse than a list. Try the full flow.
7Deploying
Run the app locally with the Run App button, then publish it to shinyapps.io with the Publish button. If publishing fails (for example, shinyapps.io mistakes the app for a Quarto document), deploy from the console and list the secrets folder so the key travels with the app:
rsconnect::deployApp(appDir = "your_app",
appPrimaryDoc = "app.R",
appFiles = c("app.R", ".secrets/"))
8Check yourself
9Workshop files
- Training materials (PDF) and slides
- Example code, building the app step by step: #1, #2, #3, #4, #5, #6, #7, #8
- Example .css and example .js
- See it running: the survey app on shinyapps.io