我使用的自定义R包具有用C编写的功能(存储在/ src中)。当我安装(devtools :: install())并加载程序包时,可以在控制台中调用R函数,但不能将我的C函数写在/ src文件夹的.cpp文件中。
我尝试使用RcppArmadillo.package.skeleton()使用https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&ved=2ahUKEwiV64ScmYfjAhUSDxQKHQVKAMUQFjACegQIAhAC&url=http%3A%2F%2Fweb.mit.edu%2Finsong%2Fwww%2Fpdf%2Frpackage_instructions.pdf&usg=AOvVaw0jHWALZod_ngv4urh6QrFW中描述的过程从头开始重做该软件包。
但是,命令:
Rcpp:compileAttributes(verbose = TRUE)
返回错误:
Error in Rcpp::compileAttributes() :
Evaluation error: no native symbols were extracted.
我尝试了本文中建议的解决方案:Rcpp::compileAttributes() Error 但没有任何帮助,他们还返回“未提取任何本地符号”。
在使用Rcpp示例(“ anPackage”)时,compileAttributes()函数可以很好地工作。
在/ src中的.cpp文件中直接使用sourceCpp时,该功能运行良好。
以下是该函数之前的内容(在.cpp文件中)
#include <string>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp ;
// [[Rcpp::export]]
SEXP GetBBoxesAsList( SEXP x ){
// determines object type and adapts the search of coordinates
... }
直到几天前,该系统一直可以正常工作。 有人可以帮助我理解为什么我的C函数无法在R中调用吗?我在.cpp文件中调用Rcpp的方式是否正确? 除了C函数,我的程序包在加载时运行良好。
我使用的是Ubuntu 18.04和R 3.4.4。
非常感谢。
编辑
调用/ R / myRscript内的/src/myscript.cpp中创建的C函数会导致
中的错误Rcpp:compileAttributes(verbose = TRUE)
该函数在src / myscript.cpp中定义为:
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
#include "string"
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
SEXP GetBBoxesAsList( SEXP x ){
// determines object type and adapts the search of coordinates
S4 obj(x) ;
std::string nameList;
std::string nameSubList;
if(Rf_inherits(x, "SpatialLines") || Rf_inherits(x, "SpatialLinesDataFrame")){
nameList = "lines";
nameSubList = "Lines";
}else if(Rf_inherits(x, "SpatialPolygons") || Rf_inherits(x, "SpatialPolygonsDataFrame")){
nameList = "polygons";
nameSubList = "Polygons";
}else{
::Rf_error("In GetBBoxes, class must be Spatial[Polygons|Lines][DataFrame]");
}
List a = obj.slot(nameList);
// count items
int nPol = a.length();
// NumericMatrix bboxes(nPol,4);
List bboxes(nPol);
// get the range
for(int iPol = 0;iPol < nPol;iPol++){
S4 pol = a(iPol);
List b = pol.slot(nameSubList);
double minX = std::numeric_limits<double>::infinity();
double maxX = -std::numeric_limits<double>::infinity();
double minY = std::numeric_limits<double>::infinity();
double maxY = -std::numeric_limits<double>::infinity();
for(int iSP = 0; iSP < b.length(); iSP++){
S4 subPol = b(iSP);
NumericMatrix coords = subPol.slot("coords");
// X
NumericVector rangeX = range(coords(_,0));
if(rangeX(0)<minX) minX = rangeX(0);
if(rangeX(1)>maxX) maxX = rangeX(1);
// Y
NumericVector rangeY = range(coords(_,1));
if(rangeY(0)<minY) minY = rangeY(0);
if(rangeY(1)>maxY) maxY = rangeY(1);
}
NumericVector bbox(4);
bbox(0) = minX;
bbox(1) = minY;
bbox(2) = maxX;
bbox(3) = maxY;
bboxes(iPol) = bbox;
// bboxes(iPol,0) = minX;
// bboxes(iPol,1) = minY;
// bboxes(iPol,2) = maxX;
// bboxes(iPol,3) = maxY;
}
// Rcpp::DataFrame BBoxes = Rcpp::DataFrame::create(Rcpp::Named("minX")=bboxes(_,0),
// Rcpp::Named("minY")=bboxes(_,1),
// Rcpp::Named("maxX")=bboxes(_,2),
// Rcpp::Named("maxY")=bboxes(_,3));
return bboxes;// BBoxes;
}
并在/ R / myRscipt中以
调用# sourceCpp("src/GetBBoxes.cpp")
# see src/GetBBoxes for GetBBoxesAsList
#' @export
GetBBoxes <-function(sp,outType="data.frame"){
out <- GetBBoxesAsList(sp)
if(outType=="data.frame"){
out <- data.frame(matrix(unlist(out),ncol=4,byrow=TRUE))
names(out) <- c("minX","minY","maxX","maxY")
}
return(out)
}
当我从/R/myRscript.R中删除上述函数时,
Rcpp::compileAttributes()
它将更新R/RcppExports.R.
中的功能
重命名功能不能解决问题。