<- sd_database(
db host = "my_host",
dbname = "my_dbname",
port = "my_port",
user = "my_user",
table = "my_table"
)
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:
You can fetch the data like this:
<- sd_get_data(db) data
Here the data
object will be a data.frame with the data from your database.
Reactive Fetching
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 - just call it inside the server()
function in your app.R
file:
<- function(input, output, session) {
server
<- sd_get_data(db)
data
sd_server()
}
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:
<- function(input, output, session) {
server
<- sd_get_data(db, refresh_interval = 5)
data
$my_plot <- renderPlot({
output<- data()
my_data # insert code here to make a plot with my_data
})
sd_server()
}
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 somewhere in your survey.qmd
file.
See the Reactivity page for more information on how to use reactivity in your survey.