Skip to content

Commit

Permalink
Expose get_db function
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveViss committed Oct 15, 2024
1 parent 851eb5f commit f26411b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
24 changes: 24 additions & 0 deletions R/db_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,27 @@ delete_entry <- function(con = NULL, tbl = NULL, ...) {
get_tbl <- function(con = NULL, tbl = NULL) {
DBI::dbReadTable(con, tbl)
}

#' Retrieve joined data from the database
#'
#' This function retrieves data by performing a series of `INNER JOIN` operations across multiple tables in the database,
#' including `lab_measurement`, `analyte`, `lab_sample`, `lab_field_sample`, `field_sample`, `sites`, and `species`.
#' The result is a combined dataset containing related information from all these tables.
#'
#' @param con A DBI connection object. The database connection used to query the data.
#'
#' @return A lazy data frame (tibble) representing the joined data. The query is not executed until explicitly requested by the user (e.g., with `dplyr::collect()`).
#'
#' @export
get_db <- function(con = NULL) {
dplyr::tbl(
con, dplyr::sql("
SELECT * FROM lab_measurement
INNER JOIN analyte USING (id_analyte)
INNER JOIN lab_sample USING (id_lab_sample)
INNER JOIN lab_field_sample USING (id_lab_sample)
INNER JOIN field_sample USING (id_field_sample)
INNER JOIN sites USING (id_site)
INNER JOIN species USING (id_species)")
)
}
16 changes: 13 additions & 3 deletions man/get_db.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f26411b

Please sign in to comment.