Skip to content

Commit

Permalink
add post_where
Browse files Browse the repository at this point in the history
  • Loading branch information
msberends committed Nov 18, 2024
1 parent 343f845 commit 5e0fd8d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: certedb
Title: A Certe R Package for Connecting to Databases
Version: 1.12.5
Version: 1.12.6
Authors@R: c(
person(given = c("Matthijs", "S."),
family = "Berends",
Expand Down
20 changes: 18 additions & 2 deletions R/get_diver_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' Thes functions can be used to download local or remote database data, e.g. Spectre data from DiveLine on a Diver server (from [Dimensional Insight](https://www.dimins.com)). The [get_diver_data()] function sets up an ODBC connection (using [db_connect()]), which requires their quite limited [DI-ODBC driver](https://www.dimins.com/online-help/workbench_help/Content/ODBC/di-odbc.html).
#' @param date_range date range, can be length 1 or 2 (or more to use the min/max) to filter on the column specified in the YAML file, see [presets]. Defaults to [this_year()]. Use `NULL` to set no date filter. Can also be years, or functions such as [`last_month()`][certetoolbox::last_month()]. Date-time ojects will be converted to dates, so using times as input is useless. It is supported to filter on a date-time column though.
#' @param where arguments to filter data on, will be passed on to [`filter()`][dplyr::filter()]. **Do not use `&&` or `||` but only `&` or `|` in filtering.**
#' @param where,post_where arguments to filter data on, will be passed on to [`filter()`][dplyr::filter()]. **Do not use `&&` or `||` but only `&` or `|` in filtering.** The `post_where` will be run after all downloading and post-processing, but before the auto-transform.
#' @param diver_cbase,diver_project,diver_dsn,diver_testserver properties to set in [db_connect()]. The `diver_cbase` argument will be based on `preset`, but can also be set to blank `NULL` to manually select a cBase in a popup window.
#' @param diver_tablename name of the database table to download data from. This is hard-coded by DI and should normally never be changed.
#' @param review_qry a [logical] to indicate whether the query must be reviewed first, defaults to `TRUE` in interactive mode and `FALSE` otherwise. This will always be `FALSE` in Quarto / R Markdown, since the output of [knitr::pandoc_to()] must be `NULL`.
Expand Down Expand Up @@ -111,6 +111,7 @@
#' }
get_diver_data <- function(date_range = this_year(),
where = NULL,
post_where = NULL,
review_qry = interactive(),
antibiogram_type = "sir",
distinct = TRUE,
Expand Down Expand Up @@ -284,7 +285,7 @@ get_diver_data <- function(date_range = this_year(),
# set query ----
if (!is.null(tryCatch(where, error = function(e) 0))) {
msg_init("Validating WHERE statement...", print = info)
# fill in columns from the 'di' object
# fill in columns from the 'di' or 'gl' object
if (isTRUE(list(...)$where_as_character)) {
where <- str2lang(where)
}
Expand Down Expand Up @@ -555,6 +556,21 @@ get_diver_data <- function(date_range = this_year(),
}
}

# post WHERE ----
if (!is.null(tryCatch(post_where, error = function(e) 0))) {
msg_init("Validating post-WHERE statement...", print = info, prefix_time = TRUE)
# fill in columns from the 'di' or 'gl' object
post_where <- where_convert_di_gl(substitute(post_where))
# convert objects, this will return msg "OK"
post_where <- where_convert_objects(deparse(substitute(post_where)), info = info)
out_new <- out |> filter(!!post_where)
if (nrow(out_new) < nrow(out)) {
msg_init("Removing ", nrow(out) - nrow(out_new), " rows from ", font_blue("post_where"), "...", print = info, prefix_time = TRUE)
out <- out_new
msg_ok(dimensions = dim(out), print = info)
}
}

# only real patients ----
if (isTRUE(only_real_patients)) {
out_new <- out
Expand Down
6 changes: 4 additions & 2 deletions R/presets.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#' cbase: "location/to/another.cbase"
#' by: ColumnName1, ColumnName2
#' type: "left"
#' filter: ColumnName1 == "abc"
#' select: ColumnName1, col_name_2 = ColumnName2, ColumnName3, everything(), !starts_with("abc")
#' wide_names_from: ColumnName3
#' join2:
Expand All @@ -70,8 +71,9 @@
#'
#' After this, the arguments in [get_diver_data()] will run:
#'
#' 5. Distinct if `distinct = TRUE` using [`distinct()`][dplyr::distinct()]
#' 6. Auto-transform if `autotransform = TRUE` using [`auto_transform()`][certetoolbox::auto_transform()]
#' 5. Post-WHERE if `post_where` is set, using [`filter()`][dplyr::filter()]
#' 6. Distinct if `distinct = TRUE`, using [`distinct()`][dplyr::distinct()]
#' 7. Auto-transform if `autotransform = TRUE`, using [`auto_transform()`][certetoolbox::auto_transform()]
#'
#' ## cBase (`cbase`)
#'
Expand Down
3 changes: 2 additions & 1 deletion man/get_diver_data.Rd

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

6 changes: 4 additions & 2 deletions man/presets.Rd

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

0 comments on commit 5e0fd8d

Please sign in to comment.