Rcpp:错误:与请求的类型不兼容 [英] Rcpp: Error: not compatible with requested type

查看:50
本文介绍了Rcpp:错误:与请求的类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 C++ 代码:

I have this C++ code:

#include <R.h>
#include <Rcpp.h>
using namespace Rcpp;
extern "C" {
  SEXP gensampleRcpp2( Function rlawfunc, SEXP n) {
    Rcpp::RNGScope __rngScope;
    return Rcpp::List::create(Rcpp::Named("sample") = rlawfunc(n),
                   Rcpp::Named("law.name") = " ",
                   Rcpp::Named("law.pars") = R_NilValue);
  }

  RcppExport SEXP gensampleRcpp(SEXP rlawfuncSEXP, SEXP nSEXP) {
    BEGIN_RCPP
    Function rlawfunc = Rcpp::as<Function >(rlawfuncSEXP);
    IntegerVector n = Rcpp::as<IntegerVector >(nSEXP);
    SEXP __result = gensampleRcpp2(rlawfunc, n);
    return Rcpp::wrap(__result);
    END_RCPP
      }

  SEXP compquantRcpp2(IntegerVector n, IntegerVector M, Function Rlaw) {
    int i;
    GetRNGstate();
    for (i=1;i<=M[0];i++) {
    List resultsample = gensampleRcpp2(Rlaw, n);
    NumericVector mysample = Rcpp::as<NumericVector >(resultsample["sample"]);
    }
    PutRNGstate();
    return Rcpp::List::create(Rcpp::Named("law.pars") = "");
  }

  RcppExport SEXP compquantRcpp(SEXP nSEXP, SEXP MSEXP, SEXP RlawSEXP) {
    BEGIN_RCPP
    IntegerVector n = Rcpp::as<IntegerVector >(nSEXP);
    IntegerVector M = Rcpp::as<IntegerVector >(MSEXP);
    Function Rlaw = Rcpp::as<Function >(RlawSEXP);
    SEXP __result = compquantRcpp2(n, M, Rlaw);
    return Rcpp::wrap(__result);
    END_RCPP
      }
}

还有这个 R 代码:

compquant <- function(n=50,M=10^3,Rlaw=rnorm) {
  out <- .Call("compquantRcpp",n=as.integer(n),M=as.integer(M),as.function(Rlaw),PACKAGE="PoweR") 
  return(out)
}

在一个名为 PoweR 的包中(实际上上面的代码是我自己代码的简化,所以不要试图理解它的目的).当我编译我的包(在 Linux 和 R 版本 3.1.0 下)并在控制台中发出以下 R 命令时:

in a package called PoweR (in fact the above codes are simplifications of my own code so do no try to understand the purpose of it). When I compile my package (under Linux and R version 3.1.0) and issue the following R command in the console:

require(PoweR)
compquant()

我收到以下错误:错误:与请求的类型不兼容

您知道可能是什么问题以及如何解决吗?

Do you have any idea on what could be the problem and how to solve it?

谢谢.

推荐答案

我只需要删除第 6 行:Rcpp::RNGScope __rngScope;来解决问题.话虽如此,Dirk Eddelbuettel 就 rcpp-devel 如何大大简化整个过程给出了很好的提示.非常感谢德克.

I just had to remove the 6th line: Rcpp::RNGScope __rngScope; to solve the problem. This being said, Dirk Eddelbuettel gave very good hints on rcpp-devel on how to greatly simplify the whole process. So thank you very much Dirk.

这篇关于Rcpp:错误:与请求的类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆