From 6016547f1fd78cc31a1c2ca4414c25dbe8c784ed Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Wed, 15 Feb 2023 17:02:10 +0100 Subject: [PATCH] support for dplyr 1.1.0 --- DESCRIPTION | 4 ++-- NEWS.md | 2 +- R/aa_helper_functions.R | 40 +++++++++++++++++++++------------------- R/first_isolate.R | 4 ++-- R/key_antimicrobials.R | 4 ++-- R/mdro.R | 2 +- pkgdown/extra.css | 5 ++++- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ea8a2c15..319305c4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.8.2.9126 -Date: 2023-02-14 +Version: 1.8.2.9127 +Date: 2023-02-15 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by diff --git a/NEWS.md b/NEWS.md index 686b6aaf..09cb4148 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.8.2.9126 +# 1.8.2.9127 *(this beta version will eventually become v2.0! We're happy to reach a new major milestone soon!)* diff --git a/R/aa_helper_functions.R b/R/aa_helper_functions.R index 76771a1b..c01fe821 100755 --- a/R/aa_helper_functions.R +++ b/R/aa_helper_functions.R @@ -889,34 +889,36 @@ get_current_data <- function(arg_name, call) { valid_df <- function(x) { !is.null(x) && is.data.frame(x) } - # try dplyr::cur_data_all() first to support dplyr groups - # only useful for e.g. dplyr::filter(), dplyr::mutate() and dplyr::summarise() - # not useful (throws error) with e.g. dplyr::select(), dplyr::across(), or dplyr::vars(), - # but that will be caught later on in this function - cur_data_all <- import_fn("cur_data_all", "dplyr", error_on_fail = FALSE) - if (!is.null(cur_data_all)) { - out <- tryCatch(cur_data_all(), error = function(e) NULL) - if (valid_df(out)) { - return(out) - } - } # try a manual (base R) method, by going over all underlying environments with sys.frames() for (env in sys.frames()) { - if (!is.null(env$`.Generic`)) { + + # dplyr support ---- + if (!is.null(env$mask) && is.function(env$mask$current_rows) && (valid_df(env$data) || valid_df(env$`.data`))) { + # an element `.data` or `data` (containing all data) and `mask` (containing functions) will be in the environment when using dplyr verbs + # we use their mask$current_rows() to get the group rows, since dplyr::cur_data_all() is deprecated and will be removed in the future + # e.g. for `example_isolates %>% group_by(ward) %>% mutate(first = first_isolate(.))` + if (valid_df(env$data)) { + # support for dplyr 1.1.x + return(env$data[env$mask$current_rows(), , drop = FALSE]) + } else { + # support for dplyr 1.0.x + return(env$`.data`[env$mask$current_rows(), , drop = FALSE]) + } + + # base R support ---- + } else if (!is.null(env$`.Generic`)) { # don't check `".Generic" %in% names(env)`, because in R < 3.2, `names(env)` is always NULL - if (valid_df(env$`.data`)) { - # an element `.data` will be in the environment when using `dplyr::select()` - # (but not when using `dplyr::filter()`, `dplyr::mutate()` or `dplyr::summarise()`) - return(env$`.data`) - } else if (valid_df(env$xx)) { - # an element `xx` will be in the environment for rows + cols, e.g. `example_isolates[c(1:3), carbapenems()]` + if (valid_df(env$xx)) { + # an element `xx` will be in the environment for rows + cols in base R, e.g. `example_isolates[c(1:3), carbapenems()]` return(env$xx) } else if (valid_df(env$x)) { - # an element `x` will be in the environment for only cols, e.g. `example_isolates[, carbapenems()]` + # an element `x` will be in the environment for only cols in base R, e.g. `example_isolates[, carbapenems()]` return(env$x) } + + # scoped dplyr support ---- } else if (!is.null(names(env)) && all(c(".tbl", ".vars", ".cols") %in% names(env), na.rm = TRUE) && valid_df(env$`.tbl`)) { # an element `.tbl` will be in the environment when using scoped dplyr variants, with or without `dplyr::vars()` # (e.g. `dplyr::summarise_at()` or `dplyr::mutate_at()`) diff --git a/R/first_isolate.R b/R/first_isolate.R index a2967f7a..3fb64b2e 100755 --- a/R/first_isolate.R +++ b/R/first_isolate.R @@ -177,7 +177,7 @@ first_isolate <- function(x = NULL, include_untested_sir = TRUE, ...) { if (is_null_or_grouped_tbl(x)) { - # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) + # when `x` is left blank, auto determine it (get_current_data() searches underlying data within call) # is also fix for using a grouped df as input (a dot as first argument) x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } @@ -634,7 +634,7 @@ filter_first_isolate <- function(x = NULL, method = c("phenotype-based", "episode-based", "patient-based", "isolate-based"), ...) { if (is_null_or_grouped_tbl(x)) { - # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) + # when `x` is left blank, auto determine it (get_current_data() searches underlying data within call) # is also fix for using a grouped df as input (a dot as first argument) x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } diff --git a/R/key_antimicrobials.R b/R/key_antimicrobials.R index 875c5e65..b62a1e1a 100755 --- a/R/key_antimicrobials.R +++ b/R/key_antimicrobials.R @@ -138,7 +138,7 @@ key_antimicrobials <- function(x = NULL, only_sir_columns = FALSE, ...) { if (is_null_or_grouped_tbl(x)) { - # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) + # when `x` is left blank, auto determine it (get_current_data() searches underlying data within call) # is also fix for using a grouped df as input (a dot as first argument) x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } @@ -250,7 +250,7 @@ all_antimicrobials <- function(x = NULL, only_sir_columns = FALSE, ...) { if (is_null_or_grouped_tbl(x)) { - # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) + # when `x` is left blank, auto determine it (get_current_data() searches underlying data within call) # is also fix for using a grouped df as input (a dot as first argument) x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } diff --git a/R/mdro.R b/R/mdro.R index 65f07697..bd4d15c6 100755 --- a/R/mdro.R +++ b/R/mdro.R @@ -178,7 +178,7 @@ mdro <- function(x = NULL, only_sir_columns = FALSE, ...) { if (is_null_or_grouped_tbl(x)) { - # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) + # when `x` is left blank, auto determine it (get_current_data() searches underlying data within call) # is also a fix for using a grouped df as input (i.e., a dot as first argument) x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } diff --git a/pkgdown/extra.css b/pkgdown/extra.css index 7434c900..5628aeca 100644 --- a/pkgdown/extra.css +++ b/pkgdown/extra.css @@ -54,10 +54,13 @@ mark, .mark { background: rgba(17, 143, 118, 0.25) !important; } +/* smaller tables */ +.table { + font-size: 0.9em !important; +} /* SYNTAX */ /* These are simple changes for the syntax highlighting */ - pre { font-size: 0.8em !important; }