2017-08-10 144 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