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

Unable to reproduce minimal working example 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 and C++ Integration 中第 66-67 页的示例进行比较,这些文件夹中缺少文件 MakevarsMakevars.winrcpp_hello_world.h与 Rcpp。为什么这些文件不像示例中那样生成?安装后 firstpackage 我无法使用 rcpp_hello_world() 功能,所以我猜是因为缺少文件。

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

> 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)并正确设置路径,在我的例子中:

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. 这本书在写作时是最新的。正如您现在观察到的,header 不再包括在内,Makevars 也不再包括在内,因为不再需要它们.
  2. 那是因为我们现在使用 Rcpp 属性构建。更简单。
  3. 它仍然有效(当然)。

在下方登录。

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> 

所以请证实这个说法

After installing firstpackage I cannot use the function rcpp_hello_world() so I guess it is because of the missing files.

运行 这个框架是 每个 单元测试 运行 的一部分,所以自本书编写以来,它已经被测试了数千次。