r - 有什么方法可以访问由 makeActiveBinding 安装的函数?

标签 r scoping

标题基本上说明了一切。

如果我这样做...

makeActiveBinding("x", function() runif(2), .GlobalEnv)
x
# [1] 0.7332872 0.4707796
x
# [1] 0.5500310 0.5013099

...那我有什么办法可以检查x吗?了解它与什么功能相关联(如果没有,为什么不)?

(在这种情况下,我希望能够了解到 x 被定义为 function() runif(2) 。)

最佳答案

envir.c 周围有一些面条,我可以让它工作:

#include <Rcpp.h>
using namespace Rcpp ;

#define HASHSIZE(x)      LENGTH(x)
#define HASHVALUE(x)    TRUELENGTH(x)

// [[Rcpp::export]]
SEXP get_binding_fun( std::string name, Environment env){
    SEXP symbol = Rf_install( name.c_str() );
    SEXP tab = HASHTAB(env) ;
    SEXP c = PRINTNAME(symbol);

    // finding the hash code for the symbol
    int hashcode = HASHVALUE(c) % HASHSIZE(tab);

    // get the value there from the hash table
    SEXP res = CAR( VECTOR_ELT(tab, hashcode ) ) ;

    return res ;
}

将此保存到 .cpp文件, sourceCpp 它并将其与此 R 代码一起使用:
> makeActiveBinding("x", function() runif(2), .GlobalEnv)
> get_binding_fun("x", .GlobalEnv)
# function ()
# runif(2)

关于r - 有什么方法可以访问由 makeActiveBinding 安装的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069566/

相关文章:

根据标题开始的位置将 CSV 读入 R

c++ - 我可以使用 block 来管理 C++ 中的内存消耗吗?

javascript - ES6/Node 中的词法作用域

R vlookup基于最接近的数字变量

r - 使用 geom_segment 指示 geom_errorbar 超出 log10 比例限制

lisp - 关于 Common Lisp 作用域的微妙之处(动态与词法)

确定事件处理程序范围的 JavaScript 最佳实践

r - R 中的作用域和评估函数

r - 如何替换 `ggplot2::geom_histogram(` y = ..密度中的点对点表示法。 .`)`?

r - 如何一步获得带有嵌套的分组 Sum