From romain.francois at dbmail.com  Sat Jan  9 20:27:08 2010
From: romain.francois at dbmail.com (Romain Francois)
Date: Sat, 09 Jan 2010 20:27:08 +0100
Subject: [Rd] PROTECT and OCaml GC.
In-Reply-To: <4B47D60B.8090007@citycable.ch>
References: <4B11B75C.9070806@citycable.ch>	<8ec76080911281625q5d5b8824hefb14af9b23d29f0@mail.gmail.com>	<4B11C55E.2060909@citycable.ch>	<F11B6C18-26EC-41F8-9EEB-A614D16945AC@r-project.org>	<4B12C417.1050308@citycable.ch>	<65D411CD-34C3-4B89-8FF0-98C9BD7613FB@r-project.org>	<4B47D439.8090304@citycable.ch>
	<4B47D60B.8090007@citycable.ch>
Message-ID: <4B48D88C.80604@dbmail.com>

On 01/09/2010 02:04 AM, Guillaume Yziquel wrote:
> Guillaume Yziquel a ?crit :
>> Simon Urbanek a ?crit :
>>>
>>> If you have suggestions for extending the API, feel free to post them
>>> with exact explanations how in general that extensions could be
>>> useful (general is the key word here - I think so far it was rather
>>> to hack around your way of implementing it). [And FWIW tryEval *is*
>>> part of the API].
>>
>> Concerning tryEval, I had a look at context.c, and it says:
>>
>>> /*
>>> R_tryEval is in Rinternals.h (so public), but not in the API.
>>> */
>
> And concerning tryEval, there's one feature I miss: there is error
> handling capabilities, but to my knowledge, it provides a boolean status
> back. True or false. Is it possible to get a full error message back, so
> that it can be analysed and translated to OCaml exceptions?

Not to my knowledge, what we do in Rcpp is to to bring the expression to 
the R side, evaluate it within a tryCatch and then grab either the 
result if successfull, or the error.

The R side of this is in :
http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/R/exceptions.R?rev=249&root=rcpp&view=markup

and the C++ class is the Evaluator class:
http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/src/Evaluator.cpp?rev=260&root=rcpp&view=markup

It smells the "it works but ..." pattern.

tryEval is just a thin wrapper around R_ToplevelExec (in context.c), 
what you need (and we need also in Rcpp) is a somewhat improved version 
of R_ToplevelExec which would modify the context. This would remove the 
need for a round trip to the R side...

I once tried to factor R_ToplevelExec out of R into a package, but 
failed. When I get a better understanding of contexts, I might propose 
something.

>> Now, here's a feature I'd like to implement with the API:
>>
>> In OCaml, there's a library called Lwt. It's a library implementing
>> lightweight, or green, threads.
>>
>> What I want to do is to be able to launch some R code, and have it
>> multithread with other OCaml code, within one single real thread.
>> Therefore I have to implement the commutation context somewhere in the
>> evaluation mechanism of R itself.
>>
>> The API doesn't support that, I guess.
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/JOlc : External pointers with Rcpp
|- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009
`- http://tr.im/IW9B : C++ exceptions at the R level



[Rd] PROTECT and OCaml GC.
Romain Francois