1

A situation that often comes up on SO, is packages having overwritten functions causing unexpected errors in packages that rely on certain function definitions (for example MASS::negative.binomial and GLMMadaptive::negative.binomial has different argument, which causes some dependent packages to fail).

If the function clashing is exported from the package, a fix that doesn't require closing and opening R can often be achieved as

pck <- find("negative.binomial")
if(length(pck) > 0){
   suppressWarnings(sapply(unlist(pck), function(x) 
                           detach(x, unload = TRUE, 
                                  force = TRUE, character.only = TRUE))
}
if(length(find("negative.binomial")) == 0)
   cat("Succes!\n")

(credits to the popular question here, for the idea.)

But in the case that a function or a method of a function is not exported, eg. lme4:::influence.merMod and car:::influence.merMod, find won't find the function

>library(lme4)
>library(car)
>find("influence.merMod")
character(0)

Given only the method of function as a string, how would one find the attached packages which includes a specific none-exported function or method?

Oliver
  • 8,169
  • 3
  • 15
  • 37
  • 1
    A not-exported function can't be masked because a package needs to import it explicitly and a user can't access it without prefixing the package. – Roland Sep 24 '19 at 12:38
  • Might be the case, my question still stands. How would one locate packages which include functions or methods that are not exported, only by using a similar method to `find(...)`. – Oliver Sep 24 '19 at 12:41

2 Answers2

2
library(lme4)
library(car)

fun <- "influence.merMod"
p <- loadedNamespaces()
p[vapply(p, function(p) fun %in% ls(envir = asNamespace(p)), FUN.VALUE = FALSE)]
#[1] "car"  "lme4"
Roland
  • 127,288
  • 10
  • 191
  • 288
2

getAnywhere will find all occurrences and then we can extract the names from the structure it returns.

unique(sub(".*[ :]", "", getAnywhere("influence.merMod")$where))  
## [1] "lme4" "car" 
bretauv
  • 7,756
  • 2
  • 20
  • 57
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341