if you really want to simulate a library call we can create a "package:yourpkg" on the searchpath containing all functions :
magrittr::as_pipe_fn
#> Error: 'as_pipe_fn' n'est pas un objet exporté depuis 'namespace:magrittr'
magrittr:::as_pipe_fn
#> function (expr, env)
#> {
#> eval(call("function", lambda_fmls, expr), env)
#> }
#> <bytecode: 0x0000000013917228>
#> <environment: namespace:magrittr>
attach_all <- function(pkg) {
pkg <- as.character(substitute(pkg))
pkg_long <- paste0("package:", pkg)
eval(bquote(with(
setNames(list(getNamespace(pkg)), pkg_long),
attach(.(as.symbol(pkg_long)))
)))
}
attach_all(magrittr)
search()
#> [1] ".GlobalEnv" "package:magrittr" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "tools:callr" "package:base"
as_pipe_fn
#> function (expr, env)
#> {
#> eval(call("function", lambda_fmls, expr), env)
#> }
#> <bytecode: 0x0000000013917228>
#> <environment: namespace:magrittr>
Created on 2020-10-09 by the reprex package (v0.3.0)
It might make you nervous to use package:mypkg
, feel free to change this part if so, the result will be the same.
Another way would be to attach the unexported functions to a different environment so you can detach it independently if needed :
attach_unexported <- function(pkg) {
pkg <- as.character(substitute(pkg))
all_funs <- lsf.str(asNamespace(pkg))
exported <- getNamespaceExports(pkg)
unexported_funs <- setdiff(all_funs, exported)
pkg_long <- paste0(pkg, "_unexported")
eval(bquote(with(
setNames(list(mget(unexported_funs, asNamespace(pkg))), pkg_long),
attach(.(as.symbol(pkg_long)))
)))
}
attach_unexported(magrittr)
search()
#> [1] ".GlobalEnv" "magrittr_unexported" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "tools:callr" "package:base"
as_pipe_fn
#> function (expr, env)
#> {
#> eval(call("function", lambda_fmls, expr), env)
#> }
#> <bytecode: 0x0000000013a05bd0>
#> <environment: namespace:magrittr>
`%>%`
#> Error in eval(expr, envir, enclos): objet '%>%' introuvable
Created on 2020-10-09 by the reprex package (v0.3.0)