anyFunction.c

language: C
license: GPL 2

Code for Snippet:

                
 
#include <R.h>
#include <Rinternals.h>
 
SEXP            getListElement(SEXP, char *);
SEXP            anyFC(SEXP);
 
SEXP
anyFC(SEXP args)
{
    int             nProtected = 0;
    double          x = REAL(getListElement(args, "x"))[0];
    SEXP            f = getListElement(args, "f");
    SEXP            env = getListElement(args, "env");
    SEXP            R_fcall, X, Y;
    PROTECT(R_fcall = lang2(f, R_NilValue)); ++nProtected;
    PROTECT(X = allocVector(REALSXP, 1)); ++nProtected;
    PROTECT(Y = allocVector(REALSXP, 1)); ++nProtected;
    REAL(X)[0] = x;
    SETCADR(R_fcall, X);
    REAL(Y)[0] = REAL(eval(R_fcall, env))[0];
    UNPROTECT(nProtected);
    return Y;
}
 
SEXP
getListElement(SEXP list, char *str)
{
    SEXP            elmt = R_NilValue, names = getAttrib(list, R_NamesSymbol);
    int             i;
    for (i = 0; i < length(list); i++)
        if (strcmp(CHAR(STRING_ELT(names, i)), str) == 0) {
            elmt = VECTOR_ELT(list, i);
            break;
        }
    return elmt;
}
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 549
Date Added: 2013-03-07 16:45:50
Last Modified: 2013-04-17 22:12:18

Web Analytics