楼主: bzmctj
7508 3

[问答] R调用Rcpp的C++函数,总是出错。 [推广有奖]

  • 0关注
  • 0粉丝

本科生

19%

还不是VIP/贵宾

-

威望
0
论坛币
196 个
通用积分
1.3049
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
259 点
帖子
20
精华
0
在线时间
117 小时
注册时间
2009-2-22
最后登录
2023-11-14

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
下载了一个R包,是用R调用C++写成的,也就是用到Rcpp。我想调用其中一个函数fastLasso,然后在此基础上修改。      里面包含了不少.R文件,.cpp文件和.h文件。也就是说同名的有fastLasso.R, fastLasso.cpp以及fastLasso.h。其中fastLasso.cpp和 fastLasso.h在一个文件夹中。
      我试着调用其中的C++函数fastLasso.cpp。

       我安装了Rcpp,Rtools,RcppArmadillo。
      用sourceCpp(fastLasso.cpp),但是出了以下错误:


(前面的两行g++省略)
fastLasso.o:fastLasso.cpp:(.text+0x1689): undefined reference to `seqLen(unsigned int const&)'
fastLasso.o:fastLasso.cpp:(.text+0x2cfa): undefined reference to `sign(double const&)'
collect2: ld returned 1 exit status
Error in sourceCpp("fastLasso.cpp") :
  Error occurred building shared library.
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="G:/0notesun1412/robust HD/robustHD/src/../inst/include": 系统找不到指定的文件。




下面是fastLasso.cpp,由于程序太长,我只贴前面部分和后面的R interface:
#include "fastLasso.h"

using namespace Rcpp;
using namespace arma;


// find maximum number of active variables
// n .............. number of observations
// p .............. number of predictors
// useIntercept ... logical indicating whether model has an intercept
uword findMaxActive(const uword& n, const uword& p, const bool& useIntercept) {
        uword maxActive = n - useIntercept;
        if(p < maxActive) {
                maxActive = p;
        }
        return maxActive;
}

// compute step size in the direction of the equiangular vector
// corActiveY.. ... correlations of active variables with current response
// corInactiveY ... correlations of inactive variables with current response
// corActiveU ..... correlations of active variables with equiangular vector
// corInactiveU ... correlations of inactive variables with equiangular vector
// eps ............ small numerical value (effective zero)
double findStep(const double& corActiveY, const vec& corInactiveY,
                const double& corActiveU, const vec& corInactiveU,
                const double& eps) {
        // construct vector of all values to consider
        vec steps = join_cols((corActiveY - corInactiveY)/(corActiveU - corInactiveU),
                        (corActiveY + corInactiveY)/(corActiveU + corInactiveU));
        steps = steps.elem(find(steps > eps));
        // find and return step size
        double step = corActiveY/corActiveU;      // maximum possible step;
        if(steps.n_elem > 0) {
                double smallestPositive = steps.min();  // smallest positive value
                if(smallestPositive < step) {
                        step = smallestPositive;
                }
        }
        return step;
}


省略……

// R interface to fastLasso()
SEXP R_fastLasso(SEXP R_x, SEXP R_y, SEXP R_lambda, SEXP R_useSubset,
                SEXP R_subset, SEXP R_normalize, SEXP R_intercept, SEXP R_eps,
    SEXP R_useGram) {
    // data initializations
        NumericMatrix Rcpp_x(R_x);                                                  // predictor matrix
        const int n = Rcpp_x.nrow(), p = Rcpp_x.ncol();
        mat x(Rcpp_x.begin(), n, p, false);                  // reuse memory
        NumericVector Rcpp_y(R_y);                                // response
        vec y(Rcpp_y.begin(), n, false);              // reuse memory
        double lambda = as<double>(R_lambda);
        bool useSubset = as<bool>(R_useSubset);
        uvec subset;
        if(useSubset) {
                IntegerVector Rcpp_subset(R_subset);        // subset to use for computation
                const int h = Rcpp_subset.size();
                subset = uvec(h);
                for(int i = 0; i < h; i++) {
      // can't use the same memory-saving conversion for integer vectors
                        subset(i) = Rcpp_subset - 1;
                }
        }
  bool normalize = as<bool>(R_normalize);
        bool useIntercept = as<bool>(R_intercept);
        double eps = as<double>(R_eps);
        bool useGram = as<bool>(R_useGram);
        // call native C++ function and return results as list
  double intercept, crit;
        vec coefficients, residuals;
        fastLasso(x, y, lambda, useSubset, subset, normalize, useIntercept, eps,
      useGram, false, intercept, coefficients, residuals, crit);
        if(useIntercept) {
                // prepend intercept
                coefficients.insert_rows(0, 1, false);
                coefficients(0) = intercept;
        }
        return List::create(
                        Named("coefficients") = coefficients,
                        Named("fitted.values") = y - residuals,
                        Named("residuals") = residuals
                        );
}


本人Rcpp是新手,自己看了、试了有一段时间了,还是不得要领。请给予指点,任何的帮助都很感激。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Rcpp R调用 coefficients correlations observations undefined reference 文件夹

沙发
DM小菜鸟 发表于 2015-3-2 16:20:49 |只看作者 |坛友微信交流群
“Error occurred building shared library”这个往往是因为有Rtool没有加载

使用道具

藤椅
jgzj_tina 发表于 2020-5-27 20:08:27 |只看作者 |坛友微信交流群
DM小菜鸟 发表于 2015-3-2 16:20
“Error occurred building shared library”这个往往是因为有Rtool没有加载
请问怎么加载rtools呢

使用道具

板凳
owenqi 在职认证  学生认证  发表于 2020-5-29 11:25:32 |只看作者 |坛友微信交流群
rtools需要另外安装的,另外如果是R 4.0.0对应的rtools还需要单独写入环境变量,在你下载rtools的地方就有教程。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-22 11:38