如何从C创建新的R环境?

bk.*_*bk. 6 c r

我无法找到这方面的文件.它看起来像R源代码使用NewEnvironmentR_NewHashedEnv,但它们都不在公共标题中,所以看起来它们不能作为用户使用.我应该使用什么功能(或什么行代码)来生成新的ENVSXP?

Jos*_*ich 3

你要allocSExp

/* C code in foo.c */
#include "Rinternals.h"
SEXP foo() {
  SEXP res = allocSExp(ENVSXP);
  return res;
}
Run Code Online (Sandbox Code Playgroud)
> # R code (after running R CMD SHLIB foo.c)
> dyn.load("foo.dll")
> .Call("foo")
<environment: 0x016a4084>
Run Code Online (Sandbox Code Playgroud)

  • 显然不需要 PROTECT,因为“allocSExp”*之后*没有分配并且(ENVSXP 不是 R 对象) (2认同)
  • @SimonUrbanek:你真的应该接受你的评论(包括你删除的评论:)并制定你自己的答案。你有很多智慧可以分享,但我不想把它埋在评论中。 (2认同)