class: center, middle, inverse .leftcol40[ <center> <img src="https://surveydown.org/images/logo.png" width=100%> </center> ] .rightcol60[ <br><br> # .font150[.fancy[Data Handling]] ] --- class: inverse, middle # .fancy[Data Handling] ### 1. Storing data ### 2. Fetching data --- class: inverse, middle # .fancy[Data Handling] ### 1. .orange[Storing data] ### 2. Fetching data --- class: center .leftcol[ ## Data is stored in any PostgreSQL database <center> <img src="images/postgresql.svg" width=70%> </center> ] .rightcol[ ## We use [Supabase](https://supabase.com/) as a free, open-source option <center> <img src="images/supabase.svg" width=70%> </center> ] --- ## Store data in [Supabase](https://supabase.com/) Steps to connect a database via Supabase: 1. Create a Supabase account 2. Create a Supabase project 3. Copy your credentials <br> ## Full details on the [Storing Data docs page](https://surveydown.org/docs/storing-data) --- ## Creating a project .leftcol60[ <center> <img src="images/supabase-project.png" width=100%> </center> ] .rightcol40[ - Choose a project name (this is your "database") - Each database can have multiple tables - Choose a strong password ] --- ## Getting your Supabase credentials .leftcol[ Click the "connect" button in your project <center> <img src="images/supabase-connect.png" width=100%> </center> ] .rightcol[ Find the “Transaction pooler” section <center> <img src="images/supabase-connection.png" width=100%> </center> ] --- ## Store your database credentials .leftcol[ In your R console, run: ``` r surveydown::sd_db_config() ``` <br> Credentials are stored in a `.env` file in your root project folder. ] .rightcol[ <center> <img src="images/sd-db-config.png" width=100%> </center> ] --- .leftcol[ # .center[app.R] .code70[ ``` r library(surveydown) # Connects to database *db <- sd_db_connect() # Main UI ui <- sd_ui() server <- function(input, output, session) { # Main server sd_server(db) } shiny::shinyApp( ui = ui, server = server ) ``` ]] .rightcol[ <br><br><br><br> The `sd_db_connect()` function uses the `.env` file to make the database connection. ] --- class: center ## Use `sdApps::sd_dashboard()` to locally view data <center> <img src="images/dashboard_1.png" width=80%> </center> --- class: inverse
−
+
10
:
00
## Your turn - Create a Supabase account and database. - Run `surveydown::sd_db_config()` in your console to store your Supabase credentials. - Run your survey locally, answer questions to generate data. - View your response data with `sdApps::sd_dashboard()` --- class: inverse, middle # .fancy[Data Handling] ### 1. Storing data ### 2. .orange[Fetching data] --- # Static Data Fetching Once your database is properly set up, you can fetch the data using: ``` r db <- sd_db_connect() data <- sd_get_data(db) ``` Or simply: ``` r data <- sd_get_data(sd_db_connect()) ``` --- ## Reactive Data Fetching You can also reactively fetch the data live inside the survey In `app.R`: .leftcol60[ ``` r db <- sd_db_connect() server <- function(input, output, session) { * data <- sd_get_data(db, refresh_interval = 5) sd_server() } ``` ] --- ## Reactive Data Fetching Use the reactive data to create some output .leftcol55[ In `app.R`: ``` r server <- function(input, output, session) { data <- sd_get_data(db, refresh_interval = 5) output$my_plot <- renderPlot({ * my_data <- data() # insert code here to make a plot }) sd_server() } ``` ] .rightcol45[ In `survey.qmd`: ```` markdown ```{r} plotOutput("my_plot") ``` ```` ] --- class: inverse
−
+
10
:
00
## Your turn - Use `sd_get_data(db)` to read in a copy of your survey response data. - Edit your `app.R` file to reactively access your survey data. - Use your data to make a plot about your data. - Display your plot in your `survey.qmd` file with `plotOutput("my_plot")`