Build a R call at C level

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Build a R call at C level

MorganMorgan
Hi All,

I was reading the R extension manual section 5.11 ( Evaluating R expression
from C) and I tried to build a simple call to the sum function. Please see
below.

call_to_sum <- inline::cfunction(
  language = "C",
  sig = c(x = "SEXP"), body = "

SEXP e = PROTECT(lang2(install(\"sum\"), x));
SEXP ans = PROTECT(eval(e, R_GlobalEnv));
UNPROTECT(2);
return ans;

")

call_to_sum(1:3)

The above works. My question is how do I add the argument "na.rm=TRUE" at C
level to the above call? I have tried various things based on what is in
section 5.11 but I did not manage to get it to work.

Thank you
Best regards

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Build a R call at C level

Jan Gorecki
It is quite known that R documentation on R C api could be improved...
Still R-package-devel mailing list should be preferred for this kind
of questions.
Not sure if that is the best way, but works.

call_to_sum <- inline::cfunction(
  language = "C",
  sig = c(x = "SEXP"), body = "

SEXP e = PROTECT(lang2(install(\"sum\"), x));
SEXP r_true = PROTECT(CONS(ScalarLogical(1), R_NilValue));
SETCDR(CDR(e), r_true);
SET_TAG(CDDR(e), install(\"na.rm\"));
Rf_PrintValue(e);
SEXP ans = PROTECT(eval(e, R_GlobalEnv));
UNPROTECT(3);
return ans;

")

call_to_sum(c(1L,NA,3L))

On Tue, Jun 30, 2020 at 10:08 AM Morgan Morgan
<[hidden email]> wrote:

>
> Hi All,
>
> I was reading the R extension manual section 5.11 ( Evaluating R expression
> from C) and I tried to build a simple call to the sum function. Please see
> below.
>
> call_to_sum <- inline::cfunction(
>   language = "C",
>   sig = c(x = "SEXP"), body = "
>
> SEXP e = PROTECT(lang2(install(\"sum\"), x));
> SEXP ans = PROTECT(eval(e, R_GlobalEnv));
> UNPROTECT(2);
> return ans;
>
> ")
>
> call_to_sum(1:3)
>
> The above works. My question is how do I add the argument "na.rm=TRUE" at C
> level to the above call? I have tried various things based on what is in
> section 5.11 but I did not manage to get it to work.
>
> Thank you
> Best regards
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Build a R call at C level

Tomas Kalibera
On 6/30/20 1:06 PM, Jan Gorecki wrote:
> It is quite known that R documentation on R C api could be improved...

Please see "5.11 Evaluating R expressions from C" from "Writing R
Extensions"

Best
Tomas

> Still R-package-devel mailing list should be preferred for this kind
> of questions.
> Not sure if that is the best way, but works.
>
> call_to_sum <- inline::cfunction(
>    language = "C",
>    sig = c(x = "SEXP"), body = "
>
> SEXP e = PROTECT(lang2(install(\"sum\"), x));
> SEXP r_true = PROTECT(CONS(ScalarLogical(1), R_NilValue));
> SETCDR(CDR(e), r_true);
> SET_TAG(CDDR(e), install(\"na.rm\"));
> Rf_PrintValue(e);
> SEXP ans = PROTECT(eval(e, R_GlobalEnv));
> UNPROTECT(3);
> return ans;
>
> ")
>
> call_to_sum(c(1L,NA,3L))
>
> On Tue, Jun 30, 2020 at 10:08 AM Morgan Morgan
> <[hidden email]> wrote:
>> Hi All,
>>
>> I was reading the R extension manual section 5.11 ( Evaluating R expression
>> from C) and I tried to build a simple call to the sum function. Please see
>> below.
>>
>> call_to_sum <- inline::cfunction(
>>    language = "C",
>>    sig = c(x = "SEXP"), body = "
>>
>> SEXP e = PROTECT(lang2(install(\"sum\"), x));
>> SEXP ans = PROTECT(eval(e, R_GlobalEnv));
>> UNPROTECT(2);
>> return ans;
>>
>> ")
>>
>> call_to_sum(1:3)
>>
>> The above works. My question is how do I add the argument "na.rm=TRUE" at C
>> level to the above call? I have tried various things based on what is in
>> section 5.11 but I did not manage to get it to work.
>>
>> Thank you
>> Best regards
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Build a R call at C level

MorganMorgan
Thanks Jan and Tomas for the feedback.
Answer from Jan is what I am looking for.
Maybe I am not looking in the right place buy it is not easy to understand
how these LCONS, CONS, SETCDR...etc works.

Thank you
Best regards
Morgan



On Tue, 30 Jun 2020, 12:36 Tomas Kalibera, <[hidden email]> wrote:

> On 6/30/20 1:06 PM, Jan Gorecki wrote:
> > It is quite known that R documentation on R C api could be improved...
>
> Please see "5.11 Evaluating R expressions from C" from "Writing R
> Extensions"
>
> Best
> Tomas
>
> > Still R-package-devel mailing list should be preferred for this kind
> > of questions.
> > Not sure if that is the best way, but works.
> >
> > call_to_sum <- inline::cfunction(
> >    language = "C",
> >    sig = c(x = "SEXP"), body = "
> >
> > SEXP e = PROTECT(lang2(install(\"sum\"), x));
> > SEXP r_true = PROTECT(CONS(ScalarLogical(1), R_NilValue));
> > SETCDR(CDR(e), r_true);
> > SET_TAG(CDDR(e), install(\"na.rm\"));
> > Rf_PrintValue(e);
> > SEXP ans = PROTECT(eval(e, R_GlobalEnv));
> > UNPROTECT(3);
> > return ans;
> >
> > ")
> >
> > call_to_sum(c(1L,NA,3L))
> >
> > On Tue, Jun 30, 2020 at 10:08 AM Morgan Morgan
> > <[hidden email]> wrote:
> >> Hi All,
> >>
> >> I was reading the R extension manual section 5.11 ( Evaluating R
> expression
> >> from C) and I tried to build a simple call to the sum function. Please
> see
> >> below.
> >>
> >> call_to_sum <- inline::cfunction(
> >>    language = "C",
> >>    sig = c(x = "SEXP"), body = "
> >>
> >> SEXP e = PROTECT(lang2(install(\"sum\"), x));
> >> SEXP ans = PROTECT(eval(e, R_GlobalEnv));
> >> UNPROTECT(2);
> >> return ans;
> >>
> >> ")
> >>
> >> call_to_sum(1:3)
> >>
> >> The above works. My question is how do I add the argument "na.rm=TRUE"
> at C
> >> level to the above call? I have tried various things based on what is in
> >> section 5.11 but I did not manage to get it to work.
> >>
> >> Thank you
> >> Best regards
> >>
> >>          [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Build a R call at C level

Dirk Eddelbuettel

On 30 June 2020 at 14:42, Morgan Morgan wrote:
| Thanks Jan and Tomas for the feedback.
| Answer from Jan is what I am looking for.
| Maybe I am not looking in the right place buy it is not easy to understand
| how these LCONS, CONS, SETCDR...etc works.

a) There are alternatives, and if memory serves I already told you about them.

b) This is quite likely the wrong list for your questions. Consider r-package-devel.

Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Build a R call at C level

MorganMorgan
In reply to this post by MorganMorgan
Sorry Dirk, I don't remember discussing this topic or alternatives with you
at all.
Have a nice day.

On Tue, 30 Jun 2020, 14:42 Morgan Morgan, <[hidden email]> wrote:

> Thanks Jan and Tomas for the feedback.
> Answer from Jan is what I am looking for.
> Maybe I am not looking in the right place buy it is not easy to understand
> how these LCONS, CONS, SETCDR...etc works.
>
> Thank you
> Best regards
> Morgan
>
>
>
> On Tue, 30 Jun 2020, 12:36 Tomas Kalibera, <[hidden email]>
> wrote:
>
>> On 6/30/20 1:06 PM, Jan Gorecki wrote:
>> > It is quite known that R documentation on R C api could be improved...
>>
>> Please see "5.11 Evaluating R expressions from C" from "Writing R
>> Extensions"
>>
>> Best
>> Tomas
>>
>> > Still R-package-devel mailing list should be preferred for this kind
>> > of questions.
>> > Not sure if that is the best way, but works.
>> >
>> > call_to_sum <- inline::cfunction(
>> >    language = "C",
>> >    sig = c(x = "SEXP"), body = "
>> >
>> > SEXP e = PROTECT(lang2(install(\"sum\"), x));
>> > SEXP r_true = PROTECT(CONS(ScalarLogical(1), R_NilValue));
>> > SETCDR(CDR(e), r_true);
>> > SET_TAG(CDDR(e), install(\"na.rm\"));
>> > Rf_PrintValue(e);
>> > SEXP ans = PROTECT(eval(e, R_GlobalEnv));
>> > UNPROTECT(3);
>> > return ans;
>> >
>> > ")
>> >
>> > call_to_sum(c(1L,NA,3L))
>> >
>> > On Tue, Jun 30, 2020 at 10:08 AM Morgan Morgan
>> > <[hidden email]> wrote:
>> >> Hi All,
>> >>
>> >> I was reading the R extension manual section 5.11 ( Evaluating R
>> expression
>> >> from C) and I tried to build a simple call to the sum function. Please
>> see
>> >> below.
>> >>
>> >> call_to_sum <- inline::cfunction(
>> >>    language = "C",
>> >>    sig = c(x = "SEXP"), body = "
>> >>
>> >> SEXP e = PROTECT(lang2(install(\"sum\"), x));
>> >> SEXP ans = PROTECT(eval(e, R_GlobalEnv));
>> >> UNPROTECT(2);
>> >> return ans;
>> >>
>> >> ")
>> >>
>> >> call_to_sum(1:3)
>> >>
>> >> The above works. My question is how do I add the argument "na.rm=TRUE"
>> at C
>> >> level to the above call? I have tried various things based on what is
>> in
>> >> section 5.11 but I did not manage to get it to work.
>> >>
>> >> Thank you
>> >> Best regards
>> >>
>> >>          [[alternative HTML version deleted]]
>> >>
>> >> ______________________________________________
>> >> [hidden email] mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-devel
>> > ______________________________________________
>> > [hidden email] mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel