From c7b4eae9b950d350b3f05aac094da5462f4099e6 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Fri, 13 Dec 2024 15:54:26 +1100 Subject: [PATCH] use a function to drop env --- NAMESPACE | 2 ++ R/methods.R | 16 ++++++++-------- R/methods_SE.R | 7 +++---- R/utilities.R | 28 ++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 7c9a52af..01ec53b3 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -155,6 +155,7 @@ importFrom(rlang,enquo) importFrom(rlang,enquos) importFrom(rlang,flatten_if) importFrom(rlang,inform) +importFrom(rlang,is_quosure) importFrom(rlang,is_spliced) importFrom(rlang,quo) importFrom(rlang,quo_is_missing) @@ -162,6 +163,7 @@ importFrom(rlang,quo_is_null) importFrom(rlang,quo_is_symbol) importFrom(rlang,quo_is_symbolic) importFrom(rlang,quo_name) +importFrom(rlang,quo_set_env) importFrom(rlang,quo_squash) importFrom(rlang,set_names) importFrom(scales,extended_breaks) diff --git a/R/methods.R b/R/methods.R index c79c3d9f..03dac22d 100755 --- a/R/methods.R +++ b/R/methods.R @@ -477,10 +477,10 @@ setGeneric("scale_abundance", function(.data, # Attach column internals add_tt_columns( - !!.sample, - !!.transcript, - !!.abundance, - !!(function(x, v) enquo(v))(x,!!value_scaled) + !!(.sample |> drop_enquo_env()), + !!(.transcript |> drop_enquo_env()), + !!(.abundance |> drop_enquo_env()), + !!(((function(x, v) enquo(v))(x,!!value_scaled)) |> drop_enquo_env()) ) @@ -698,10 +698,10 @@ setGeneric("quantile_normalise_abundance", function(.data, # Attach column internals add_tt_columns( - !!.sample, - !!.transcript, - !!.abundance, - !!(function(x, v) enquo(v))(x,!!value_scaled) + !!(.sample |> drop_enquo_env()), + !!(.transcript |> drop_enquo_env()), + !!(.abundance |> drop_enquo_env()), + !!(((function(x, v) enquo(v))(x,!!value_scaled)) |> drop_enquo_env()) ) diff --git a/R/methods_SE.R b/R/methods_SE.R index f3e8d1f5..1fe580e4 100755 --- a/R/methods_SE.R +++ b/R/methods_SE.R @@ -196,7 +196,7 @@ setMethod("tidybulk", "RangedSummarizedExperiment", .tidybulk_se) memorise_methods_used(c("edger", "tmm")) %>% # Attach column internals - add_tt_columns(.abundance_scaled = !!(function(x, v) enquo(v))(x,!!as.symbol(value_scaled))) + add_tt_columns(.abundance_scaled = !!(((function(x, v) enquo(v))(x,!!as.symbol(value_scaled))) |> drop_enquo_env()) ) } @@ -321,7 +321,7 @@ setMethod("scale_abundance", memorise_methods_used(c("quantile")) %>% # Attach column internals - add_tt_columns(.abundance_scaled = !!(function(x, v) enquo(v))(x,!!as.symbol(value_scaled))) + add_tt_columns(.abundance_scaled = !!(((function(x, v) enquo(v))(x,!!as.symbol(value_scaled))) |> drop_enquo_env()) ) } @@ -953,8 +953,7 @@ setMethod("remove_redundancy", memorise_methods_used("sva") %>% # Attach column internals - add_tt_columns(.abundance_adjusted = !!(function(x, v) - enquo(v))(x, !!as.symbol(value_adjusted))) + add_tt_columns(.abundance_adjusted = !!(((function(x, v) enquo(v))(x,!!as.symbol(value_adjusted))) |> drop_enquo_env()) ) } diff --git a/R/utilities.R b/R/utilities.R index 3f1dc2bb..c0f9f4e1 100755 --- a/R/utilities.R +++ b/R/utilities.R @@ -1514,3 +1514,31 @@ check_and_install_packages <- function(packages) { ) } } + +#' Drop Environment from a Quosure +#' +#' Takes a quosure and resets its environment to `emptyenv()` without altering +#' its expression. +#' +#' @param q A quosure object to have its environment stripped. +#' @return A quosure with the same expression but environment set to `emptyenv()`. +#' +#' @importFrom rlang is_quosure +#' @importFrom rlang quo_set_env +#' +#' @examples +#' library(rlang) +#' +#' q <- quo(x + y) +#' environment(q) +#' +#' q_stripped <- drop_enquo_env(q) +#' identical(quo_get_env(q_stripped), emptyenv()) # TRUE +#' +#' @noRd +drop_enquo_env <- function(q) { + if (!rlang::is_quosure(q)) { + stop("`q` must be a quosure.") + } + rlang::quo_set_env(q, emptyenv()) +}