From ripley at stats.ox.ac.uk  Sun Apr  8 17:53:50 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Sun, 8 Apr 2007 16:53:50 +0100 (BST)
Subject: [Rd] Rf_PrintValue problem with methods::show
In-Reply-To: <5DAFC0A4-EAF5-4692-BE65-0C19F843154B@r-project.org>
References: <eb555e660704071633sf733745s897754e30f8eed44@mail.gmail.com>
	<5DAFC0A4-EAF5-4692-BE65-0C19F843154B@r-project.org>
Message-ID: <Pine.LNX.4.64.0704081642130.8106@gannet.stats.ox.ac.uk>

Simon,

There are a couple of subtle issues here.  As I wrote, PrintValue 
evaluates in the base environment, so sees only base functions.  It is not 
clear to me that is the right place (why the base environment and not the 
base namespace, for example), and one might consider evaluating in 
R_GlobalEnv.

However, as I understand it, one can load the methods namespace and turn 
S4 dispatch on without attaching the package.  So it seems entirely 
possible (but unusual) to have a package import 'methods' and then create 
S4 objects, and expect autoprinting to work.  That's why PrintValueEnv has 
to get show() from the methods namespace (at least as a last resort).

Now, there is an issue as to what autoprinting should do if a user defines 
his own version of print or show higher up the search path.  At present I 
believe the users's version is used for autoprinting, so we might want to 
preserve those semantics.  There are also issues with the visibility of S3 
print methods, and possible discrepancies in the choice between methods on 
the search path and registered ones.

I need to think through what the best fix is (suggestions welcome), and 
get this into 2.5.0 as a bug fix (I won't have time until Tuesday at the 
earliest).

Brian

On Sun, 8 Apr 2007, Simon Urbanek wrote:

> Deepayan,
>
> you fail to load the methods package, so you cannot use S4. Eval
> "library(methods)" first then everything is fine.
>
> Cheers,
> Simon
>
> On Apr 7, 2007, at 7:33 PM, Deepayan Sarkar wrote:
>
>> Hi,
>>
>> I think this is a bug (even though I can't find documentation
>> explicitly saying that it should work). Basically, Rf_PrintValue(obj)
>> fails when 'obj' is an S4 object that should be printed using show()
>> rather than print(). From the error message I'm guessing that the need
>> to use show is detected correctly but then show is not found.
>>
>> "cbind2" in the code below is just a representative object, the same
>> holds for the few other S4 objects I have tried.
>>
>> The following uses r-devel on i686-pc-linux-gnu, but the results are
>> same with R 2.4.1 (with suitable changes to the R_ParseVector call).
>> On powerpc Linux I get a segfault.
>>
>>
>> $ RPROG=R-devel
>> $ ${RPROG} --version | head -1
>> R version 2.6.0 Under development (unstable) (2007-04-06 r41080)
>> $ export LD_LIBRARY_PATH=`${RPROG} RHOME`/lib
>> $ cat parseEvalS4.c
>>
>> #include <Rinternals.h>
>> #include <Rembedded.h>
>> #include <R_ext/Parse.h>
>>
>> int main(int argc, char *argv[])
>> {
>>     SEXP e, val;
>>     int i, errorOccurred;
>>     ParseStatus status;
>>     char *cmds[] = { "show(5)", "show(cbind2)", "cbind2" };
>>
>>     argv[0] = "R";
>>     Rf_initEmbeddedR(argc, argv);
>>
>>     for (i = 0; i < 3; i++) {
>>         printf("** I **: Executing command: %s\n", cmds[i]);
>>         PROTECT(e = R_ParseVector(mkString(cmds[i]), -1, &status,
>> R_NilValue));
>>         val = eval(VECTOR_ELT(e, 0), R_GlobalEnv);
>>         printf("** I **: Evaluation succeeded, now printing using
>> Rf_PrintValue:\n");
>>         Rf_PrintValue(val);
>>         UNPROTECT(1);
>>     }
>>
>>     Rf_endEmbeddedR(0);
>>     return 0;
>> }
>>
>> $
>> $ gcc -o parseEvalS4 \
>>>        `${RPROG} CMD config --cppflags` \
>>>        `${RPROG} CMD config --ldflags` parseEvalS4.c
>> $
>> $ ${RPROG} CMD ./parseEvalS4 --vanilla --silent
>> ** I **: Executing command: show(5)
>> [1] 5
>> ** I **: Evaluation succeeded, now printing using Rf_PrintValue:
>> [1] 5
>> ** I **: Executing command: show(cbind2)
>> standardGeneric for "cbind2" defined from package "methods"
>>
>> function (x, y)
>> standardGeneric("cbind2")
>> <environment: 0x8b34ef8>
>> Methods may be defined for arguments: x, y
>>
>> ** I **: Evaluation succeeded, now printing using Rf_PrintValue:
>> NULL
>> ** I **: Executing command: cbind2
>> ** I **: Evaluation succeeded, now printing using Rf_PrintValue:
>> Error: could not find function "show"
>> Error: unprotect(): only 0 protected items
>> $
>>
>> -Deepayan
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Rf_PrintValue problem with methods::show
Prof Brian Ripley