From ef062a4db528781a6abffda75d1c120953a6f868 Mon Sep 17 00:00:00 2001 From: RossanaTat Date: Tue, 18 Jun 2024 11:41:29 -0400 Subject: [PATCH] remove unmaksing --- NAMESPACE | 1 - R/utils.R | 180 -------------------------------------- man/detach_package.Rd | 19 ---- man/unmask_joyn.Rd | 38 -------- man/unmask_joyn_fun_ns.Rd | 20 ----- 5 files changed, 258 deletions(-) delete mode 100644 man/detach_package.Rd delete mode 100644 man/unmask_joyn.Rd delete mode 100644 man/unmask_joyn_fun_ns.Rd diff --git a/NAMESPACE b/NAMESPACE index ad1c9092..984bc0ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,7 +16,6 @@ export(possible_ids) export(rename_to_valid) export(right_join) export(set_joyn_options) -export(unmask_joyn) import(collapse, except = fdroplevels) import(data.table, except = fdroplevels) importFrom(lifecycle,deprecated) diff --git a/R/utils.R b/R/utils.R index d97d2046..544ff09f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -129,186 +129,6 @@ is_balanced <- function(df, } -#' Unmask joyn function (namespace version) -#' -#' @param fun_name character of function to unmask -#' @param pkg_name character specifying package from which joyn masks the function -#' -#' @return invisibly unmask function that joyn masks -#' @keywords internal -unmask_joyn_fun_ns <- function(fun_name, - pkg_name) { - - # Checks #### - - # if package {pkg_name} is not loaded, stop and inform user - if (!pkg_name %in% tolower(.packages())) { - - store_joyn_msg(err = "package {.strong {pkg_name}} must be loaded.") - - joyn_msg("err") - cli::cli_abort("{pkg_name} is not loaded") - } - - # if function {fun_name} is not an exported object of {pkg_name}, stop and inform user - - if (!any(fun_name %in% getNamespaceExports(pkg_name))) { - - store_joyn_msg(err = " {.strong {fun_name}} must be exported object(s) of {.strong {pkg_name}}." - ) - - joyn_msg("err") - cli::cli_abort("{fun_name} not exported from {pkg_name}") - } - - # get joyn namespace - joyn_ns <- getNamespace("joyn") - - # unlock binding - unlockBinding(fun_name, env = joyn_ns) - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # Unmask functions ---- - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - # get namespace exports - joyn_ns_exports_names <- getNamespaceExports(joyn_ns) - - # get functions to unmask -filter those that are in joyn_ns exports - fun_name <- fun_name[fun_name %in% joyn_ns_exports_names] - - # get joyn's namespace exports' environment - joyn_ns_exports <- .getNamespaceInfo(joyn_ns, - "exports") - - # remove binding from joyn's namespace exports' environment - remove(list = fun_name, - envir = joyn_ns_exports) - - joyn_ns <- getNamespace("joyn") - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # Apply the new mask ---- - #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - new_mask <- getExportedValue(ns = getNamespace(pkg_name), name = fun_name) - - rlang::env_poke(env = joyn_ns, - nm = fun_name, - value = new_mask, - create = TRUE) - - - # Lock the binding again - lockEnvironment(joyn_ns, - bindings = TRUE) - - # Detach and reattach "joyn" if currently loaded - - # if(anyv(search(), "package:joyn")) { #anyv is the collapse version of any() - # detach_package("joyn") - # suppressPackageStartupMessages(attachNamespace(joyn_ns)) - # } - - # return - invisible(TRUE) - -} - -#' Unmask joyn functions -#' -#' @param fun_name character vector of one or more functions to unmask -#' @param pkg_name character specifying package from which joyn masks the function(s) -#' -#' @return invisible list -#' @export -#' @examples -#' \dontrun{ -#' library(dplyr) -#' library(joyn) -#' -#' left_join( -#' data.frame(x = 1:3, y = 1:3), -#' data.frame(x = 1:2, z = 1:2), -#' by = "x" -#' ) -#' unmask_joyn("left_join") -#' -#' left_join( -#' data.frame(x = 1:3, y = 1:3), -#' data.frame(x = 1:2, z = 1:2), -#' by = "x" -#' ) -#' } -unmask_joyn <- \(fun_name, - pkg_name = "dplyr") { - - fun_name <- match.arg(arg = fun_name, - choices = c("right_join", "left_join", "inner_join", "full_join"), - several.ok = TRUE) - - l <- lapply(fun_name, \(.) { - - tryCatch( - expr = { - # unmask joyn function - unmask_joyn_fun_ns(., pkg_name) - "unmasked" - }, - - error = function(e) { - paste("Error:", e$message) - }, # end of error section - - warning = function(w) { - paste("Warning:",w$message) - } - ) # End of tryCatch - } - ) - - joyn_ns <- getNamespace("joyn") - - if(anyv(search(), "package:joyn")) { #anyv is the collapse version of any() - detach_package("joyn") - suppressPackageStartupMessages(attachNamespace(joyn_ns)) - } - - names(l) <- fun_name - - clear_joynenv() - - # Inform the user - store_joyn_msg(info = "function {.strong {fun_name}} unmasked. {.strong {pkg_name}::{fun_name}} preferred") - - joyn_msg() - - return(invisible(l)) - -} - - -#' Detach a package from search list -#' -#' This is an auxiliary function to avoid errors when detaching a package -#' -e.g., when multiple versions are loaded at the same time -#' -#' @param pkg_name name of the package to detach -#' @return invisibly detach package from search list -#' @keywords internal -#' -detach_package <- function(pkg_name) { - - search_item <- paste("package", pkg_name, sep = ":") - - if (search_item %in% search()) { - - detach(search_item, - unload = TRUE, - character.only = TRUE) - - } -} diff --git a/man/detach_package.Rd b/man/detach_package.Rd deleted file mode 100644 index 2d403b8a..00000000 --- a/man/detach_package.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{detach_package} -\alias{detach_package} -\title{Detach a package from search list} -\usage{ -detach_package(pkg_name) -} -\arguments{ -\item{pkg_name}{name of the package to detach} -} -\value{ -invisibly detach package from search list -} -\description{ -This is an auxiliary function to avoid errors when detaching a package --e.g., when multiple versions are loaded at the same time -} -\keyword{internal} diff --git a/man/unmask_joyn.Rd b/man/unmask_joyn.Rd deleted file mode 100644 index 69abfbe9..00000000 --- a/man/unmask_joyn.Rd +++ /dev/null @@ -1,38 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{unmask_joyn} -\alias{unmask_joyn} -\title{Unmask joyn functions} -\usage{ -unmask_joyn(fun_name, pkg_name = "dplyr") -} -\arguments{ -\item{fun_name}{character vector of one or more functions to unmask} - -\item{pkg_name}{character specifying package from which joyn masks the function(s)} -} -\value{ -invisible list -} -\description{ -Unmask joyn functions -} -\examples{ -\dontrun{ -library(dplyr) -library(joyn) - -left_join( - data.frame(x = 1:3, y = 1:3), - data.frame(x = 1:2, z = 1:2), - by = "x" -) -unmask_joyn("left_join") - -left_join( - data.frame(x = 1:3, y = 1:3), - data.frame(x = 1:2, z = 1:2), - by = "x" -) -} -} diff --git a/man/unmask_joyn_fun_ns.Rd b/man/unmask_joyn_fun_ns.Rd deleted file mode 100644 index 5d663da8..00000000 --- a/man/unmask_joyn_fun_ns.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{unmask_joyn_fun_ns} -\alias{unmask_joyn_fun_ns} -\title{Unmask joyn function (namespace version)} -\usage{ -unmask_joyn_fun_ns(fun_name, pkg_name) -} -\arguments{ -\item{fun_name}{character of function to unmask} - -\item{pkg_name}{character specifying package from which joyn masks the function} -} -\value{ -invisibly unmask function that joyn masks -} -\description{ -Unmask joyn function (namespace version) -} -\keyword{internal}