Fetching Data

Static Fetching

Once you have your database properly set up and your survey is live, you can fetch the data using the sd_get_data() function. All you have to do is pass in the db object you created with sd_database() to the sd_get_data() function.

For example, if you have a table called my_table in your Supabase database:

# In your setup chunk
db <- sd_database(
  host   = "my_host",
  dbname = "my_dbname",
  port   = "my_port",
  user   = "my_user",
  table  = "my_table"
)

You can fetch the data like this:

# In your server chunk or server.R
data <- sd_get_data(db)

Here the data object will be a data.frame with the data from your database.

Reactive Fetching

Tip

Run the Demo survey of a reactive plot for an example of reactively fetching data.

The above example is intended for an analysis context, where all you want to do is fetch the latest data from your database. However, you may also want to fetch the data in a reactive context (i.e. in your survey). For example, perhaps you want to show respondents a bar chart of the most common responses to a question. The sd_get_data() function is compatible with reactive fetching as well:

# Same as static fetching
data <- sd_get_data(db)

Now the data object will be a reactive expression, not a data.frame.

To use it, you have to call it with () to get the latest data as a data.frame. For example:

data <- sd_get_data(db, refresh_interval = 5)

output$my_plot <- renderPlot({  
  my_data <- data()
  # insert code here to make a plot with my_data
})

Here the my_data object will be a data.frame with the latest data from your database, refreshed every 5 sections according to the refresh_interval parameter (5 seconds is the default value), which you could then use to make a plot.

In this example, the output$my_plot object will be a plot, which you can then display in your survey by placing plotOutput("my_plot") in a code chunk (or inline code) somewhere in your survey.

See the Reactivity page for more information on how to use reactivity in your survey.

Back to top