I'm trying to create this object in C

SWX.RET[1:6,c("SBI,"SPI","SII")]

While i am able to access and use a simple SWX.RET object, I'm getting
confused on how to create an object with the array subscripts like above.

Here is what I tried to do. It doesn't work becase [ is obviously not an
operation on SWX.RET. So how do I create this object so that I may be able
to use it in an expression?

#include <stdio.h>
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include <Rembedded.h>

int main(int argc, char** argv)
{

    SEXP e,c,portSpec,portData,portConstr,portVal,tsAssets;
    int errorOccurred,nx,ny,i,j;
    double *v;
    const char *x,*y;

    Rf_initEmbeddedR(argc, argv);

    // loading fPortfolio
    PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
    R_tryEval(e, R_GlobalEnv, NULL);
    UNPROTECT(1);


    // creating a default portfolioSpec object
    PROTECT(e=lang1(install("portfolioSpec")));
    PROTECT(portSpec=R_tryEval(e,R_GlobalEnv, NULL));

    // creating a portfolioData object
    // reqd: take a time series and make a porftfolio object

PROTECT(e=lang4(install("c"),mkString("SBI"),mkString("SPI"),mkString("SII")));
    PROTECT(c=R_tryEval(e,R_GlobalEnv,NULL));

    PROTECT(e=lang3(install("["),install("SWX.RET"),c));
    PROTECT(portData=R_tryEval(e,R_GlobalEnv,NULL));

    PROTECT(e=lang2(install("print"),portData));
    R_tryEval(e,R_GlobalEnv,NULL);


    UNPROTECT(3);

    Rf_endEmbeddedR(0);

    return 0;
}

On Tue, Sep 15, 2009 at 1:05 PM, Abhijit Bera <abhib...@gmail.com> wrote:

> Hi
>
> I have confusion in evaluating this expression using C. It's similar to my
> previous expression with the addition of row and column specifiers. I'm
> confused how to create such objects.
>
> Covariance <- round(cov(100 * SWX.RET[1:6,c("SBI","SPI","SII")]),
>
> I thought of creating vectors for 1:6 and c("SBI","SPI","SII")
>
> But I don't know how to create the object
> SWX.RET[1:6,c("SBI","SPI","SII")]
>
>
> On Tue, Aug 25, 2009 at 6:11 PM, Abhijit Bera <abhib...@gmail.com> wrote:
>
>> Hi
>>
>> I think I have asked these questions earlier, but I been able to find
>> answers from the documentation (which I found poorly written in several
>> places). Will someone be kind enough to give me answers and enlighten me?
>> (as in explain with CODE?)
>>
>> I want to embed R in my application and use the fPortfolio package for
>> carrying out risk management computations. Right now I'm reading the
>> Rmetrics Ebook and trying to convert the various examples into embedded C
>> code.
>>
>> Coming from a strictly C background, I have slight difficulty in
>> comprehending a functional language like R and it gets worse when I try to
>> embed R into a procedural language like C. So here is a list of my doubts:
>>
>> 1) I am very confused on how the lang 1 2 3 4 ... set of functions work. I
>> haven't found any relevant documentation explaining it clearly. I have a
>> vague idea but still I cannot understand how I would evaluate an R
>> expression like Covariance <- round(cov(100 * SWX.RET), digits = 4) using
>> lang, install and R_tryEval.
>>
>> 2) What exactly does install do?
>>
>> 3) I wrote the following code:
>>
>> #include <Rinternals.h>
>> #include <Rembedded.h>
>>
>> int main (int argc, char** argv) {
>>
>>     SEXP e,val;
>>     int errorOccurred;
>>
>>     Rf_initEmbeddedR(argc, argv);
>>
>>     // library("fPortfolio")
>>     PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
>>     R_tryEval(e, R_GlobalEnv, NULL);
>>     UNPROTECT(1);
>>
>>    // colMeans(SWX.RET)
>>     PROTECT(e = lang2(install("colMeans"), install("SWX.RET")));
>>     val = (R_tryEval(e, NULL, &errorOccurred));
>>
>>     Rf_PrintValue(val);
>>
>>     return 0;
>>
>> }
>>
>> When I tried :
>>
>> >mean(SWX.RET)
>>
>> in the R prompt I got the following output:
>>
>>          SBI          SPI          SII         LP25         LP40
>> LP60
>> 4.660521e-06 2.153198e-04 2.033869e-04 1.388886e-04 1.349041e-04
>> 1.226859e-04
>>
>>
>> However when I replaced colMeans with mean in the C code above I got a
>> mean of the means (0.0001366410) of all the columns when Rf_PrintValue was
>> called. Using colMeans gave me the output as shown above. Why does this
>> happen? How do I get the above output using mean?
>>
>> 4) From the above code segment, how can I deal with the SEXPREC val which
>> is returned by R_tryEval in the above code and convert it to my own local
>> vector datatype? How do I access the values of val? val will now be a
>> timeseries so how do i convert it?
>>
>>
>> Thanks
>>
>> Abhijit Bera
>>
>>
>>
>>
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to