【问题标题】:Rcpp: Error: not compatible with requested typeRcpp:错误:与请求的类型不兼容
【发布时间】:2014-10-05 02:31:34
【问题描述】:

我有这个 C++ 代码:

#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 命令时:

require(PoweR)
compquant()

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

您对可能出现的问题以及如何解决问题有任何想法吗?

谢谢。

【问题讨论】:

  • 在 R 3.1.1 上使用 RStudio 0.98.1056 构建,x86_64-w64-mingw32/x64(64 位),compquant() 返回一个包含一个元素 law.pars 的列表,其中包括具有单个空元素的字符向量。
  • 这是交叉发布到 rcpp-devel,我跟进了那里。
  • 要回复jbaums,可以尝试发出以下命令,因为(i in 1:100) compquant()时不时会报错
  • 参见关于 rcpp-devel 的讨论;您可以通过让 Rcpp 属性为您处理 RNG 状态等事项来避免所有这些问题。

标签: r rcpp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多