library(surveydown)
# sd_db_config()
db <- sd_db_connect()
App File
This page details the contents of the app.R
file, where you define the global settings and server logic for your survey. It defines a Shiny app, so if you are familiar with Shiny you should feel right at home.
The main components of the app.R
file are:
- Global settings: where you load the surveydown package and define the database connection.
- Server: where you define the logic of the survey (conditional question display / conditional survey flow, etc.)
Global settings
The global settings are at the top of the app.R
file. At a minimum, you need to load the surveydown
package and define the database connection. You can also load other packages / global objects here if you need to. It typically looks like this:
The db
object is used to store survey data - see the Storing Data page for details on how to set up the database connection.
Server
The server()
function is a standard Shiny server function that takes input
, output
, and session
as arguments. It is where you can set custom control logic and other configuration options, such as conditional question display logic with the sd_show_if()
function, or conditional survey flow logic with the sd_skip_forward()
function.
If you create a new survey using a template, the server()
function looks like this:
server <- function(input, output, session) {
# Define conditional skip logic (skip forward to page if a condition is TRUE)
sd_skip_forward()
# Define conditional display logic (show a question if a condition is TRUE)
sd_show_if()
# Main server to control the app
sd_server(db = db)
}
The sd_server()
function at the bottom makes everything run. It also includes some optional arguments that you can use to customize the survey.
See the Server Options page for details on the different options you can use to customize the server.
The db = db
argument in sd_server()
is required if you are using a database connection, which should be defined using the sd_database()
function as mentioned above in the Global Settings section. See the Store Data page for more details.
Run code
At the very bottom of the app.R
file, you will see the following code:
This code defines the Shiny app and should always be at the bottom of the app.R
file.
What’s with the sd_ui()
thing?
In a typical Shiny app, you have to define the UI and server functions separately. In a surveydown survey, the UI is a fixed structure that is defined by the sd_ui()
function, so simply provide it to the ui
argument in shiny::shinyApp()
to run the survey.
Locally running the survey
To preview your survey, you can run the Shiny app locally by clicking the “Run App” button in RStudio or in your R console run the code shiny::runApp('app.R')
. Typically, RStudio will launch the app in a new window, but you can also choose to have the app launch in a dedicated viewer pane, or in your external web browser. Make your selection by clicking the icon next to Run App:
