Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search all existing functions for package dependencies?

Tags:

package

r

I have a package that I wrote while learning R and its dependency list is quite long. I'm trying to trim it down, for two cases:

  1. I switched to other approaches, and packages listed in Suggests simply aren't used at all.
  2. Only one function out of my whole package relies on a given dependency, and I'd like to switch to an approach where it is loaded only when needed.

Is there an automated way to track down these two cases? I can think of two crude approaches (download the list of functions in all the dependent packages and automate a text search for them through my package's code, or load the package functions without loading the required packages and execute until there's an error), but neither seems particularly elegant or foolproof....

like image 693
Ari B. Friedman Avatar asked Mar 22 '12 01:03

Ari B. Friedman


People also ask

How do I see all package functions in R?

We can find the functions inside a package by using lsf.

What are package dependencies?

A dependency is another package that your package needs in order to work. Dependencies are specified in your pubspec. You list only immediate dependencies—the software that your package uses directly.

What does dependencies mean in R?

This could be due to the fact that the package you are trying to install has what is known as a dependency. What this means is that in order for the package to properly install and run, it requires another package to already be installed.


1 Answers

One way to check dependancies in all functions is to use the byte compiler because that will check for functions being available in the global workspace and issue a notice if it does not find said function.

So if you as an example use the na.locf function from the zoo package in any of your functions and then byte compile your function you will get a message like this:

Note: no visible global function definition for 'na.locf' 

To correctly address it for byte compiling you would have to write it as zoo::na.locf

So a quick way to test all R functions in a library/package you could do something like this (assuming you didn't write the calls to other functions with the namespace):

Assuming your R files with the functions are in C:\SomeLibrary\ or subfolders there of and then you define a sourceing file as C:\SomeLibrary.r or similar containing:

if (!(as.numeric(R.Version()$major) >=2 && as.numeric(R.Version()$minor) >= 14.0)) {
        stop("SomeLibrary needs version 2.14.0 or greater.")
}

if ("SomeLibrary" %in% search()) {
        detach("SomeLibrary")
}

currentlyInWorkspace <- ls()

SomeLibrary <- new.env(parent=globalenv())

require("compiler",quietly=TRUE)

pathToLoad <- "C:/SomeLibraryFiles"

filesToSource <- file.path(pathToLoad,dir(pathToLoad,recursive=TRUE)[grepl(".*[\\.R|\\.r].*",dir(pathToLoad,recursive=TRUE))])

for (filename in filesToSource) {

        tryCatch({
                suppressWarnings(sys.source(filename, envir=SomeLibrary))
        },error=function(ex) {
                cat("Failed to source: ",filename,"\n")
                print(ex)
        })
}

for(SomeLibraryFunction in ls(SomeLibrary)) {
        if (class(get(SomeLibraryFunction,envir=SomeLibrary))=="function") {
                outText <- capture.output(with(SomeLibrary,assign(SomeLibraryFunction,cmpfun(get(SomeLibraryFunction)))))
                if(length(outText)>0){
                        cat("The function ",SomeLibraryFunction," produced the following compile note(s):\n")
                        cat(outText,sep="\n")
                        cat("\n")
                }
        }
}

attach(SomeLibrary)

rm(list=ls()[!ls() %in% currentlyInWorkspace])

invisible(gc(verbose=FALSE,reset=TRUE))

Then start up R with no preloaded packages and source in C:\SomeLibrary.r

And then you should get notes from cmpfun for any call to a function in a package that's not part of the base packages and doesn't have a fully qualified namespace defined.

like image 123
Hansi Avatar answered Nov 16 '22 03:11

Hansi