Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load package's internal function in global environment

Tags:

r

I have a package whose internal functions I want to load in the global environment. The only way I can think of is to do it manually, i.e.

f <- packageName:::someInternalFunction

For all internal functions. I'm guessing there's an easier way to do it, but how? Thanks in advance.

like image 410
Stijn Avatar asked Dec 01 '22 16:12

Stijn


2 Answers

You could do the following:

library(pacman)

pack.name <- "qdap"

hidden <- setdiff(p_funs(pack.name, TRUE), p_funs(pack.name))

invisible(lapply(hidden, function(x) {

    a <- strtrim(x, 1) == "%" 
    b <- substring(x, nchar(x)) == "%"

    if (a && b) {
        x2 <- paste0("`", x, "`")
    } else {
        x2 <- x
    }

    assign(x, eval(parse(text=paste0(pack.name, ":::", x2))), 
        envir = .GlobalEnv)
}))
like image 98
Tyler Rinker Avatar answered Dec 15 '22 10:12

Tyler Rinker


Use package pacman as an example:

attach(loadNamespace("pacman"), name = "pacman_all")

Now all its unexported functions are available. You can see the attached "pacman_all" with search().

To revert, run detach("pacman_all").

like image 30
Jialin Ma Avatar answered Dec 15 '22 11:12

Jialin Ma