【问题标题】:Rcpp trouble importing 'hessian' from R package 'numDeriv'Rcpp 无法从 R 包“numDeriv”导入“hessian”
【发布时间】:2018-11-10 05:36:58
【问题描述】:

我正在尝试构建一个使用包“numDeriv”中的函数“hessian”的包。但是,当我构建包并运行代码时,我得到了错误

无法将对象转换为环境:[type=character;目标=ENVSXP]。

下面的示例简化 Rcpp 代码

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <stdio.h> 
#include<armadillo>
using namespace Rcpp;
using namespace std;


double testfunc(double X){

  return pow(X+1,2);

}


double hessian_rcpp(double X){

  Rcpp::Environment numDeriv("package:numDeriv");
  Rcpp::Function hessian = numDeriv["hessian"];

  Rcpp::List hessian_results = hessian(
  Rcpp::_["func"] = Rcpp::InternalFunction(testfunc), 
    Rcpp::_["x"] = X);

  arma::vec out = Rcpp::as<arma::vec>(hessian_results[0]);

  return out[0];
}

// [[Rcpp::export]]
double returnhess(double X){

  double out = hessian_rcpp(X);

  return out;

}

然后在构建包后运行以下 R 代码会导致错误。

library(test)
returnhess(X=3)
Error in returnhess(X = 3) : 
Cannot convert object to an environment: [type=character; target=ENVSXP].

我的命名空间是

useDynLib(test, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")

我的描述是

Package: test
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line) Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Imports: Rcpp, RcppArmadillo, numDeriv
LinkingTo: Rcpp, RcppArmadillo, numDeriv
Encoding: UTF-8
LazyData: true

我的R版本是3.5.1,RStudio版本是1.1.456,Rcpp版本是0.12.19,RcppArmadillo版本是0.9.100.5.0,numDeriv版本是2016.8.1。我的操作系统是 windows 10。

我能够通过类似的方法从 R 包“stats”中成功导入“optimize”。下面的示例简化代码

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <stdio.h> 
#include<armadillo>
using namespace Rcpp;
using namespace std;

  double testfunc(double X){

  return pow(X+1,2);

}

double optim_rcpp(){

  Rcpp::Environment stats("package:stats");
  Rcpp::Function optimize = stats["optimize"];

  Rcpp::List opt_results = optimize(
  Rcpp::_["f"] = Rcpp::InternalFunction(testfunc), 
  Rcpp::_["lower"] = -10, 
  Rcpp::_["upper"] =  10);

  arma::vec out = Rcpp::as<arma::vec>(opt_results[0]);

  return out[0];
}

// [[Rcpp::export]]
double returnoptim(){

  double out = optim_rcpp();

  return out;

}

与上面相同的命名空间和描述

然后运行下面的 R 代码就可以了

returnoptim()
[1] -1

【问题讨论】:

  • 我忘了提到如果我在运行 library(numDeriv) 或 require(numDeriv) 之前运行它,returnhess 有效。但是,如果它在打包的 R 函数中,则在运行 devtools::check() 时会发出警告。它最终是。如果我尝试 requireNamespace(numDeriv) 它将不起作用
  • 您是否尝试过与上一个问题stackoverflow.com/q/52952775/8416610类似的方法
  • @RalfStubner,我有。它没有用:(我的最终目标是在 CRAN 上获得这个包。我试图避免“要求”解决方法,因为它会发出警告并可能导致我的包不被接受。

标签: r rcpp


【解决方案1】:

作为一种解决方法,您可以添加

 Depends:numDeriv

到您的DESCRIPTION。这可确保 numDeriv 包与您的包一起加载。

顺便说一句:我会避免在包中使用using namespace Rcpp;。而且我永远不会使用using namespace std;。我想不出使用#include &lt;stdio.h&gt; 的充分理由,而#include&lt;armadillo&gt; 在使用RcppArmadillo 时是不必要的。

【讨论】:

  • 成功了,谢谢!!我也很欣赏其他提示。我在那里有#include &lt;stdio.h&gt;,因为我在构建包时收到错误“退出状态15”。但是这个错误现在已经消失了。
猜你喜欢
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2017-08-15
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
相关资源
最近更新 更多