From ripley at stats.ox.ac.uk  Wed Apr  4 09:38:19 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 4 Apr 2007 08:38:19 +0100 (BST)
Subject: [Rd] R callbacks
In-Reply-To: <eb555e660704031617j6ce3f5ebk4ad236e58cf9274a@mail.gmail.com>
References: <eb555e660704021751i30f8854m9f83c81d966ef6a@mail.gmail.com>
	<7DAE25C3-CAD3-4491-8B1E-7A1A5D2EDC49@r-project.org>
	<eb555e660704031617j6ce3f5ebk4ad236e58cf9274a@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0704040702200.8290@gannet.stats.ox.ac.uk>

On Tue, 3 Apr 2007, Deepayan Sarkar wrote:

> On 4/3/07, Simon Urbanek <simon.urbanek at r-project.org> wrote:
>> On Apr 2, 2007, at 8:51 PM, Deepayan Sarkar wrote:
>>
>>> Hi,
>>>
>>> I'm trying to understand (mostly from the R-exts manual) how to use
>>> the callbacks declared in Rinterface.h. As a first attempt, I'm trying
>>> to redefine ptr_R_WriteConsole in a very trivial manner. Here's my
>>> code:
>>>
>>> ---------------
>>> $ cat altr.c
>>>
>>> int Rf_initialize_R(int ac, char **av);
>>>
>>> #define R_INTERFACE_PTRS 1
>>> #include <Rinterface.h>
>>>
>>> extern int R_running_as_main_program;
>>>
>>> static void my_R_WriteConsole(char *buf, int len)
>>> {
>>>     printf("R<< %s", buf);
>>> }
>>>
>>> int main(int ac, char **av)
>>> {
>>>     R_running_as_main_program = 1;
>>>     ptr_R_WriteConsole = my_R_WriteConsole;
>>>     Rf_initialize_R(ac, av);
>>>     Rf_mainloop();
>>>     return 0;
>>> }
>>> ---------------
>>>
>>> I compile and run this in bash with
>>>
>>>
>>> RPROG=R
>>>
>>> INC=`${RPROG} CMD config --cppflags`
>>> LDF=`${RPROG} CMD config --ldflags`
>>> export LD_LIBRARY_PATH=`${RPROG} RHOME`/lib
>>> gcc -o altr ${INC} ${LDF} altr.c
>>>
>>> ${RPROG} CMD ./altr
>>>
>>> However, my customized version seems not to be used. What am I
>>> missing?
>>>
>>
>> 1) see Byron's e-mail
>> 2) by default R uses stdout/err connections instead of the console
>> API, you need to disable that
>>
>> so the resulting code should look like this:
>>
>>      Rf_initialize_R(ac, av);
>>      ptr_R_WriteConsole = my_R_WriteConsole;
>>      R_Outputfile = NULL;
>>      R_Consolefile = NULL;
>>      Rf_mainloop();
>
> Thanks, setting things to NULL was the critical piece (I had tried
> Byron's suggestion myself). A note mentioning this in R-exts would be
> helpful.

Done for 2.5.0.

-- 
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] R callbacks
Prof Brian Ripley