2013-12-20 93 views
0

我想将我自己的很多自定义函数转换成可以使用package.skeleton重复使用的软件包。我的功能取决于其他包(例如zoo,reshape,boot等)。例如,假设我写了我所有的自定义函数在一个名为myOwnFunctions.R文件,该文件类似于如下:R - regd软件包依赖于另一个软件包


myFunc1<-function() { 
    require(zoo) 
    ... 
} 
myFunc2<-function() { 
    require(boot) 
    ... 
} 
... 
myFuncN<-function() { 
    require(reshape) 
    ... 
} 

其中每个功能已与require()功能的行,如果它使用了另一个库。我使用下面的代码(createPackage)将该文件转换为我自己的包,这是我从其他人在StackOverflow上发布的3-4篇文章中采用的(这实际上与我的问题无关,但为了以防万一)。

createPackage<-function (rCodeFile) { 
    source(rCodeFile) 

    # name the package as the same name as rCodeFile, but remove the path 
    pkgName<-strsplit(rCodeFile,"/")[[1]] 
    pkgName<-strsplit(pkgName[length(pkgName)],"\\.")[[1]][1] 

    # remove existing directory of files that package.skeleton creates 
    pkgDir<-paste(getwd(),pkgName,sep="/") 
    if (file.exists(pkgDir)) unlink(pkgDir,recursive=TRUE) 

    # create a skeleton directory for package 
    package.skeleton(name=pkgName) 

    # remove the data directory 
    unlink(paste(pkgDir,"data",sep="/"),recursive=TRUE) 

    # remove files in man directory, except the ones with the name as package 
    for (i in list.files(paste(pkgDir,"man",sep="/"))) { 
    if (length(grep("package",i))==0) unlink(paste(pkgDir,"man",i,sep="/")) 
    } 

    # build the package 
    system(paste("rcmd","build",pkgName,sep=" ")) 

    # install the package 
    system(paste("rcmd INSTALL -l",paste0("\"",.libPaths()[1],"\""),pkgName,sep=" ")) 
} 

因此,它工作正常,但有一个问题。如果我打电话library(myOwnFunctions),那么它不会加载zooreshape,等那个时候,而是加载zooreshape我第一次打电话,有一个require(zoo)require(reshape)线等

我想一个函数当我打电话给library(myOwnFunctions)时,需要加载依赖包。我的问题是,不是在每个我的自定义函数myFunc1myFunc2等使用require,如果我写我的源代码myOwnFunctions.R如下:


library(zoo) 
library(reshape) 
library(boot) 
... 
myFunc1<-function() { 
    ... 
} 
myFunc2<-function() { 
    ... 
} 
... 
myFuncN<-function() { 
    ... 
} 

,然后,如果我跑package.skeletonpackage.skeleton创建的所有文件/目录当中(即在哪个文件夹中的哪个文件?)是否包含此包将对zoo,reshape,boot等具有依赖关系

感谢

回答

0

您可以通过编辑创建package.skeleton描述文件,特别是DependsImports领域。

相关问题