[Rd] Calling R functions from C and setting function's formal list values

Andrew Finley afinley at stat.umn.edu
Thu Feb 16 14:59:08 CET 2006


Hello,
I am writing an extension that requires the user to pass a function into
the C code. The function returns a covariance matrix. The user defines an
arbitrary set of parameters as the function's arguments, which are used to
construct this covariance matrix.  Within the C code these parameters and
hence covariance matrix are iteratively updated.

The user's function might look like this:

cov.function <- function(sigmasq, tausq, phi){
  sigmasq*exp(-phi*D) + tausq*diag(n)
}

The way my C code is now, the iteratively proposed values for the cov.fun
parameters are in the same order as the parameters used in the cov.fun
function (i.e., same order as the formal list).  So what I'd like to do is
just set the value portion of the tags in the cov.fun formal list, then
just call the function using its newly set defaults at the values to be
passed in.

This way might look is:

for(i = 0; i < iters; i++){
  //proposed new values for cov.fun parameters and set them as the
function's
  //default arg. values

  //call the function and collect the returned matrix
  PROTECT(RFnCall = lang1(CovFn));
  PROTECT(fnCov = eval(RFnCall, env));

}

My question is, does this seem reasonable?  And if so, how do I set the
value portion of the formal's list tags.  Also, is the formal list a
LISTSXP.  I have read the sections on 'Evaluating R expression from C' in
the docs. But the idea of setting the argument defaults then just calling
the function is a bit different.

Thanks for your time-
Andy


Research Fellow
Department of Forest Resources
University of Minnesota
Office: 305 Green Hall
Phone: (612) 624-1714
Fax: (612) 625-5212
web: http://blue.fr.umn.edu



More information about the R-devel mailing list