我正在尝试重现“使用Rcpp进行无缝R和C ++集成”一书的示例,但有些代码没有运行。具体来说,它在第5章第5.2.3节,代码如下:
#ifndef _mypackage_RCPP_HELLO_WORLD_H
#define _mypackage_RCPP_HELLO_WORLD_H
#include <Rcpp.h>
/*
* note : RcppExport is an alias to 'extern "C"' defined by Rcpp.
*
* It gives C calling convention to the rcpp_hello_world
* function so that it can be called from .Call in R.
* Otherwise, the C++ compiler mangles the
* name of the functioand .Call can’t find it.
* It is only useful to use RcppExport when the function
* is intended to be called by .Call. See the thread
* http://thread.gmane.org/gmane.comp.lang.r.rcpp/649/focus=672
* on Rcpp-devel for a misuse of RcppExport 19
*/
RcppExport SEXP rcpp_hello_world();
#endif
,下一个是
#include "rcpp_hello_world.h"
SEXP rcpp_hello_world() {
using namespace Rcpp;
CharacterVector x = CharacterVector::create( "foo", "bar" ) ;
NumericVector y = NumericVector::create( 0.0, 1.0 ) ;
List z = List::create( x, y ) ;
return z ;
}
R显示的错误是
Error in .Call("rcpp_hello_world", PACKAGE = "mypackage") :
"rcpp_hello_world" not available for .Call() for package "mypackage"
有没有人遇到这样的问题?
提前致谢!
当我尝试提示时
R CMD check mypackage_1.0.tar.gz
我收到了这样的错误
* using log directory ‘/Users/Marcos/Downloads/mypackage.Rcheck’
* using R version 3.0.2 (2013-09-25)
* using platform: x86_64-apple-darwin10.8.0 (64-bit)
* using session charset: UTF-8
* checking for file ‘mypackage/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘mypackage’ version ‘1.0’
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘mypackage’ can be installed ... ERROR
Installation failed.
See ‘/Users/Marcos/Downloads/mypackage.Rcheck/00install.out’ for details.
在文件00install.out中,第一个错误是
* installing *source* package ‘mypackage’ ...
** libs
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o
In file included from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/RcppCommon.h:28,
from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp.h:27,
from RcppExports.cpp:4:
/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:93:17: error: cmath: No such file or directory
然后,我收到了错误消息。
答案 0 :(得分:1)
此示例(和章节)讨论了如何构建(和加载)整个包,此处称为mypackage
。
您实际创建,编译和加载mypackage
了吗?
这是有效的,因为我们每次运行单元测试时都这样做。
编辑:以下是您的完整日志
R> library(Rcpp)
R> Rcpp.package.skeleton("mypackage") ## page 66, Section 5.2.1
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './mypackage/Read-and-delete-me'.
Adding Rcpp settings
>> added Depends: Rcpp
>> added LinkingTo: Rcpp
>> added useDynLib directive to NAMESPACE
>> added Makevars file with Rcpp settings
>> added Makevars.win file with Rcpp settings
>> added example src file using Rcpp attributes
>> compiled Rcpp attributes
>> added Rd file for rcpp_hello_world
R>
我们现在可以构建tarball:
R> system("R CMD build mypackage")
* checking for file 'mypackage/DESCRIPTION' ... OK
* preparing 'mypackage':
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building 'mypackage_1.0.tar.gz'
R>
然后我们可以安装它:(我在Windows机器上进行更改)
R> system("R CMD INSTALL mypackage_1.0.tar.gz")
* installing to library 'c:/opt/R-library'
* installing *source* package 'mypackage' ...
** libs
*** arch - i386
g++ -m32 -I"c:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m32 -I"c:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
g++ -m32 -shared -s -static-libgcc -o mypackage.dll tmp.def RcppExports.o rcpp_hello_world.o c:/opt/R-library/Rcpp/lib/i386/libRcpp.a -Ld:/RCompile/CRANpkg/extralibs64/local/lib/i386 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -Lc:/opt/R-CURR~1/bin/i386 -lR
installing to c:/opt/R-library/mypackage/libs/i386
*** arch - x64
g++ -m64 -I"c:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m64 -I"c:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
g++ -m64 -shared -s -static-libgcc -o mypackage.dll tmp.def RcppExports.o rcpp_hello_world.o c:/opt/R-library/Rcpp/lib/x64/libRcpp.a -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -Lc:/opt/R-CURR~1/bin/x64 -lR
installing to c:/opt/R-library/mypackage/libs/x64
** R
** preparing package for lazy loading
** help
Warning: C:/Users/deddelbuettel/AppData/Local/Temp/RtmpaiaZVk/R.INSTALL65481db46d2b/mypackage/man/mypackage-package.Rd:30: All text must be in a section
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (mypackage)
R>
之后我们可以加载并运行它:
R> library(mypackage)
R> rcpp_hello_world()
[[1]]
[1] "foo" "bar"
[[2]]
[1] 0 1
R>
最后删除它,因为除了演示这些机制之外它没有真正的用例:
R> remove.packages("mypackage")
Removing package from ‘c:/opt/R-library’
(as ‘lib’ is unspecified)
R>
答案 1 :(得分:1)
转到终端:
R CMD build mypackage
R CMD check mypackage_1.0.tar.gz
R CMD install mypackage
并在R:
library(mypackage)
rcpp_hello_world()
[[1]]
[1] "foo" "bar"
[[2]]
[1] 0 1
现在,我不明白在哪里使用“清单5.4”中编写的代码,我是否需要编写它或者这是自动创建的?