无法重现最小的工作示例Rcpp

时间:2016-01-12 09:51:44

标签: c++ r rcpp

我最近下载并安装了Rcpp软件包,以便使用一些C ++代码加速我自己的软件包的代码。

我要做的第一件事是使用Rcpp.package.skeleton(name="firstpackage")制作一个包:

> Rcpp.package.skeleton(name="firstpackage")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './firstpackage/Read-and-delete-me'.

Adding Rcpp settings
 >> added Imports: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
 >> added example src file using Rcpp attributes
 >> compiled Rcpp attributes
 >> added Rd file for rcpp_hello_world

这将生成一个包含文件夹manRsrc的文件系统。但是,如果我与 Seamless R和C ++与Rcpp集成的书中的第66-67页上的示例进行比较,则这些文件夹中缺少文件MakevarsMakevars.winrcpp_hello_world.h 。为什么这些文件不像示例中那样生成?安装firstpackage后,我无法使用函数rcpp_hello_world(),所以我猜这是因为缺少文件。

编辑:现在很清楚,问题不是丢失的文件,而是其他问题。我再次尝试,现在完全按照说明操作:

> Rcpp.package.skeleton("ruben")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './ruben/Read-and-delete-me'.

Adding Rcpp settings
 >> added Imports: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
 >> added example src file using Rcpp attributes
 >> compiled Rcpp attributes
 >> added Rd file for rcpp_hello_world

然后我查看文件。 (有点奇怪,但文件结构看起来是一样的)

> system("tree ruben")
Folder PATH listing for volume OSDisk
Volume serial number is A811-3ED9
C:\USERS\N14083\DOCUMENTS\RUBEN
ÃÄÄÄman
ÃÄÄÄR
ÀÄÄÄsrc

然后我安装新创建的包:

> system("R CMD INSTALL ruben")
* installing to library 'C:/Users/n14083/Documents/R/win-library/3.1'
* installing *source* package 'ruben' ...
** libs

*** arch - i386
Warning: running command 'make -f "C:/PROGRA~1/R/R-31~1.1/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-31~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="ruben.dll" OBJECTS="RcppExports.o rcpp_hello_world.o"' had status 127
ERROR: compilation failed for package 'ruben'
* removing 'C:/Users/n14083/Documents/R/win-library/3.1/ruben'
Warning message:
running command 'R CMD INSTALL ruben' had status 1 

安装失败:

library(ruben)
Error in library(ruben) : there is no package called ‘ruben’

编辑:解决方案下载Windows工具集(http://cran.at.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset)并正确设置PATH,在我的情况下:

pathRtools <- paste(c("c:\\Rtools\\bin",
                      "C:\\Rtools\\gcc-4.6.3\\bin",
                      "c:\\PROGRA~2\\MIKTEX~1.9\\miktex\\bin",
                      "C:\\PROGRA~1\\R\\R-3.1.1\\bin\\i386",
                      "c:\\windows",
                      "c:\\windows\\system32"), collapse=";")
Sys.setenv(PATH=pathRtools)
setwd("C:/Users/n14083/Documents")
system("R CMD INSTALL ruben")

然后重新启动R.

1 个答案:

答案 0 :(得分:1)

  1. 这本书写的时候是最新的。正如您现在所观察到的那样,标题不再包含在内,Makevars 也不再包含它们
  2. 那是因为我们现在使用Rcpp属性构建。这很容易。
  3. 它仍然有效(当然)。
  4. 记录下面。

    R> Rcpp.package.skeleton("ruben")
    Creating directories ...
    Creating DESCRIPTION ...
    Creating NAMESPACE ...
    Creating Read-and-delete-me ...
    Saving functions and data ...
    Making help files ...
    Done.
    Further steps are described in './ruben/Read-and-delete-me'.
    
    Adding Rcpp settings
     >> added Imports: Rcpp
     >> added LinkingTo: Rcpp
     >> added useDynLib directive to NAMESPACE
     >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
     >> added example src file using Rcpp attributes
     >> compiled Rcpp attributes
     >> added Rd file for rcpp_hello_world
    R> 
    

    让我们来看看文件:

    R> system("tree ruben")
    ruben
    ├── DESCRIPTION
    ├── man
    │   ├── rcpp_hello_world.Rd
    │   └── ruben-package.Rd
    ├── NAMESPACE
    ├── R
    │   └── RcppExports.R
    ├── Read-and-delete-me
    └── src
        ├── RcppExports.cpp
        └── rcpp_hello_world.cpp
    
    3 directories, 8 files
    R> 
    

    并使用它们:

    R> system("R CMD INSTALL ruben")
    * installing to library ‘/usr/local/lib/R/site-library’
    * installing *source* package ‘ruben’ ...
    ** libs
    g++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -O3 -Wall -pipe -Wno-unused -pedantic  -c rcpp_hello_world.cpp -o rcpp_hello_world.o
    g++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -O3 -Wall -pipe -Wno-unused -pedantic  -c RcppExports.cpp -o RcppExports.o
    g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o ruben.so RcppExports.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
    installing to /usr/local/lib/R/site-library/ruben/libs
    ** R
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded
    * DONE (ruben)
    R> 
    

    显然有效:

    R> library(ruben)
    R> rcpp_hello_world()
    [[1]]
    [1] "foo" "bar"
    
    [[2]]
    [1] 0 1
    
    R> 
    

    所以请证实索赔

      

    安装firstpackage后我无法使用函数rcpp_hello_world(),所以我猜这是因为缺少文件。

    运行此骨架是每个单元测试运行的一部分,因此自本书编写以来已对其进行了数千次测试。