在所有现有功能中搜索程序包依赖性吗? [英] Search all existing functions for package dependencies?

查看:80
本文介绍了在所有现有功能中搜索程序包依赖性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在学习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. 我改用其他方法,而根本不使用Suggests中列出的软件包.
  2. 整个软件包中只有一个函数依赖于给定的依赖关系,我想切换到一种
  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....

推荐答案

检查所有函数中的依赖关系的一种方法是使用字节编译器,因为这将检查全局工作空间中是否有可用的函数并发出通知(如果这样做)找不到所说的功能.

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.

因此,举例来说,如果您在任何函数中使用zoo软件包中的na.locf函数,然后对函数进行字节编译,您将得到如下消息:

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' 

要正确寻址以进行字节编译,您必须将其编写为zoo :: na.locf

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

因此,一种快速的测试库/包中所有R函数的方法可以执行以下操作(假设您没有使用命名空间编写对其他函数的调用):

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):

假定具有功能的R文件位于C:\ SomeLibrary \或其中的子文件夹中,然后将源文件定义为C:\ SomeLibrary.r或包含以下内容的类似文件:

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))

然后在C:\ SomeLibrary.r中启动没有预装程序包和源代码的R

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

然后,您应该从cmpfun获得注释,以了解对不属于基本软件包且未定义完全限定名称空间的软件包中的任何函数的调用.

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.

这篇关于在所有现有功能中搜索程序包依赖性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆