简体   繁体   中英

Rcpp deparse equivalent

Is there any function in Rcpp that is equivalent to deparse ? For example, what would be an equivalent of deparse(list(a=1, b = "foo")) in Rcpp ? Something like,

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

If that is not possible, I know that deparse in R is internal implemented deparse.c . Any advise on how to call it in Rcpp?

You can just use the R function:

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

After more searching, I occasionally came across this gist by Romain Francois

The code scans through table of internal function defined in names.c . So it allows to get a pointer to any internal function, which then can be called.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM