2017-08-10 96 views
0

RCPP初学者的问题:添加依赖我RCPP包

我想提高我的R.执行效率所以我写的cpp一些代码,并使用RCPP帮我编译它们。

问题是我在我的.cpp文件中使用了一些其他R包,我希望在用户安装我的包时自动安装和导入这些包。

例如如果我用我的文件将R包“gtools”,我不希望错误:

* installing to library 'C:/Program Files/R/R-3.4.1/library' 
* installing *source* package 'pkgname' ... 
make: Nothing to be done for `all`. 
** libs 
installing to C:/Program Files/R/R-3.4.1/library/pkgname/libs/i386 
** R 
** preparing package for lazy loading 
Error in library(gtools) : there is no package called 'gtools' 
Error : unable to load R code in package 'pkgname' 
ERROR: lazy loading failed for package 'pkgname' 
* removing 'C:/Program Files/R/R-3.4.1/library/pkgname' 

Exited with status 1. 

我试图依靠包名称添加到描述文件。即

Imports: Rcpp (>= 0.12.12),gtools 
LinkingTo: Rcpp, gtools 

但它给了我以下错误:

ERROR: dependency 'gtools' is not available for package 'pkgname' 

我没有找到任何类似的问题,并告诉我,如果有。

回答

2

首先,你应该确保gtools在你的系统上安装了。我这样说是因为有下列错误:

Error in library(gtools) : there is no package called 'gtools'

有了这个虽这么说,你碰到的主要问题是在DESCRIPTION文件LinkingTo:Imports:领域之间的不确定性。这包括在Writing R ExtensionsSection 1.1.3: Package Dependencies中。

具体来说,我们有:

The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’ (see below). Ideally this field will include all the standard packages that are used, and it is important to include S4-using packages (as their class definitions can change and the DESCRIPTION file is used to decide which packages to re-install when this happens). Packages declared in the ‘Depends’ field should not also be in the ‘Imports’ field. Version requirements can be specified and are checked when the namespace is loaded (since R >= 3.0.0).

而且LinkingTo领域:

A package that wishes to make use of header files in other packages needs to declare them as a comma-separated list in the field ‘LinkingTo’ in the DESCRIPTION file. For example

LinkingTo: link1, link2 

The ‘LinkingTo’ field can have a version requirement which is checked at installation.

Specifying a package in ‘LinkingTo’ suffices if these are C++ headers containing source code or static linking is done at installation: the packages do not need to be (and usually should not be) listed in the ‘Depends’ or ‘Imports’ fields. This includes CRAN package BH and almost all users of RcppArmadillo and RcppEigen .

For another use of ‘LinkingTo’ see Linking to native routines in other packages .

因此,Imports:意味着指定包含[R功能,你要导入的包。特别是,必须在NAMESPACE文件中指定来自给定包或整个包本身的功能。对于使用Rcpp的软件包,如果作者从C++导出例程,则通常可以预期功能可用R

现在,关于LinkingTo:,这是一个更具体一点。如果作者希望通过头文件提供API,那么他们必须明确地声明如在Writing R Extensionsnative methods中给出的语句。通常,以这种方式进行的软件包是“仅标头”。这些包将报头定义放置在inst/include下,例如,

|- pkgname 
    |- inst/ 
     |- include/ 
     |- pkgname.h 
    |- R/ 
    |- man/ 
    |- DESCRIPTION 
    |- NAMESPACE 

但是,另一个趋势是允许“非标题”包。由于必须了解共享对象和动态库,这会导致主题稍微复杂一些。CRAN呈现如何“链接”包Section 5.8: Linking to other packagesWriting R Extensions

概述如果作者不无法提供一份C++ API,那么有选项:

  1. 作者很好地支持调用C++ API或提交一个修补程序,该修补程序允许访问API的C++
  2. Call an R function from C++。 (这会消除在C++中编写代码所带来的性能收益)
  3. 在尊重知识产权的同时,从作者的包中复制实现。
  4. 从零开始实施所需的功能以避免许可问题。

不幸的是,这是gtools的情况。正如作者do not provide a means to "link" to the C++ version of package's code