OK, this needs a more accurate answer.
Short version: you can't.
To see why, consider the following code in a package:
eval(parse(text = paste0("foo <- func", "tion () 1 + 1")))
This will create a function foo
. But you will only learn that by running the R code.
You could check the NAMESPACE file for export(foo)
, but unfortunately the author might have written something like exportPattern("f.*")
, so that won't be reliable either.
Long version: you can't avoid loading the package, but you can avoid attaching it. In other words, R will interpret the package source files (and load any dlls), and will store the package in memory, but it won't be directly available on the search path.
ns <- loadNamespace(package)
exists("foo", ns)
You can then unload the namespace with unloadNamespace(package)
. But see the warnings in ?detach
: this is not always guaranteed to work! loadNamespace(package, partial = TRUE)
may be helpful, or maybe devtools::load_all
and devtools::unload
do something cleverer, I don't know.
Some answers are suggesting stuff like try{package::foo}
. The problem is that this itself loads the namespace:
> isNamespaceLoaded("broom")
[1] FALSE
> try(broom::tidy)
function(x, ...) UseMethod("tidy")
<environment: namespace:broom>
> isNamespaceLoaded("broom")
[1] TRUE
:::
operator to search NAMESPACE might cover most cases. – Brown