This function retrieves all data from a specified table in a database. It automatically detects whether it's being used in a reactive context (e.g., within a Shiny application) and behaves accordingly. In a reactive context, it returns a reactive expression that automatically refreshes the data at specified intervals.
Arguments
- db
A list containing database connection details. Must have elements:
db: A DBI database connection object
table: A string specifying the name of the table to query
- refresh_interval
Numeric. The time interval (in seconds) between data refreshes when in a reactive context. Default is
5
seconds. Ignored in non-reactive contexts.
Value
In a non-reactive context, returns a data frame containing all rows and columns from the specified table. In a reactive context, returns a reactive expression that, when called, returns the most recent data from the specified database table.
Examples
if (FALSE) { # \dontrun{
# Non-reactive context
db <- list(db = DBI::dbConnect(...), table = "my_table")
data <- sd_get_data(db)
# Reactive context (inside a Shiny server function)
server <- function(input, output, session) {
data <- sd_get_data(db, refresh_interval = 10)
output$table <- renderTable({
data() # Note the parentheses to retrieve the reactive value
})
}
} # }