[Rd] print.closure at the R level

Romain Francois romain.francois at dbmail.com
Sat Apr 18 20:30:42 CEST 2009


Duncan Murdoch wrote:
> On 18/04/2009 10:12 AM, Romain Francois wrote:
>> Hello,
>>
>> Could the code that auto prints a function/closure be extracted from 
>> print.c so that there would be a print.closure function.
>> I would like to be able to mask a print.closure function so that I 
>> have a custom auto-print. One reason for that is I plan to have 
>> syntax highlighting within the R console.
>
> The class of a closure is "function", so you'd want the method to be 
> print.function.  Currently that doesn't work for auto printing, so 
> your suggestion is still interesting.  (I'm not sure why auto printing 
> is special here...)
>
> Duncan Murdoch
Apparently, auto printing does not use the regular dispatch mechanism. 
See below:

I'll  make a more concrete proposal. This could be an opportunity to 
nail down as.character.function as well.

Romain

(from print.c)

 *  print.default()  ->     do_printdefault (with call tree below)
 *
 *  auto-printing   ->  PrintValueEnv
 *                      -> PrintValueRec
 *                      -> call print() for objects

and PrintValueRec switches on the typeof :

switch (TYPEOF(s)) {
...
    case CLOSXP:
    case LANGSXP:
    t = getAttrib(s, R_SourceSymbol);
    if (!isString(t) || !R_print.useSource)
        t = deparse1(s, 0, R_print.useSource | DEFAULTDEPARSE);
    for (i = 0; i < LENGTH(t); i++)
        Rprintf("%s\n", CHAR(STRING_ELT(t, i))); /* translated */
#ifdef BYTECODE
    if (TYPEOF(s) == CLOSXP && isByteCode(BODY(s)))
        Rprintf("<bytecode: %p>\n", BODY(s));
#endif
    if (TYPEOF(s) == CLOSXP) {
        t = CLOENV(s);
        if (t != R_GlobalEnv)
        Rprintf("%s\n", EncodeEnvironment(t));
    }
    break;





-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr



More information about the R-devel mailing list