Extends 'cohortBuilder' with a source layer for database tables. Cohort filtering steps are translated to 'dbplyr' queries and executed on the database server, so no data needs to be pulled into R. Supports the 'shinyCohortBuilder' graphical interface and offers configurable strategies for how intermediate step results are passed between filtering steps.

cohortBuilder.db is database layer for
cohortBuilder and
shinyCohortBuilder.
The package allows you to operate with cohortBuilder API on remote
tables stores within a database.
All of the operations are performed directly on the database server side!
# CRAN version
install.packages("cohortBuilder.db")
# Latest development version
remotes::install_github("https://github.com/r-world-devs/cohortBuilder.db")
cohortBuilder.db allows you to use cohortBuilder and
shinyCohortBuilder features working with database connection.
Comparing to the standard workflow (working on list of tables) you just
need to create database tables configuration using dbtables where you
provide database connection, schema name and table names. Than pass the
created object to set_source method:
library(cohortBuilder)
library(cohortBuilder.db)
db_tables_conf <- dbtables(
connection = <db-connection>,
schema = <schema-name>,
tables = <vector-of-table-names-to-use>
)
librarian_db_source <- set_source(
db_tables_conf
)
and configure Cohort using the same rules known from cohortBuilder:
librarian_db_cohort <- cohort(
librarian_source,
filter(
"discrete", id = "author", dataset = "books",
variable = "author", value = "Dan Brown"
),
filter(
"range", id = "copies", dataset = "books",
variable = "copies", range = c(5, 10)
),
filter(
"date_range", id = "registered", dataset = "borrowers",
variable = "registered", range = c(as.Date("2010-01-01"), Inf)
)
)
Now feel free to use all the known cohortBuilder features.
You can also use shinyCohortBuilder to use cohortBuilder features in
shiny. Just use db-based Cohort with the known approach:
library(shiny)
library(shinyCohortBuilder)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
cb_ui("librarian")
),
mainPanel(
verbatimTextOutput("cohort_data")
)
)
)
server <- function(input, output, session) {
cb_server("librarian", librarian_db_cohort)
output$cohort_data <- renderPrint({
input[["librarian-data-updated"]]
get_data(librarian_cohort)
})
}
shinyApp(ui, server)
Each filtering step can either stay a lazy dbplyr query or be written
to a temporary table before the next step reads it. The behaviour is
controlled with a single option:
options(cohortBuilder.db.step_strategy = "lazy") # default, nothing is materialized
options(cohortBuilder.db.step_strategy = "materialize") # every table computed in every step
options(cohortBuilder.db.step_strategy = "materialize_selective") # only tables touched by the step
See vignette("step-strategy", package = "cohortBuilder.db") for when
to pick each one.
Special thanks to:
In a case you found any bugs, have feature request or general question please file an issue at the package Github. You may also contact the package author directly via email at [email protected].