From guillaume.yziquel at citycable.ch  Sat Nov 14 23:52:03 2009
From: guillaume.yziquel at citycable.ch (Guillaume Yziquel)
Date: Sat, 14 Nov 2009 23:52:03 +0100
Subject: [Rd] R_tryEval for OCaml-R.
In-Reply-To: <19198.55740.785453.541164@ron.nulle.part>
References: <4AFED087.7090603@citycable.ch>
	<19198.55740.785453.541164@ron.nulle.part>
Message-ID: <4AFF3493.1080609@citycable.ch>

Dirk Eddelbuettel a ?crit :
> On 14 November 2009 at 16:45, Guillaume Yziquel wrote:
> 
> | I've had a look at R-exts.pdf, but R_tryEval is not documented.
> | 
> | Could someone please point me to relevant documentation, 
> 
> I don't think it is documented as it is not part of the API -- see for
> example R's src/main/context.c where you find
> 
>    R_tryEval is in Rinternals.h (so public), but not in the API.

Thanks for your help, Dirk and Romain.

I've gathered that 'eval' and R_tryEval are roughly equivalent, except 
that R_tryEval has error checking mechanisms. Please correct me if I'm 
wrong.

Here's the code I've been writing:

> yziquel at seldon:~/sandbox/ocaml-r/src$ grep -A 16 r_eval_sexp_list r_base.c 
> CAMLprim value r_eval_sexp_list (value sexp_list) {
>   CAMLparam1(sexp_list);
> 
>   SEXP sexp2eval = (SEXP) Long_val(Field(sexp_list,0));
>   SEXP e;
>   int error = 0;
> 
>   /* Should this be wrapped with a PROTECT() and
>      an UNPROTECT(1)? */
>   e = R_tryEval(sexp2eval, R_GlobalEnv, &error);
> 
>   if (error) {caml_failwith(
>     "OCaml-R error in eval_sexp_list C stub."
>   );};
> 
>   CAMLreturn(Val_sexp(e));
> }
> yziquel at seldon:~/sandbox/ocaml-r/src$ 

Can be found over there:

http://svn.gna.org/viewcvs/ocaml-r/trunk/src/r_base.c?rev=45&view=markup

It seems to me that I should be wrapping the call to R_tryEval with a 
PROTECT(). Not too sure where to put the UNPROTECT(1) for now.

I've however been looking more closely at

http://cran.r-project.org/doc/manuals/R-exts.html#Evaluating-R-expressions-from-C

and more specifically at the function lapply2 and the piece of code just 
afterwards, from src/main/print.c. I'm therefore wondering what should 
the first argument of the LISTSXP be. In lapply2, it seems that it's an 
R function per se, whereas in the code from src/main/print.c, it seems 
that it's more a symbol.

There's very probably something obvious that I do not yet get.

What I'd like, is my r_eval_sexp_list to be pure SEXPs. Not symbols. I 
want to transparently move SEXPs from R to OCaml, and backwards. So 
ideally, the first element of the LISTSXP would be a function and 
nothing else.

So my question: what exactly is the first element of the LISTSXP 
supposed to be?

Please tell me if I'm going offtrack here.

All the best,

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/



[Rd] R_tryEval for OCaml-R.
Guillaume Yziquel