Skip to content

Commit

Permalink
Merge pull request #66 from randrescastaneda:rm_unmasking
Browse files Browse the repository at this point in the history
Remove function to unmask joyn functions
  • Loading branch information
randrescastaneda authored Jul 8, 2024
2 parents 5c540cd + fbc6ace commit da1da15
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 259 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
180 changes: 0 additions & 180 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}



Expand Down
1 change: 0 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,3 @@ reference:
- is_balanced
- rename_to_valid
- joyn_msg
- unmask_joyn
19 changes: 0 additions & 19 deletions man/detach_package.Rd

This file was deleted.

38 changes: 0 additions & 38 deletions man/unmask_joyn.Rd

This file was deleted.

20 changes: 0 additions & 20 deletions man/unmask_joyn_fun_ns.Rd

This file was deleted.

0 comments on commit da1da15

Please sign in to comment.