From 81a3886d7c02eb71a4a6398cf14a39c5973aa391 Mon Sep 17 00:00:00 2001 From: RossanaTat Date: Thu, 25 Apr 2024 12:48:31 -0400 Subject: [PATCH] unmask_joyn function --- NAMESPACE | 1 + R/utils.R | 167 ++++++++++---------------------------- _pkgdown.yml | 1 + man/detach_package.Rd | 4 +- man/unmask_joyn.Rd | 19 +++++ man/unmask_joyn_fun.Rd | 35 -------- man/unmask_joyn_fun_ns.Rd | 6 +- 7 files changed, 70 insertions(+), 163 deletions(-) create mode 100644 man/unmask_joyn.Rd delete mode 100644 man/unmask_joyn_fun.Rd diff --git a/NAMESPACE b/NAMESPACE index 1f61bca5..5f4e79f7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ 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 b175574e..a55d8f03 100644 --- a/R/utils.R +++ b/R/utils.R @@ -128,119 +128,13 @@ is_balanced <- function(df, } -#' Unmask joyn's functions ({conflicted} version) -#' -#' -#' @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 TRUE -#' @keywords internal -#' -#' -# unmask_joyn_fun_conflicted <- function(fun_name, -# pkg_name) { -# -# clear_joynenv() -# -# lapply(fun_name, -# conflicted::conflict_prefer, -# winner = pkg_name, -# loser = "joyn", -# quiet = TRUE) -# -# store_msg(type = "info", -# ok = paste(cli::symbol$info, " Note: "), -# pale = "function", -# bolded_pale = " {fun_name}", -# pale = " unmasked.", -# bolded_pale = " {pkg_name}::{fun_name}", -# pale = " preferred") -# joyn_msg() -# -# invisible(TRUE) -# } - - -#' Unmask joyn's function(s) (local version, vectorized) -#' -#' -#' @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 TRUE -#' @keywords internal -to be replaced with export once done -unmask_joyn_fun <- function(fun_name, - pkg_name) { - - # if package {pkg_name} is not loaded, stop and inform user - - if (!pkg_name %in% tolower(.packages())) { - - store_msg(type = "err", - err = paste(cli::symbol$cross, "Error:"), - pale = " package {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_msg(type = "err", - err = paste(cli::symbol$cross, "Error:"), - pale = " {fun_name} must be exported object(s) of {pkg_name}." - ) - - joyn_msg("err") - cli::cli_abort("{fun_name} not exported from {pkg_name}") - } - - # get namespace exports - joyn_ns_exports <- getNamespaceExports("joyn") - - # get functions to unmask -filter those that are in joyn_ns exports - fun_name <- fun_name[fun_name %in% joyn_ns_exports] - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # apply the new mask ---- - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - lapply(fun_name, function(fn) { - - new_mask <- getExportedValue(ns = getNamespace(pkg_name), - name = fn) - - assign(x = fn, - value = new_mask, - envir = .GlobalEnv) - - }) - - #Inform the user - clear_joynenv() - - store_msg(type = "info", - ok = paste(cli::symbol$info, " Note: "), - pale = "function", - bolded_pale = " {fun_name}", - pale = " unmasked.", - bolded_pale = " {pkg_name}::{fun_name}", - pale = " preferred") - - joyn_msg("info") - - invisible(TRUE) -} - - -#' Unmask joyn function (namespace version, NOT vectorized) +#' 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 from another package +#' @return invisibly unmask function that joyn masks #' @keywords internal unmask_joyn_fun_ns <- function(fun_name, pkg_name) { @@ -292,20 +186,12 @@ unmask_joyn_fun_ns <- function(fun_name, joyn_ns_exports <- .getNamespaceInfo(joyn_ns, "exports") - # debug lines to be removed ---- - # print("EXPORTS BEFORE") - # print(getNamespaceExports(joyn_ns)) - # remove binding from joyn's namespace exports' environment remove(list = fun_name, envir = joyn_ns_exports) joyn_ns <- getNamespace("joyn") - # debug lines to be removed ---- - # print("EXPORTS AFTER `remove`") - # print(getNamespaceExports(joyn_ns)) - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Apply the new mask ---- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -329,9 +215,48 @@ unmask_joyn_fun_ns <- function(fun_name, suppressPackageStartupMessages(attachNamespace(joyn_ns)) } - # Inform the user - clear_joynenv() + # 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 +unmask_joyn <- \(fun_name, + pkg_name = "dplyr") { + + fun <- 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 + } + ) + + names(l) <- fun_name + + # Inform the user store_msg(type = "info", ok = paste(cli::symbol$info, " Note: "), pale = "function", @@ -342,8 +267,7 @@ unmask_joyn_fun_ns <- function(fun_name, joyn_msg() - # return - invisible(TRUE) + return(invisible(l)) } @@ -353,8 +277,7 @@ unmask_joyn_fun_ns <- function(fun_name, #' 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 of the package to detach -#' @param character logical. TRUE when pkg_name provided as a character string, FALSE otherwise; Default to FALSE +#' @param pkg_name name of the package to detach #' @return invisibly detach package from search list #' @keywords internal #' diff --git a/_pkgdown.yml b/_pkgdown.yml index e08ef2ea..e82d58fa 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -84,3 +84,4 @@ reference: - is_balanced - rename_to_valid - joyn_msg + - unmask_joyn diff --git a/man/detach_package.Rd b/man/detach_package.Rd index defa7598..2d403b8a 100644 --- a/man/detach_package.Rd +++ b/man/detach_package.Rd @@ -7,9 +7,7 @@ detach_package(pkg_name) } \arguments{ -\item{pkg}{name of the package to detach} - -\item{character}{logical. TRUE when pkg_name provided as a character string, FALSE otherwise; Default to FALSE} +\item{pkg_name}{name of the package to detach} } \value{ invisibly detach package from search list diff --git a/man/unmask_joyn.Rd b/man/unmask_joyn.Rd new file mode 100644 index 00000000..d35dd709 --- /dev/null +++ b/man/unmask_joyn.Rd @@ -0,0 +1,19 @@ +% 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 +} diff --git a/man/unmask_joyn_fun.Rd b/man/unmask_joyn_fun.Rd deleted file mode 100644 index ca61144a..00000000 --- a/man/unmask_joyn_fun.Rd +++ /dev/null @@ -1,35 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{unmask_joyn_fun} -\alias{unmask_joyn_fun} -\title{Unmask joyn's functions ({conflicted} version)} -\usage{ -unmask_joyn_fun(fun_name, pkg_name) -} -\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 TRUE - -invisible TRUE -} -\description{ -Unmask joyn's functions ({conflicted} version) -} -\keyword{(local} -\keyword{-to} -\keyword{Unmask} -\keyword{be} -\keyword{done} -\keyword{export} -\keyword{function(s)} -\keyword{internal} -\keyword{joyn's} -\keyword{once} -\keyword{replaced} -\keyword{vectorized)} -\keyword{version,} -\keyword{with} diff --git a/man/unmask_joyn_fun_ns.Rd b/man/unmask_joyn_fun_ns.Rd index fd62a6f9..5d663da8 100644 --- a/man/unmask_joyn_fun_ns.Rd +++ b/man/unmask_joyn_fun_ns.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/utils.R \name{unmask_joyn_fun_ns} \alias{unmask_joyn_fun_ns} -\title{Unmask joyn function (namespace version, NOT vectorized)} +\title{Unmask joyn function (namespace version)} \usage{ unmask_joyn_fun_ns(fun_name, pkg_name) } @@ -12,9 +12,9 @@ unmask_joyn_fun_ns(fun_name, pkg_name) \item{pkg_name}{character specifying package from which joyn masks the function} } \value{ -invisibly unmask function that joyn masks from another package +invisibly unmask function that joyn masks } \description{ -Unmask joyn function (namespace version, NOT vectorized) +Unmask joyn function (namespace version) } \keyword{internal}