[R] Calling primitive functions from C code

Duncan Temple Lang duncan at research.bell-labs.com
Wed Dec 31 18:17:01 CET 2003


As Martin has just mailed, please post a question to one and only one
R-* list.

Without knowing the details of your task, I would strongly suggest
that you consider calling the corresponding R function rather than the
much, much lower-level routines in the C code.  For one, the routines
listed in R_FunTab do have a different and more complex calling
sequence as you have discovered.  While do_subset_dflt might do
precisely what you want, calling these internal routines in general is
best avoided as you are by-passing aspects of the language that might
be done at a higher level, i.e. in the R code itself, such as method
dispatching, etc.  When faced with these issues in the past, I have
written an R function that does what I want in R code and then called
that from my C code. The extra steps are unlikely to be the most
critical bottlenecks.  

If were really advocating using these routines in embedded code, we
would probably centralize the code with a more appropriate signature
and have do_subset_dflt unwrap its arguments and call that new routine
which would also be available to programmers such as yourself.  But
since we don't do this, you might want to worry about any changes that
happen to the routine for use by R itself.

So my advice is, especially if you are not a regular and knowledgeable
C programmer, keep things as simple as possible (and no simpler!) and
use just one calling scheme, that is to call an R function from C code
which is covered in the Writing R extensions manual.

 D.



Whit Armstrong wrote:
> Does anyone have an example of calling primitive or internal functions from
> C code that they would share with me?
> 
> I am having trouble trying to figure out how to construct the proper
> arguments to pass to "do_subset_dflt"
> 
> Here is the prototype:
> SEXP do_subset_dflt(SEXP call, SEXP op, SEXP args, SEXP rho);
> 
> The R_FunTab from "names.c" gives some additional information on the
> arguments that the function expects.
> 
> {".subset",	do_subset_dflt,	1,	1,	-1,	{PP_FUNCALL,
> PREC_FN,	  0}},
> 
> However, this is a little intimidating for someone like me who doesn't do
> much C programming.
> 
> I understand that args is simply the argument list: x, rowsubset, colsubset,
> drop, but the other variables are a mystery to me.
> 
> I've read the Appendix A R (internal) programming miscellanea from
> R_exts.pdf, but there are no examples of calling these functions from C
> code.
> http://cran.r-project.org/doc/manuals/R-exts.pdf
> <http://cran.r-project.org/doc/manuals/R-exts.pdf> 
> 
> How does one create call, op and rho to be passed into do_subset_dflt?
> 
> Any advice on constructing a successful call to do_subset_dflt from C would
> be greatly appreciated.
> 
> Thanks,
> Whit Armstrong
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
         http://cm.bell-labs.com/stat/duncan




More information about the R-help mailing list