The title basically says it all.
标题基本上都说明了一切。
If I do this ...
如果我这样做......
makeActiveBinding("x", function() runif(2), .GlobalEnv)
x
# [1] 0.7332872 0.4707796
x
# [1] 0.5500310 0.5013099
... is there then any way for me to examine x
to learn what function it is linked to (and if not, why not)?
...那么我有什么方法可以检查x以了解它所链接的功能(如果没有,为什么不呢)?
(In this case, I'd like to be able to learn that x
was defined to be function() runif(2)
.)
(在这种情况下,我希望能够知道x被定义为function()runif(2)。)
1 个解决方案
#1
11
With a bit of noodling around in envir.c
, I can get this to work:
在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 ;
}
Save this into a .cpp
file, sourceCpp
it and use it with this R code:
将其保存到.cpp文件中,sourceCpp并将其与此R代码一起使用:
> makeActiveBinding("x", function() runif(2), .GlobalEnv)
> get_binding_fun("x", .GlobalEnv)
# function ()
# runif(2)
#1
11
With a bit of noodling around in envir.c
, I can get this to work:
在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 ;
}
Save this into a .cpp
file, sourceCpp
it and use it with this R code:
将其保存到.cpp文件中,sourceCpp并将其与此R代码一起使用:
> makeActiveBinding("x", function() runif(2), .GlobalEnv)
> get_binding_fun("x", .GlobalEnv)
# function ()
# runif(2)