【问题标题】:Rcpp deparse equivalentrcpp deparse 等价物
【发布时间】:2014-11-17 21:27:39
【问题描述】:

Rcpp中有没有等价于deparse的函数? 例如,Rcpp 中的 deparse(list(a=1, b = "foo")) 等价物是什么? 类似的,

// [[Rcpp::export]]
void WriteCapInfo (List args) {
  Rcout << deparse(args) << endl;   
}

如果那不可能,我知道 R 中的 deparse 是内部实现的 deparse.c。关于如何在 Rcpp 中调用它有什么建议吗?

【问题讨论】:

    标签: r string rcpp


    【解决方案1】:

    你可以使用R函数:

    Function deparse("deparse") ;
    Rf_PrintValue( deparse(args) ) ;
    

    【讨论】:

    • 谢谢,确实有效。但是,有没有什么方法可以避免调用R函数而调用C/C++函数呢?
    • 并非如此。 TBH,我认为这不值得。你有什么用例。否则,你知道代码在哪里,你可以随时复制它。
    【解决方案2】:

    经过更多的搜索,我偶然发现了Romain Francois这个gist

    代码扫描names.c中定义的内部函数表。因此,它允许获取指向任何内部函数的指针,然后可以调用该指针。

    typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
    
    typedef struct {
        PPkind kind;     /* deparse kind */
        PPprec precedence; /* operator precedence */
        unsigned int rightassoc;  /* right associative? */
    } PPinfo;
    
    typedef struct {
        char   *name;    /* print name */
        CCODE  cfun;     /* c-code address */
        int    code;     /* offset within c-code */
        int    eval;     /* evaluate args? */
        int    arity;    /* function arity */
        PPinfo gram;     /* pretty-print info */
    } FUNTAB;
    
    extern FUNTAB   R_FunTab[];     /* Built in functions */ 
    
    CCODE get_internal( std::string name){
        FUNTAB* p = R_FunTab ;
        for( ; p->name != NULL; ++p ){
            if( name == p->name )
                return p->cfun ;
        }
        return NULL ;
    }
    
    /* Information for Deparsing Expressions */
    typedef enum {
        PP_INVALID  =  0,
        PP_ASSIGN   =  1,
        PP_ASSIGN2  =  2,
        PP_BINARY   =  3,
        PP_BINARY2  =  4,
        PP_BREAK    =  5,
        PP_CURLY    =  6,
        PP_FOR      =  7,
        PP_FUNCALL  =  8,
        PP_FUNCTION =  9,
        PP_IF   = 10,
        PP_NEXT   = 11,
        PP_PAREN    = 12,
        PP_RETURN   = 13,
        PP_SUBASS   = 14,
        PP_SUBSET   = 15,
        PP_WHILE    = 16,
        PP_UNARY    = 17,
        PP_DOLLAR   = 18,
        PP_FOREIGN  = 19,
        PP_REPEAT   = 20
    } PPkind;
    
    typedef enum {
        PREC_FN  = 0,
        PREC_LEFT    = 1,
        PREC_EQ  = 2,
        PREC_RIGHT   = 3,
        PREC_TILDE   = 4,
        PREC_OR  = 5,
        PREC_AND     = 6,
        PREC_NOT     = 7,
        PREC_COMPARE = 8,
        PREC_SUM     = 9,
        PREC_PROD    = 10,
        PREC_PERCENT = 11,
        PREC_COLON   = 12,
        PREC_SIGN    = 13,
        PREC_POWER   = 14,
        PREC_DOLLAR  = 15,
        PREC_NS  = 16,
        PREC_SUBSET  = 17
    } PPprec;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2023-03-05
      • 2015-08-10
      • 2015-03-06
      相关资源
      最近更新 更多