Skip to content

Commit

Permalink
Merge pull request #319 from stemangiola/drop_env_for_enquo
Browse files Browse the repository at this point in the history
use a function to drop env
  • Loading branch information
stemangiola authored Dec 13, 2024
2 parents d310912 + c7b4eae commit f58b1e3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ 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)
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)
Expand Down
16 changes: 8 additions & 8 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
)


Expand Down Expand Up @@ -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())
)


Expand Down
7 changes: 3 additions & 4 deletions R/methods_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()) )

}

Expand Down Expand Up @@ -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()) )

}

Expand Down Expand Up @@ -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()) )

}

Expand Down
28 changes: 28 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit f58b1e3

Please sign in to comment.