Rcpp相当于decarse

时间:2014-11-17 21:27:39

标签: r string rcpp

Rcpp中的任何函数是否等同于deparse? 例如,deparse(list(a=1, b = "foo"))中的Rcpp相当于什么? 像,

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

如果无法做到这一点,我知道R中的deparse是内部实施的deparse.c。关于如何在Rcpp中调用它的任何建议?

2 个答案:

答案 0 :(得分:3)

您可以使用R功能:

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

答案 1 :(得分:0)

经过更多搜索后,我偶尔会在gist

之前看到Romain Francois

代码扫描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;