如何使用RcppEigen

时间:2014-10-24 07:04:36

标签: c++ windows r eigen rcpp

我是C ++的初学者,我想添加一个新包" CRTRCppEigen"在C ++代码中RcppEigen,而在运行一些.bat文件时发生了一些错误。任何帮助将不胜感激。

这是名为" CRTRCppEigen"的C ++代码函数。我想在文件' src'中添加RcppEigen:

#include < Rcpp.h >
#include < RcppEigen.h >
#include <Eigen/Dense>
#include <iostream>
#include <string>
using Eigen::MatrixXd;
using namespace std;
using namespace Rcpp;
using namespace RcppEigen;
using namespace Eigen;

RcppExport SEXP MatOp(SEXP Xr, SEXP Yr, SEXP Kr)
{ 
  MatrixXd X = RcppEigen::as<MatrixXd>(Xr);   
  MatrixXd Y = RcppEigen::as<MatrixXd>(Yr);
  string K = Rcpp::as<string>(Kr);

  int n=X.rows();
  int p=X.cols();
  int nY=Y.cols();
  MatrixXd I(n,n);
  I.setIdentity(n,n);
  double SSE=(Y.transpose()*(I-X*(X.transpose()*X).inverse()*X.transpose())*Y).determinant();

  if(Criteria=="k1")
    return (wrap(n*log(SSE/n)+log(n)*p));
  if(Criteria=="k2")
    return (wrap(n*log(SSE/n)+(2*p*nY*n+nY*(nY+1))/n-2/n+n+2)); 
}  

这是文件中的R代码&#39; R&#39;

RcppEigen.package.skeleton("CRTRCppEigen")
getwd()
#[1] "C:/Users/LJH/Documents"
CRTR <- function(Xr,Yr,criteriar) {
  .Call('CRTRCppEigen',Xr,Yr,criteriar,PACKAGE = 'CRTRCppEigen')
}

prompt(CRTR)

描述文件是

Package: CRTRCppEigen
Type: Package
Title: RSS calculation based matrix operation
Version: 1.0
Date: 2014-10-22
Author: Junhui Li
Maintainer: Junhui Li<junhuili@cau.edu.cn>
Description: Residual sum of square 
License: GPL (>= 2)
Imports: Rcpp (>= 0.11.3), RcppEigen (>= 0.3.2.2.0)
LinkingTo: Rcpp, RcppEigen

build CRTRCppEigen package Windows Binary.bat文件是:

Rcmd INSTALL --build CRTRCppEigen

PAUSE

build CRTRCppEigen package Linux Source Code.bat文件是:

Rcmd build CRTRCppEigen

pause

install CRTRCppEigen package.bat文件是:

Rcmd INSTALL CRTRCppEigen

PAUSE

check CRTRCppEigen package.bat文件是:

Rcmd check CRTRCppEigen

PAUSE

环境变量是

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Hewlett-Packard\HP ProtectTools Security Manager\Bin\;c:\Program Files\Intel\DMIX;C:\Program Files (x86)\Intel\Services\IPT\;C:\Program Files\SASHome\Secure;C:\Program Files\SASHome\x86\Secure;C:\Opt\MiKTeX2.9\miktex\bin\x64\;C:\Opt\MiKTeX2.9\miktex\bin\x64\;C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;C:\R\R-3.1.1\bin\x64;C:\R\R-3.1.1\bin\i386;

当我运行build CRTRCppEigen package Windows Binary.bat时,会发生错误:

    * installing *source* package 'CRTRCppEigen' ...
    ** libs

    *** arch - i386
    cygwin warning:
      MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
      Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
      CYGWIN environment variable option "nodosfilewarning" turns off this warning.
      Consult the user's guide for more details about POSIX paths:
        http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
    g++ -m32 -I"C:/R/R-31~1.1/include" -DNDEBUG    -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c CRTRCppEigen.cpp -o CRTRCppEigen.o
    CRTRCppEigen.cpp:3:20: fatal error:  Rcpp.h : No such file or directory
    compilation terminated.
    make: *** [CRTRCppEigen.o] Error 1
    Warning: running command 'make -f "Makevars.win" -f "C:/R/R-31~1.1/etc/i386/Makeconf" -f "C:/R/R-31~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="CRTRCppEigen.dll" OBJECTS="CRTRCppEigen.o RcppExports.o rcppeigen_hello_world.o"' had status 2
    cygwin warning:
      MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
      Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
      CYGWIN environment variable option "nodosfilewarning" turns off this warning.
      Consult the user's guide for more details about POSIX paths:
        http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
    g++ -m32 -I"C:/R/R-31~1.1/include" -DNDEBUG    -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c CRTRCppEigen.cpp -o CRTRCppEigen.o
    CRTRCppEigen.cpp:3:20: fatal error:  Rcpp.h : No such file or directory
    compilation terminated.
    make: *** [CRTRCppEigen.o] Error 1
    Warning: running command 'make -f "Makevars.win" -f "C:/R/R-31~1.1/etc/i386/Makeconf" -f "C:/R/R-31~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="CRTRCppEigen.dll" OBJECTS="CRTRCppEigen.o RcppExports.o rcppeigen_hello_world.o" symbols.rds' had status 2
    ERROR: compilation failed for package 'CRTRCppEigen'
    * removing 'C:/Users/LJH/Documents/CRTRCppEigen.Rcheck/CRTRCppEigen'

但在这种情况下的Rcpp包有效。所以我猜C ++代码有问题。

1 个答案:

答案 0 :(得分:1)

您的错误是

CRTRCppEigen.cpp:3:20:致命错误:Rcpp.h:没有这样的文件或目录

这不应该发生在LinkingTo: Rcpp中似乎有DESCRIPTION的情况。

要检查,我只是在Windows框中执行此操作:

R> setwd("c:/temp")
R> RcppEigen.package.skeleton(name="quicktest")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './quicktest/Read-and-delete-me'.

Adding RcppEigen settings
 >> added Imports: Rcpp, RcppEigen
 >> added LinkingTo: Rcpp, RcppEigen
 >> added useDynLib directive to NAMESPACE
 >> added Makevars file
 >> added Makevars.win file
 >> added example src file using Eigen classes
 >> invoked Rcpp::compileAttributes to create wrappers
R> 

然后直接安装:

R> install.packages("quicktest", repos=NULL, type="source")
Installing package into ‘c:/opt/R-library’
(as ‘lib’ is unspecified)
* installing *source* package 'quicktest' ...
** libs

*** arch - i386
g++ -m32 -I"C:/opt/R-CURR~1/include" -DNDEBUG    -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/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"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
g++ -m32 -shared -s -static-libgcc -o quicktest.dll tmp.def RcppExports.o rcppeigen_hello_world.o -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/quicktest/libs/i386

*** arch - x64
g++ -m64 -I"C:/opt/R-CURR~1/include" -DNDEBUG    -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/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"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
g++ -m64 -shared -s -static-libgcc -o quicktest.dll tmp.def RcppExports.o rcppeigen_hello_world.o -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/quicktest/libs/x64
** R
** preparing package for lazy loading
** help
Warning: c:/TEMP/quicktest/man/quicktest-package.Rd:32: All text must be in a section
Warning: c:/TEMP/quicktest/man/quicktest-package.Rd:33: 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 (quicktest)
R> 

正如你所看到它按预期工作所以我建议你将空的骨架包与你的比较。

FWIW我也“通常”在命令行上工作,所以你在那里做的也很好看。也就是说,你的R PATH条目已经结束,手册有点强烈建议将它们放在前面。