Skip to content

Commit

Permalink
add reactive argument to sd_get_data()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Sep 26, 2024
1 parent c31ddaf commit 4d469e0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# surveydown (development version)

- Add `reactive` logical argument to `sd_get_data()` so that data can be reactively fetched in the server or not according to user control. Defaults to `FALSE`.
- Modified messaging from `sd_set_password()` to not print out user's password and provide clearer instructions.

# surveydown 0.3.0

- Introduced `sd_ui()` function to set placeholders for the shiny app ui.
Expand Down
8 changes: 4 additions & 4 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ sd_database <- function(
#' \item db: A DBI database connection object
#' \item table: A string specifying the name of the table to query
#' }
#' @param reactive Logical. If `TRUE`, will return a reactive expression to obtain the latest data continuously refreshed according to the `refresh_interval`. Defaults to `FALSE`.
#' @param 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.
#'
Expand All @@ -152,7 +153,7 @@ sd_database <- function(
#' })
#' }
#' }
sd_get_data <- function(db, refresh_interval = 5) {
sd_get_data <- function(db, reactive = FALSE, refresh_interval = 5) {
if (is.null(db)) {
warning("Database is not connected, db is NULL")
return(NULL)
Expand All @@ -162,14 +163,13 @@ sd_get_data <- function(db, refresh_interval = 5) {
DBI::dbReadTable(conn, db$table)
})
}
if (!is.null(shiny::getDefaultReactiveDomain())) {
# In a reactive context
if (reactive) {
return(shiny::reactive({
shiny::invalidateLater(refresh_interval * 1000)
fetch_data()
}))
} else {
# If not in a reactive context, just return the data
# If not in a reactive context, just return the data once
return(fetch_data())
}
}
Expand Down

0 comments on commit 4d469e0

Please sign in to comment.