从C评估R命令不起作用

用户名

我试图做一个简单的R命令评估器。

我有以下代码:

评估者

void evaluate(const std::string &command);

extern "C" SEXP run_eval() {
  evaluate("grDevices:::png(\"snapshot.png\")");

  return R_NilValue;
}

评估器

SEXP createSexp(const std::string &str, ScopeProtector *protector) {
  SEXP result = Rf_allocVector(STRSXP, 1);

  protector->add(result);

  SET_STRING_ELT(result, 0, Rf_mkChar(str.c_str()));

  return result;
}

SEXP createExpressionSexp(SEXP strSexp, ScopeProtector *protector) {
  ParseStatus status;

  SEXP result = R_ParseVector(strSexp, 1, &status, R_NilValue);

  protector->add(result);

  return result;
}

SEXP createExpressionSexp(const std::string &str, ScopeProtector *protector) {
  return createExpressionSexp(createSexp(str, protector), protector);
}

SEXP evaluateExpression(SEXP exprSexp, ScopeProtector *protector) {
  SEXP result = Rf_eval(exprSexp, R_GlobalEnv);

  protector->add(result);

  return result;
}

void evaluate(const std::string &command) {
  ScopeProtector protector;

  evaluateExpression(createExpressionSexp(command, &protector), &protector);
}

范围保护器

class ScopeProtector: boost::noncopyable {
 public:
  ScopeProtector();

  virtual ~ScopeProtector();

  void add(SEXP sexp);

 private:
  class Impl;

  const std::unique_ptr<Impl> pImpl;
};

ScopeProtector.cpp

class ScopeProtector::Impl {
 public:
  Impl() : count(0) {
  }

  virtual ~Impl() {
    if (count > 0) {
      UNPROTECT(count);
    }
  }

  void add(SEXP sexp) {
    PROTECT(sexp);
    count++;
  }

 private:
  int count;
};

ScopeProtector::ScopeProtector() : pImpl(new Impl) {
}

ScopeProtector::~ScopeProtector() = default;

void ScopeProtector::add(SEXP sexp) {
  pImpl->add(sexp);
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(REvaluator)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")

include_directories("/usr/share/R/include")

set(
        SOURCE_FILES
        Evaluator.cpp Evaluator.h
        ScopeProtector.cpp ScopeProtector.h
)

add_library(revaluator SHARED ${SOURCE_FILES})

我正在尝试通过执行以下命令来检查我的评估器:

> dyn.load("librevaluator.so")
> .Call("run_eval")
NULL
> dev.list()
NULL

如您所见,没有任何png设备。你能说出什么问题吗?

用户名

我找到了解决方案。只需更换

SEXP result = Rf_eval(exprSexp, R_GlobalEnv);

SEXP result = Rf_eval(VECTOR_ELT(exprSexp, 0), R_GlobalEnv);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章